problem with if statement

This is a discussion on problem with if statement within the C Programming forums, part of the General Programming Boards category; hello every one I'm trying to solve this question but i solve it wrong Write a program to prompt 5 ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    11

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > 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...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    11
    at first i wanna thank u alot about your answer

    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?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    11
    how can i solve it using different solution?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by MK27 View Post
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, the more you code the more you think that way. I even brush my teeth in an iterative loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Would a lisp programmer have 32 brushes working in his mouth?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That might explain the "lisp smile":

    Code:
    ((((((((((((((((((((((  )))))))))))))))))))))))
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    LOL it would look like:

    Code:
    (toothpaste
     (toothbrush
      (tooth1,tooth2,tooth3...)
      )
    )
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    11
    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

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by eagle0eyes View Post
    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;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM
  4. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21