Doubt about max, minimum and average value with if function

This is a discussion on Doubt about max, minimum and average value with if function within the C Programming forums, part of the General Programming Boards category; Hi, I started this semester with C programming and one of the assignments asks to find the max value, minimum ...

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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  5. #5
    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?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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....

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

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

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Yes, so you have an idea right there: you need to keep track of the currently known largest number.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

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

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

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    thats quite alright. as i mentioned above, the real difficulty is finding a solution to the problem, and not the part of inputting that into the computer in some programming language. if you were writing a book, what would be more difficult (mentally, not wrist strain!): coming up with the storyline/plot and its characters, etc, or writing those thoughts you have come up with on a piece of paper?

    just think of a list/array as a bunch of "things". in this case, those "things" are numbers, more precisely they are integers. think of a loop as something that goes through a bunch of things (i.e. a list/array) and does something. in your case, you want to go through the list and compare the numbers in the list. using "for" loops you can easily go through a list, and have a variable that points to which thing in the list you're currently working on (i.e., an "index" in that list). heres an example of printing 10 numbers:
    Code:
    int[] myList = {5, 8, 10, 11, 4};
    int i; // this is our "counter", which is used to keep track of where we are while looping through the above list of 5 things/numbers/integers
    for ( i = 0; i < 5; i++ )
    {
       printf ("number %d is: %d\n" , i, list[i] );
    }
    the output would be:
    Code:
    number 0 is: 5
    number 1 is: 8
    number 2 is: 10
    number 3 is: 11
    number 4 is: 4
    but anyways, im sure you must have some notes on looping to refer to.

    the point is to come up with something on paper, disregarding the syntax of the "C" language, then translate that to C code. so try and work on the first part.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by georgio777 View Post
    Is there is a way to do it without loops nor arrays?
    yes, this is what you were trying to do in your initial code. however, you can see that if you changed the problem to finding max of 1000, instead of 10, numbers, your code grows exponentially or unreasonably large and not maintainable/readable. in order to make it generic (i.e. your algorithm works with a list of 1 number, 5 numbers, and 1,000,000 numbers), you use an array and a loop (otherwise you are hard coding, as you have already tried).

    input: array of N integers
    logic: loop over all N numbers and store the largest into "max" (setting an initial "max", you have to start somewhere!)
    output: "max"

    just go through the primitive steps you do yourself when finding the max of this list: 1, 4, 3, 5. a computer is actually very "dumb", and you must tell it exactly what to do. pretend you are writing instructions on how to teach a young child how to find a maximum number from the above list. ("is 1 greater than 4?", etc).

    lets see your attempt!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  2. Statistical Mode Function
    By h4rrison.james in forum C Programming
    Replies: 5
    Last Post: 01-16-2009, 08:48 PM
  3. Find max profit among input salary
    By hlam in forum C Programming
    Replies: 8
    Last Post: 11-16-2008, 09: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

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