problem with if statement

• 05-27-2010
eagle0eyes
problem with if statement
hello every one
I'm trying to solve this question but i solve it wrong

Write a program to prompt 5 integers from the operator. Find the largest of those 5 integers and display it to the screen.

Sample input-output
Number 1: 8
Number 2: 12
Number 3: 5
Number 4: 22
Number 5: 17
The largest is: 22

this is my solution which is totally wrong
Code:

``` #include<stdio.h> int main(void) {  float num1;  float num2;  float num3;  float num4;  float num5;           printf ("inter 5 numbers "); scanf ("%f%f%f%f%f",&num1,&num2,&num3,&num4,&num5); if (num1>num2,num3,num4,num5)               printf("\nthe bigger number is %f",num1);       else         if (num1<num2,num3,num4,num5)           printf("\nthe bigger number is %f",num2);           if (num3>num1,num2,num4,num5)             printf("\nthe bigger number is %f",num3);         else         if (num4>num1,num2,num3,num5)               printf("\nthe bigger number is %f",num4);           else      printf("\nthe bigger number is %f",num5);                 getchar();       getchar();               }```
i wont know the right answer
• 05-27-2010
Salem
> if (num1>num2,num3,num4,num5)
This doesn't compare num1 with 4 other numbers.

You need
if ( num1 > num2 && num1 > num3 && and so on...
• 05-27-2010
eagle0eyes

now the program work fine when i put the value from 1 to 10
but if i put the values such as 33
23
12
54
45
it doesn't work so whats wrong now?
• 05-27-2010
claudiu
Alternatively, you may want to consider a different solution that does not involve so many if else blocks. Hint: You don't need to remember all the numbers in different variables in order to determine which one is the largest.
• 05-27-2010
eagle0eyes
how can i solve it using different solution?
• 05-27-2010
claudiu
You need to remember the greatest number you have read so far each time you read another number. However, that number you can read can be stored in a single variable. All you need are two variables:

nr - the number currently read
max - the greatest number so far

Then check if the number you have just read is bigger than what you had as max so far.
• 05-27-2010
MK27
Use a for() loop and two variables, one for the input and one as a sort of "high score". The first time thru (you could also do this before the loop), you set the high to the input. Thereafter, if the input is higher than the high, you set the high to the input.

At the end, the high value will be the highest number entered.

I see great minds think alike claudiu ;)
• 05-27-2010
claudiu
Quote:

Originally Posted by MK27
I see great minds think alike claudiu ;)

Yes, this was a real tough one to figure out ;).

But hey, if this was my first time programming I would be using the 5 number method as well :), it just makes sense for someone who doesn't divide his whole life in small algorithmic steps.
• 05-27-2010
MK27
Yeah, the more you code the more you think that way. I even brush my teeth in an iterative loop. :D
• 05-27-2010
fronty
Would a lisp programmer have 32 brushes working in his mouth? :D
• 05-27-2010
MK27
That might explain the "lisp smile":

Code:

`((((((((((((((((((((((  )))))))))))))))))))))))`
• 05-27-2010
claudiu
LOL it would look like:

Code:

``` (toothpaste  (toothbrush   (tooth1,tooth2,tooth3...)   ) )```
• 05-27-2010
eagle0eyes
hey guys finally i solve it using 5 integers

and i dont know what u talking about cuz im new in c programming happy life
• 05-27-2010
MK27
Quote:

Originally Posted by eagle0eyes
hey guys finally i solve it using 5 integers

and i dont know what u talking about cuz im new in c programming happy life

Code:

```#include <stdio.h> int main(void) {         int in, high, i;         for (i=0;i<5;i++) {                 printf("Enter an integer: ");                 scanf("%d",&in);                 if (!i) high = in;        // the first loop                 else if (in > high) high = in;         }         printf("High: %d\n",high);         return 0; }```