Thread: Doubt about max, minimum and average value with if function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    Question Doubt about max, minimum and average value with if function

    Hi,
    I started this semester with C programming and one of the assignments asks to find the max value, minimum value and average of 10 integers with range of 0 and 100.

    So here is what I did:
    Code:
    int grad0,grad1,grad2,grad3,grad4,grad5,grad6,grad7,grad8,grad9;
    
    	if (grad0 > grad1 && grad0 > grad2 && grad0 > grad3 && grad0 > grad4 && grad0 > grad5 && grad0 > grad6 && grad0 > grad7 && grad0 > grad8 && grad0 > grad9)
    			{
    				printf("The max value of your grades is: %d\n", grad0);
    			}
    		else if (grad1 > grad0 && grad1 > grad2 && grad1 > grad3 && grad1 > grad4 && grad1 > grad5 && grad1 > grad6 && grad1 > grad7 && grad1 > grad8 && grad1 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad1);
    		}
    		else if (grad2 > grad0 && grad2 > grad1 && grad2 > grad3 && grad2 > grad4 && grad2 > grad5 && grad2 > grad6 && grad2 > grad7 && grad2 > grad8 && grad2 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad2);
    		}
    		else if (grad3 > grad0 && grad3 > grad1 && grad3 > grad2 && grad3 > grad4 && grad3 > grad5 && grad3 > grad6 && grad3 > grad7 && grad3 > grad8 && grad3 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad3);
    		}
    		else if (grad4 > grad0 && grad4 > grad1 && grad4 > grad2 && grad4 > grad3 && grad4 > grad5 && grad4 > grad6 && grad4 > grad7 && grad4 > grad8 && grad4 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad4);
    		}
    		else if (grad5 > grad0 && grad5 > grad1 && grad5 > grad2 && grad5 > grad3 && grad5 > grad4 && grad5 > grad6 && grad5 > grad7 && grad5 > grad8 && grad5 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad5);
    		}
    		else if (grad6 > grad0 && grad6 > grad1 && grad6 > grad2 && grad6 > grad3 && grad6 > grad4 && grad6 > grad5 && grad6 > grad7 && grad6 > grad8 && grad6 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad6);
    		}
    		else if (grad7 > grad0 && grad7 > grad1 && grad7 > grad2 && grad7 > grad3 && grad7 > grad4 && grad7 > grad5 && grad7 > grad6 && grad7 > grad8 && grad7 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad7);
    		}
    		else if (grad8 > grad0 && grad8 > grad1 && grad8 > grad2 && grad8 > grad3 && grad8 > grad4 && grad8 > grad5 && grad8 > grad6 && grad8 > grad7 && grad8 > grad9)
    		{
    			printf("The max value of your grades is: %d\n", grad8);
    		}
    		else if (grad9 > grad0 && grad9 > grad1 && grad9 > grad2 && grad9 > grad3 && grad9 > grad4 && grad9 > grad5 && grad9 > grad6 && grad9 > grad7 && grad9 > grad8)
    		{
    			printf("The max value of your grades is: %d\n", grad9);
    		}
    	if (grad0 < grad1 && grad0 < grad2 && grad0 < grad3 && grad0 < grad4 && grad0 < grad5 && grad0 < grad6 && grad0 < grad7 && grad0 < grad8 && grad0 < grad9)
    			{
    				printf("The min value of your grades is: %d\n", grad0);
    			}
    		else if (grad1 < grad0 && grad1 < grad2 && grad1 < grad3 && grad1 < grad4 && grad1 < grad5 && grad1 < grad6 && grad1 < grad7 && grad1 < grad8 && grad1 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad1);
    		}
    		else if (grad2 < grad0 && grad2 < grad1 && grad2 < grad3 && grad2 < grad4 && grad2 < grad5 && grad2 < grad6 && grad2 < grad7 && grad2 < grad8 && grad2 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad2);
    		}
    		else if (grad3 < grad0 && grad3 < grad1 && grad3 < grad2 && grad3 < grad4 && grad3 < grad5 && grad3 < grad6 && grad3 < grad7 && grad3 < grad8 && grad3 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad3);
    		}
    		else if (grad4 < grad0 && grad4 < grad1 && grad4 < grad2 && grad4 < grad3 && grad4 < grad5 && grad4 < grad6 && grad4 < grad7 && grad4 < grad8 && grad4 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad4);
    		}
    		else if (grad5 < grad0 && grad5 < grad1 && grad5 < grad2 && grad5 < grad3 && grad5 < grad4 && grad5 < grad6 && grad5 < grad7 && grad5 < grad8 && grad5 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad5);
    		}
    		else if (grad6 < grad0 && grad6 < grad1 && grad6 < grad2 && grad6 < grad3 && grad6 < grad4 && grad6 < grad5 && grad6 < grad7 && grad6 < grad8 && grad6 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad6);
    		}
    		else if (grad7 < grad0 && grad7 < grad1 && grad7 < grad2 && grad7 < grad3 && grad7 < grad4 && grad7 < grad5 && grad7 < grad6 && grad7 < grad8 && grad7 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad7);
    		}
    		else if (grad8 < grad0 && grad8 < grad1 && grad8 < grad2 && grad8 < grad3 && grad8 < grad4 && grad8 < grad5 && grad8 < grad6 && grad8 < grad7 && grad8 < grad9)
    		{
    			printf("The min value of your grades is: %d\n", grad8);
    		}
    		else if(grad9 < grad0 && grad9 < grad1 && grad9 < grad2 && grad9 < grad3 && grad9 < grad4 && grad9 < grad5 && grad9 < grad6 && grad9 < grad7 && grad9 < grad8)
    		{
    			printf("The min value of your grades is: %d\n", grad9);
    		}
    int aver = (grad0+grad1+grad2+grad3+grad4+grad5+grad6+grad7+grad8+grad9)/10;
    	printf("The average of your grades is: %d\n",aver);
    Imagine that the integers have already a value by doing a scanf.

    My problem starts when two or more integers have the same value, the minimum and/or maximum value will not appear.

    How can I solve this problem?

    Thanks for your help and happy Programming!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would start by solving the problem, as opposed to cutting and pasting the same line of code ten times.

    If someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by tabstop View Post
    If someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?
    as tabstop suggests, the real difficulty is coming up with a solution to the problem (i.e. nothing to do with programming!). pretend you are the computer, get a piece of a paper and a pencil (and an eraser!) and try to solve the problem. once you have that, it is quite easy to translate it into code. this is what "programming" really is, and dont get discouraged by learning this new language and its syntax.

    what is the largest of the following three numbers: 1, 4, 3? how did you come up with your answer? well you simply went through the list one by one and compared them. lets start with an initial "max" value of whatever the first number is, 1. how do we determine that 1 < 4 and 4 > 3, thus the max is 4?

    say your requirement was to find the max number, but instead of a list of 10, the list is now 1000, or simply 50. would you continue using your current design?
    Last edited by nadroj; 09-18-2009 at 02:56 PM.

  4. #4
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    as tabstop suggests, the real difficulty is coming up with a solution to the problem (i.e. nothing to do with programming!). pretend you are the computer, get a piece of a paper and a pencil (and an eraser!) and try to solve the problem. once you have that, it is quite easy to translate it into code. this is what "programming" really is, and dont get discouraged by learning this new language and its syntax.

    what is the largest of the following three numbers: 1, 4, 3? how did you come up with your answer? well you simply went through the list one by one and compared them. lets start with an initial "max" value of whatever the first number is, 1. how do we determine that 1 < 4 and 4 > 3, thus the max is 4?

    say your requirement was to find the max number, but instead of a list of 10, the list is now 1000, or simply 50. would you continue using your current design?
    Well with ten numbers is already very complex, so I would find another design.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by georgio777 View Post
    Well with ten numbers is already very complex, so I would find another design.
    good answer

  6. #6
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Well I was thinking in nested statements, but in this case I have no idea how to start.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nested statements is not thinking about the problem. That's not related to finding the answer, that doesn't help you finding the answer, and if you think about the problem you'll realize that.

  8. #8
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Ok, if someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?

    As common sense I would compare all the numbers and simply tell the one that is the biggest.

    Am I right?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by georgio777 View Post
    Ok, if someone showed you ten numbers, one at a time, and then asked you which is largest, how would you answer the question?

    As common sense I would compare all the numbers and simply tell the one that is the biggest.

    Am I right?
    And how would you compare all the numbers? You can't see them all at one time....

  10. #10
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by tabstop View Post
    And how would you compare all the numbers? You can't see them all at one time....
    Well, If I am smart enough I will pay more attention to the biggest number as the person is telling me the numbers.

    For example is he starts telling 5, 10, 6, 8, 2; I would care about the second one since is the biggest by that moment, if he continues telling number like 5, 15, 11; I would change from 10 to 15 since is now the biggest.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by georgio777 View Post
    Well, If I am smart enough I will pay more attention to the biggest number as the person is telling me the numbers.

    For example is he starts telling 5, 10, 6, 8, 2; I would care about the second one since is the biggest by that moment, if he continues telling number like 5, 15, 11; I would change from 10 to 15 since is now the biggest.
    exactly. for the problem of finding a max (or min) of an unsorted list, which is what your problem is, there really isnt anything special going on. can you translate what you propose the algorithm is into "pseudocode"? that is, simply english statements which mimic the way an algorithm is written? maybe start with:
    Code:
    - create a list (array) with the following numbers: 5, 10, 6, 8, 2
    - declare a variable "max" and assign it the first value in the list
    - loop over all remaining numbers in the list:
      - if current number ....
        - (do something?)
    hope it helps

  12. #12
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by nadroj View Post
    exactly. for the problem of finding a max (or min) of an unsorted list, which is what your problem is, there really isnt anything special going on. can you translate what you propose the algorithm is into "pseudocode"? that is, simply english statements which mimic the way an algorithm is written? maybe start with:
    Code:
    - create a list (array) with the following numbers: 5, 10, 6, 8, 2
    - declare a variable "max" and assign it the first value in the list
    - loop over all remaining numbers in the list:
      - if current number ....
        - (do something?)
    hope it helps
    Ok, well here is the problem, we haven't seen arrays nether loops still. That's the reason I am concerned and a little bit confused.

    Is there is a way to do it without loops nor arrays?
    Last edited by georgio777; 09-18-2009 at 03:24 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, so you have an idea right there: you need to keep track of the currently known largest number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    Talking Finally I finished this assigment!

    After some researching and after looking at a similar program code that I founded here. I finally changed the whole C code into arrays and loops. Making less complex and easy to understand!

    Wow I learned a lot about arrays today!

    Thanks everyone for your help!

    I attached the .c file in case you wanted to see it.

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Statistical Mode Function
    By h4rrison.james in forum C Programming
    Replies: 5
    Last Post: 01-16-2009, 09:48 PM
  3. Find max profit among input salary
    By hlam in forum C Programming
    Replies: 8
    Last Post: 11-16-2008, 10:30 AM
  4. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 05:14 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM