Thread: Invalid Output of Minimum and Maximum Value in Array

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

    Angry Invalid Output of Minimum and Maximum Value in Array

    Hi,

    OK, so I am trying to find the minimum and maximum number from an array in my program but it is showing invalid numbers instead of the ones that should be showing.

    The program simplifies by finding the max value, minimum value and average of 10 integers with range of 0 and 100.

    Here is the code:
    Code:
    int grades[10], maximum, minimum, average, sum, x;
    		
    		printf ("Please enter your ten course grades in numbers between 0 and 100:\n");
    		
    		sum = 0;
    		
    		for (x = 0; x < 10; x++)
    		{
    			printf ("Enter grade #%i: ", x + 1);
    			scanf ("%i", &grades[x]);
    			
    			maximum = grades[0];
    			minimum = grades[0];
    
    			if(grades[x] == 100)
    				printf("The letter grade is A+\n");
    			if(grades[x] <= 99.9 && grades[x] >= 93.5)
    				printf("The letter grade is A\n");
    			if(grades[x] <= 93.4 && grades[x] >= 90)
    				printf("The letter grade is A-\n");
    			if(grades[x] <= 89.9 && grades[x] >= 86.5)
    				printf("The letter grade is B+\n");
    			if(grades[x] <= 86.4 && grades[x] >= 83.5)
    				printf("The letter grade is B\n");
    			if(grades[x] <= 83.4 && grades[x] >= 80)
    				printf("The letter grade is B-\n");
    			if(grades[x] <= 79.9 && grades[x] >= 76.5)
    				printf("The letter grade is C+\n");
    			if(grades[x] <= 76.4 && grades[x] >= 73.5)
    				printf("The letter grade is C\n");
    			if(grades[x] <= 73.4 && grades[x] >= 70)
    				printf("The letter grade is C-\n");
    			if(grades[x] <= 69.9 && grades[x] >= 66.5)
    				printf("The letter grade is D+\n");
    			if(grades[x] <= 66.4 && grades[x] >= 63.5)
    				printf("The letter grade is D\n");
    			if(grades[x] <= 63.4 && grades[x] >= 60)
    				printf("The letter grade is D-\n");
    			if(grades[x] <= 59.9 && grades[x] >= 0)
    				printf("The letter grade is F\n");
    					
    			if (grades[x] < minimum)
    			{
    				minimum = grades[x];
    			}
    			
    			if (grades[x] > maximum)
    			{
    				maximum = grades[x];
    			}
    			
    			sum = sum + grades[x];
    		}
    		average = sum / 10;
    		
    		printf ("The minimum grade is %i\n", minimum);
    		printf ("The maximum grade is %i\n", maximum);
    		printf ("The average grade is %i\n", average);
    		printf ("\n");
    And here is the output:
    Code:
    Please enter your ten course grades in numbers between 0 and 100:
    Enter grade #1: 50
    The letter grade is F
    Enter grade #2: 37
    The letter grade is F
    Enter grade #3: 89
    The letter grade is B+
    Enter grade #4: 92
    The letter grade is A-
    Enter grade #5: 65
    The letter grade is D
    Enter grade #6: 46
    The letter grade is F
    Enter grade #7: 87
    The letter grade is B+
    Enter grade #8: 67
    The letter grade is D+
    Enter grade #9: 89
    The letter grade is B+
    Enter grade #10: 42
    The letter grade is F
    The minimum grade is 42
    The maximum grade is 50
    The average grade is 66
    You can see in the output that it appears that 42 is the minimum and 50 the maximum which both are incorrect.

    As I am concerned, I think the code ok, so I have no idea why is this happening.

    Please check for any errors.

    Thanks for your help and happy Programming!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You're resetting minimum/maximum to the first element of the array on each iteration of the loop. You can either initialize them inside the loop when the index equals zero, or else set them to extremely low/high values (eg: INT_MIN/INT_MAX) before the loop.

  3. #3
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by Sebastiani View Post
    You're resetting minimum/maximum to the first element of the array on each iteration of the loop. You can either initialize them inside the loop when the index equals zero, or else set them to extremely low/high values (eg: INT_MIN/INT_MAX) before the loop.
    How I do that?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    How you do what?

  5. #5
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by Sebastiani View Post
    How you do what?
    How do I insert them inside the loop (between which lines of the code).

    Sorry I'm new at this.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You can leave the assignment where it is - just wrap it in a conditional statement, eg: If 'x' is non-zero then the values don't get assigned.

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

    Unhappy

    Quote Originally Posted by Sebastiani View Post
    You can leave the assignment where it is - just wrap it in a conditional statement, eg: If 'x' is non-zero then the values don't get assigned.
    Sorry for my misunderstanding but still I don't get it. I started with C programming two weeks ago, so I still don't get many programming-related definitions.

    As I understand the conditional statement is the "if, if else" statement, but my question is what exactly I need to wrap.

    Sorry for the inconvenience.


    If you can edit the code will be better.
    Last edited by georgio777; 09-19-2009 at 02:29 AM.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Right before (as in immediately prior to the point where) you assign a value to min/max, check the value of 'x'. If it is zero, you know that min/max are uninitialized, so you need to initialize them (technically to 'x'th element, but effectively to the 0th). In other words, just wrap the existing assignment into an 'if' statement.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by georgio777
    If you can edit the code will be better.
    Or better yet, just give it a try yourself. There really isn't much to it. Give it a whirl and then post the updated code showing your attempt to do as much, and we'll see how far off it is.

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

    Talking

    Ok, I tried what you said and it worked! Still I don't have any idea / understand why this change make the code work :P

    Here is the code:
    Code:
    int grades[10], maximum, minimum, average, sum, x;
    		
    		printf ("Please enter your ten course grades in numbers between 0 and 100:\n");
    		
    		sum = 0;
    		
    		for (x = 0; x < 10; x++)
    		{
    			printf ("Enter grade #%i: ", x + 1);
    			scanf ("%i", &grades[x]);
    			
    			if(x == 0){
    			maximum = grades[0];
    			minimum = grades[0];
    			}
    			
    			if(grades[x] == 100)
    				printf("The letter grade is A+\n");
    			if(grades[x] <= 99.9 && grades[x] >= 93.5)
    				printf("The letter grade is A\n");
    			if(grades[x] <= 93.4 && grades[x] >= 90)
    				printf("The letter grade is A-\n");
    			if(grades[x] <= 89.9 && grades[x] >= 86.5)
    				printf("The letter grade is B+\n");
    			if(grades[x] <= 86.4 && grades[x] >= 83.5)
    				printf("The letter grade is B\n");
    			if(grades[x] <= 83.4 && grades[x] >= 80)
    				printf("The letter grade is B-\n");
    			if(grades[x] <= 79.9 && grades[x] >= 76.5)
    				printf("The letter grade is C+\n");
    			if(grades[x] <= 76.4 && grades[x] >= 73.5)
    				printf("The letter grade is C\n");
    			if(grades[x] <= 73.4 && grades[x] >= 70)
    				printf("The letter grade is C-\n");
    			if(grades[x] <= 69.9 && grades[x] >= 66.5)
    				printf("The letter grade is D+\n");
    			if(grades[x] <= 66.4 && grades[x] >= 63.5)
    				printf("The letter grade is D\n");
    			if(grades[x] <= 63.4 && grades[x] >= 60)
    				printf("The letter grade is D-\n");
    			if(grades[x] <= 59.9 && grades[x] >= 0)
    				printf("The letter grade is F\n");
    					
    			if (grades[x] < minimum)
    			{
    				minimum = grades[x];
    			}
    			
    			if (grades[x] > maximum)
    			{
    				maximum = grades[x];
    			}
    			
    			sum = sum + grades[x];
    		}
    		average = sum / 10;
    		
    		printf ("The minimum grade is %i\n", minimum);
    		printf ("The maximum grade is %i\n", maximum);
    		printf ("The average grade is %i\n", average);
    		printf ("\n");
    Thank you, I appreciate that!

    ____________________________

    I need to practice my "pseudocoding"

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well done. See, I just knew you could do it.

    Anyway, I would recommend getting aquainted with a good C programming text. I think that will help clarify a lot of your confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. minimum numbers to be entered into an array
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2003, 06:19 PM
  5. Replies: 2
    Last Post: 10-31-2002, 07:27 PM