# Newbie Problem

Discussion in 'C Programming' started by Larry Tooley, Jan 28, 2007.

1. ### Larry TooleyGuest

I am new to C and am using "C How to Program" to get started with my C
education. I am working an exercise where I am supposed to use only
arithmetic, equity and relational operators, and the "if" statement to
determine the largest and smallest of three integers. It seems like there
is an easy answer, but I just haven't come up with a solution. It has
been a long time since CS101 with Pascal so pardon my ignorance.

Thanks for the help.

Larry

Larry Tooley, Jan 28, 2007

2. ### Gustavo RondinaGuest

Larry Tooley <> writes:

>determine the largest and smallest of three integers.

if A > C then swap them
if A > B then swap them
if B > C then swap them

Now A is the smallest and C is the largest.

--
Gustavo Rondina
http://gustgr.freeshell.org

Gustavo Rondina, Jan 28, 2007

3. ### Kelly BrookesGuest

Larry Tooley wrote:
> I am new to C and am using "C How to Program" to get started with my C
> education. I am working an exercise where I am supposed to use only
> arithmetic, equity and relational operators, and the "if" statement to
> determine the largest and smallest of three integers. It seems like there
> is an easy answer, but I just haven't come up with a solution. It has
> been a long time since CS101 with Pascal so pardon my ignorance.
>
> Thanks for the help.
>
> Larry

/*Maximum of three integers*/
int max(int a,int b,int c)
{
return ((a>b&&a>c)? aa>b &&a<c)? cb>c)?b:c);
}
/*Minimum of three integers*/
int min(int a,int b,int c)
{
return ((a<b && a<c)?aa>b&&a<c)?bb<c)?b:c);
}

Kelly Brookes, Jan 28, 2007
4. ### Kelly BrookesGuest

Larry Tooley wrote:
> I am new to C and am using "C How to Program" to get started with my C
> education. I am working an exercise where I am supposed to use only
> arithmetic, equity and relational operators, and the "if" statement to
> determine the largest and smallest of three integers. It seems like there
> is an easy answer, but I just haven't come up with a solution. It has
> been a long time since CS101 with Pascal so pardon my ignorance.
>
> Thanks for the help.
>
> Larry

int max(int a,int b,int c)
{
return ((a>b&&a>c)? aa>b &&a<c)? cb>c)?b:c);
}

int min(int a,int b,int c)
{
return ((a<b && a<c)?aa>b&&a<c)?bb<c)?b:c);
}

Kelly Brookes, Jan 28, 2007
5. ### Malcolm McLeanGuest

"Larry Tooley" <> wrote in message
>I am new to C and am using "C How to Program" to get started with my C
> education. I am working an exercise where I am supposed to use only
> arithmetic, equity and relational operators, and the "if" statement to
> determine the largest and smallest of three integers. It seems like there
> is an easy answer, but I just haven't come up with a solution. It has
> been a long time since CS101 with Pascal so pardon my ignorance.
>

C does things in functions.

So start by defining your function.

/*
max3 - gets the biggest of three integers.
Params: a, b, c - three integers to test
Returns: value of the biggest integer.
*/

int max(int a, int b, int c)
{

/* code here */
}

Now how do we do the code?

If we were doing two numbers we could say

if( a > b)
else

If we are doing three numbers, the biggest of a,b,c must be either the
biggest of a, b, or it must be c.
So effectively we've just got two tests with two numbers.

If you are not allowed assignment statements then you can write the whole
lot on one line, using the ?: operator instead of if ... else. It is messy
but it will show you how to use the operators.

Malcolm McLean, Jan 28, 2007
6. ### Ben BacarisseGuest

Gustavo Rondina <> writes:

> Larry Tooley <> writes:
>
>>determine the largest and smallest of three integers.

>
>
> if A > C then swap them
> if A > B then swap them
> if B > C then swap them
>
> Now A is the smallest and C is the largest.

I was going to point out that assignment was not one of the allowed
operations when I thought of:

void order3(int a, int b, int c)
{
if (a > c) order3(c, b, a);
else if (a > b) order3(b, a, c);
else if (b > c) order3(a, c, b);
else printf("min = %d, max = %d\n", a, c);
}

Function calls were not allowed either, of course, but I think they
get a special exemption in this sort of exercise.

--
Ben.

Ben Bacarisse, Jan 28, 2007
7. ### LeoGuest

Larry Tooley wrote:
> I am new to C and am using "C How to Program" to get started with my C
> education. I am working an exercise where I am supposed to use only
> arithmetic, equity and relational operators, and the "if" statement to
> determine the largest and smallest of three integers. It seems like there
> is an easy answer, but I just haven't come up with a solution. It has
> been a long time since CS101 with Pascal so pardon my ignorance.
>
> Thanks for the help.
>
> Larry

#include<stdio.h>

int max(int a,int b,int c)
{
return ((a>b&&a>c)? aa>b &&a<c)? cb>c)?b:c);
}

int min(int a,int b,int c)
{
return ((a<b && a<c)?aa>b&&a<c)?bb<c)?b:c);
}

int main(void)
{
int a,b,c;
printf("Enter the three integers");
scanf("%d %d %d",&a,&b,&c);
printf("Max:%d\nMin:%d\n",max(a,b,c),min(a,b,c));
return 0;
}

Leo, Jan 28, 2007
8. ### Robert GambleGuest

On Jan 28, 1:31 am, Larry Tooley <> wrote:
> I am new to C and am using "C How to Program" to get started with my C
> education. I am working an exercise where I am supposed to use only
> arithmetic, equity and relational operators, and the "if" statement to
> determine the largest and smallest of three integers.

The following code will find the greatest of 3 integers entered into
stdin, it is straight-forward and appears to meet your restrictions.
If the largest value is shared by multiple variables, the first one
entered wins. The same approach can be used to find the smallest
integer.

#include <stdio.h>

int main (void) {
int a, b, c;
printf("Enter 3 integers\n");
scanf("%d %d %d", &a, &b, &c);

if (a >= b)
if (a >= c)
printf("a is greatest\n");
else if (b >= c)
printf("b is greatest\n");
else
printf("c is greatest\n");
else
if (b >= c)
printf("b is greatest\n");
else
printf("c is greatest\n");

return 0;
}

Robert Gamble

Robert Gamble, Jan 28, 2007

Malcolm McLean wrote:
> "Larry Tooley" <> wrote in message
>
>>I am new to C and am using "C How to Program" to get started with my C
>>education. I am working an exercise where I am supposed to use only
>>arithmetic, equity and relational operators, and the "if" statement to
>>determine the largest and smallest of three integers. It seems like there
>>is an easy answer, but I just haven't come up with a solution. It has
>>been a long time since CS101 with Pascal so pardon my ignorance.

>
> C does things in functions.
> So start by defining your function.
>
> /*
> max3 - gets the biggest of three integers.
> Params: a, b, c - three integers to test
> Returns: value of the biggest integer.
> */
>
> int max(int a, int b, int c)
> {
>
> /* code here */
> }
>
> Now how do we do the code?
> If we were doing two numbers we could say
>
> if( a > b)
> else
>

....
> If you are not allowed assignment statements then you can write the whole
> lot on one line, using the ?: operator instead of if ... else. It is messy
> but it will show you how to use the operators.

You can also use separate return statements:
if (a > b) return a;
else return b;

For 3 values, nested if statements work:
if (something) {
if (another condition) {
return proper_value;
} else ...

The result can be found by doing tests and handling each case.

--

10. ### CBFalconerGuest

Robert Gamble wrote:
> On Jan 28, 1:31 am, Larry Tooley <> wrote:
>
>> I am new to C and am using "C How to Program" to get started with
>> my C education. I am working an exercise where I am supposed to
>> use only arithmetic, equity and relational operators, and the "if"
>> statement to determine the largest and smallest of three integers.

>
> The following code will find the greatest of 3 integers entered
> into stdin, it is straight-forward and appears to meet your
> restrictions. If the largest value is shared by multiple variables,
> the first one entered wins. The same approach can be used to find
> the smallest integer.
>
> #include <stdio.h>
>
> int main (void) {
> int a, b, c;
> printf("Enter 3 integers\n");
> scanf("%d %d %d", &a, &b, &c);
>
> if (a >= b)
> if (a >= c)
> printf("a is greatest\n");
> else if (b >= c)
> printf("b is greatest\n");
> else
> printf("c is greatest\n");
> else
> if (b >= c)
> printf("b is greatest\n");
> else
> printf("c is greatest\n");
>
> return 0;
> }

A sin. Using scanf without checking the return value. Suggested
improvement:

#include <stdio.h>

int main (void) {
int a, b, c;

printf("Enter 3 integers\n");
if (3 != scanf("%d %d %d", &a, &b, &c))
puts("You didn't listen");
else {
if (a >= b)
if (a >= c) putchar('a');
else if (b >= c) putchar('b');
else putchar('c');
else if (b >= c) putchar('b');
else putchar('c');
puts(" is greatest");
}
return 0;
}

which will also generate considerably less object code. This also
shows up the even shorter:

#include <stdio.h>

int main (void) {
int a, b, c;

puts("Enter 3 integers");
if (3 != scanf("%d %d %d", &a, &b, &c))
puts("You didn't listen");
else {
if ((a >= b) && (a >= c)) putchar('a');
else if (b >= c) putchar('b');
else putchar('c');
puts(" is greatest");
}
return 0;
}

My point being that using the first implementation that occurs to
you may not be optimum. Stand back and look at your code for
insecurities and better expressions. 22 lines has become 16, even
with input checking. It is easier to examine the above code and
satisfy yourself that it is correct because there are no longer
multiple paths to a single conclusion. You can easily see that
adding 4 similar lines after "puts(" is greatest");" will also dig
out the smallest.

Routinely doing this will give you practice, and you will soon be
writing the clearer and more effective code in the first place.

I got rid of printf in favor of puts and putchar, because the added
abilities of printf are never used. This can often significantly
reduce the size of the end program and improve its speed. It also
means you can't get the type signifiers wrong.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

CBFalconer, Jan 29, 2007
11. ### Richard BosGuest

Kelly Brookes <> wrote:

> Larry Tooley wrote:
> > I am new to C and am using "C How to Program" to get started with my C
> > education. I am working an exercise where I am supposed to use only
> > arithmetic, equity and relational operators, and the "if" statement to
> > determine the largest and smallest of three integers. It seems like there
> > is an easy answer, but I just haven't come up with a solution. It has
> > been a long time since CS101 with Pascal so pardon my ignorance.

> int max(int a,int b,int c)
> {
> return ((a>b&&a>c)? aa>b &&a<c)? cb>c)?b:c);
> }
>
> int min(int a,int b,int c)
> {
> return ((a<b && a<c)?aa>b&&a<c)?bb<c)?b:c);
> }

Even simpler:

int max3(int a, int b, int c)
{
return (a>b && a>c)? a: (b>c)? b: c;
}

int min3(int a, int b, int c)
{
return (a<b && a<c)? a: (b<c)? b: c;
}

If a isn't larger than both others, either b or c is. If, under those
circumstances, b is larger than c, it must be the largest.

(Depending on the system, changing > to >= may be more efficient. It's
not likely to be a measurable difference unless you're doing gazillions
of comparisons; in which case, measure, don't pre-optimise; and if it
doesn't matter, go for legibility.)

Richard

Richard Bos, Jan 29, 2007
12. ### LeoGuest

CBFalconer wrote:

> A sin. Using scanf without checking the return value. Suggested
> improvement:

can you please explain the line that follows?how do we check for the
return to to be say float and not a char during input?

if (3 != scanf("%d %d %d", &a, &b, &c))
> puts("You didn't listen");

Leo, Jan 29, 2007
13. ### LeoGuest

CBFalconer wrote:

> A sin. Using scanf without checking the return value. Suggested
> improvement:

can you please explain the line that follows?how do we check for the
return to to be say float and not a char during input?

if (3 != scanf("%d %d %d", &a, &b, &c))
> puts("You didn't listen");

Leo, Jan 29, 2007
14. ### CBFalconerGuest

Leo wrote:
> CBFalconer wrote:
>
>> A sin. Using scanf without checking the return value. Suggested
>> improvement:

>
> can you please explain the line that follows?how do we check for
> the return to to be say float and not a char during input?
>
>> if (3 != scanf("%d %d %d", &a, &b, &c))
>> puts("You didn't listen");

Just read the documentation. For example, n869 says:

7.19.6.4 The scanf function

Synopsis

[#1]
#include <stdio.h>
int scanf(const char * restrict format, ...);

Description

[#2] The scanf function is equivalent to fscanf with the
argument stdin interposed before the arguments to scanf.

Returns

[#3] The scanf function returns the value of the macro EOF
if an input failure occurs before any conversion.
Otherwise, the scanf function returns the number of input
items assigned, which can be fewer than provided for, or
even zero, in the event of an early matching failure.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

CBFalconer, Jan 29, 2007
15. ### Robert GambleGuest

On Jan 28, 9:14 pm, CBFalconer <> wrote:
> Robert Gamble wrote:
> > On Jan 28, 1:31 am, Larry Tooley <> wrote:

>
> >> I am new to C and am using "C How to Program" to get started with
> >> my C education. I am working an exercise where I am supposed to
> >> use only arithmetic, equity and relational operators, and the "if"
> >> statement to determine the largest and smallest of three integers.

>
> > The following code will find the greatest of 3 integers entered
> > into stdin, it is straight-forward and appears to meet your
> > restrictions. If the largest value is shared by multiple variables,
> > the first one entered wins. The same approach can be used to find
> > the smallest integer.

>
> > #include <stdio.h>

>
> > int main (void) {
> > int a, b, c;
> > printf("Enter 3 integers\n");
> > scanf("%d %d %d", &a, &b, &c);

>
> > if (a >= b)
> > if (a >= c)
> > printf("a is greatest\n");
> > else if (b >= c)
> > printf("b is greatest\n");
> > else
> > printf("c is greatest\n");
> > else
> > if (b >= c)
> > printf("b is greatest\n");
> > else
> > printf("c is greatest\n");

>
> > return 0;
> > }

> A sin. Using scanf without checking the return value.

The focus was on solving the OP's problem given the provided
restrictions while trying to convey a simple concept, but I agree with
you in principle and I could have thrown in a simple check.

> Suggested
> improvement:
>
> #include <stdio.h>
>
> int main (void) {
> int a, b, c;
>
> printf("Enter 3 integers\n");
> if (3 != scanf("%d %d %d", &a, &b, &c))
> puts("You didn't listen");
> else {
> if (a >= b)
> if (a >= c) putchar('a');
> else if (b >= c) putchar('b');
> else putchar('c');
> else if (b >= c) putchar('b');
> else putchar('c');
> puts(" is greatest");
> }
> return 0;
>
> }which will also generate considerably less object code.

Which really wasn't the goal.

> This also
> shows up the even shorter:
>
> #include <stdio.h>
>
> int main (void) {
> int a, b, c;
>
> puts("Enter 3 integers");
> if (3 != scanf("%d %d %d", &a, &b, &c))
> puts("You didn't listen");
> else {
> if ((a >= b) && (a >= c)) putchar('a');
> else if (b >= c) putchar('b');
> else putchar('c');
> puts(" is greatest");
> }
> return 0;
> }

Probably the most obvious solution but the use of && doesn't meet the
OP's requirement of using only "arithmetic, equity and relational
operators, and the if statement".

> My point being that using the first implementation that occurs to
> you may not be optimum.

Again, optimization wasn't the goal here, providing a solution that
clearly demonstrates the concept the given the requirements was and I
think the solution provided did that.

[diatribe about writing optimized code snipped]

I would personally worry about trying to comprehend the requirements

Robert Gamble

Robert Gamble, Jan 29, 2007
16. ### Larry TooleyGuest

On Sun, 28 Jan 2007 12:10:08 -0700, Thad Smith wrote:

> Malcolm McLean wrote:
>> "Larry Tooley" <> wrote in message
>>
>>>I am new to C and am using "C How to Program" to get started with my C
>>>education. I am working an exercise where I am supposed to use only
>>>arithmetic, equity and relational operators, and the "if" statement to
>>>determine the largest and smallest of three integers. It seems like there
>>>is an easy answer, but I just haven't come up with a solution. It has
>>>been a long time since CS101 with Pascal so pardon my ignorance.

>>
>> C does things in functions.
>> So start by defining your function.
>>
>> /*
>> max3 - gets the biggest of three integers.
>> Params: a, b, c - three integers to test
>> Returns: value of the biggest integer.
>> */
>>
>> int max(int a, int b, int c)
>> {
>>
>> /* code here */
>> }
>>
>> Now how do we do the code?
>> If we were doing two numbers we could say
>>
>> if( a > b)
>> else
>>

> ...
>> If you are not allowed assignment statements then you can write the whole
>> lot on one line, using the ?: operator instead of if ... else. It is messy
>> but it will show you how to use the operators.

>
> You can also use separate return statements:
> if (a > b) return a;
> else return b;
>
> For 3 values, nested if statements work:
> if (something) {
> if (another condition) {
> return proper_value;
> } else ...
>
> The result can be found by doing tests and handling each case.

Thanks Thad. I think the nested approach is what they were looking for.
The parameters were very restrictive.

Larry Tooley, Jan 29, 2007
17. ### Peter Shaggy HaywoodGuest

Groovy hepcat Larry Tooley was jivin' on Sun, 28 Jan 2007 01:31:37
-0500 in comp.lang.c.
Newbie Problem's a cool scene! Dig it!

>I am new to C and am using "C How to Program" to get started with my C
>education. I am working an exercise where I am supposed to use only
>arithmetic, equity and relational operators, and the "if" statement to
>determine the largest and smallest of three integers. It seems like there
>is an easy answer, but I just haven't come up with a solution. It has
>been a long time since CS101 with Pascal so pardon my ignorance.

Divide and conquer. That's the first principle of programming. Break
a large problem down into a smaller one. You determine the largest of
three numbers by first finding the largest of two of them, then
finding the largest of the result of that operation and the last
number. We'll express the largest of two values as MAX(x,y) where x
and y represent the two values.
So, say you have numbers labelled a, b and c. To find the largest of
these three, you first find MAX(a,b). Then you find MAX(MAX(a,b),c).
In fact, C's syntax makes it easy to do that in one go. All you need
is a function that can return the larger of two values. This, of
course, is trivial.

int max(int x, int y)
{
if(x >= y)
{
return x;
}
else
{
return y;
}
}

#include <stdio.h>

int main(void)
{
int a = 107, b = 42, c = 23;
int big = max(max(a, b), c);

printf("The biggest of %d, %d and %d is %d.\n", a, b, c, big);

return 0;
}

The above code is quite self explanitory. Notice the use of the max()
function. If max(a, b) returns the largest of a and b, then max(max(a,
b), c) returns the largest of (the largest of a and b) and c; which,
of course, gives us the largest of the three numbers. What could be
simpler?

--

Dig the even newer still, yet more improved, sig!