Velocity Reviews > Newbie Problem

# Newbie Problem

Larry Tooley
Guest
Posts: n/a

 01-28-2007
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

Gustavo Rondina
Guest
Posts: n/a

 01-28-2007
Larry Tooley <(E-Mail Removed)> 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

Kelly Brookes
Guest
Posts: n/a

 01-28-2007
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
Guest
Posts: n/a

 01-28-2007
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);
}

Malcolm McLean
Guest
Posts: n/a

 01-28-2007

"Larry Tooley" <(E-Mail Removed)> 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.

Ben Bacarisse
Guest
Posts: n/a

 01-28-2007
Gustavo Rondina <(E-Mail Removed)> writes:

> Larry Tooley <(E-Mail Removed)> 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.

Leo
Guest
Posts: n/a

 01-28-2007
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;
}

Robert Gamble
Guest
Posts: n/a

 01-28-2007
On Jan 28, 1:31 am, Larry Tooley <(E-Mail Removed)> 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

Guest
Posts: n/a

 01-28-2007
Malcolm McLean wrote:
> "Larry Tooley" <(E-Mail Removed)> 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)
> answer = a;
> else
> answer = b;
>

....
> 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.

--

CBFalconer
Guest
Posts: n/a

 01-29-2007
Robert Gamble wrote:
> On Jan 28, 1:31 am, Larry Tooley <(E-Mail Removed)> 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>