# Invalid Output of Minimum and Maximum Value in Array

• 09-19-2009
georgio777
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.

Thanks for your help and happy Programming!
• 09-19-2009
Sebastiani
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.
• 09-19-2009
georgio777
Quote:

Originally Posted by Sebastiani
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?
• 09-19-2009
Sebastiani
How you do what?
• 09-19-2009
georgio777
Quote:

Originally Posted by Sebastiani
How you do what?

How do I insert them inside the loop (between which lines of the code).

Sorry I'm new at this.
• 09-19-2009
Sebastiani
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.
• 09-19-2009
georgio777
Quote:

Originally Posted by Sebastiani
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.
• 09-19-2009
Sebastiani
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.
• 09-19-2009
Sebastiani
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.
• 09-19-2009
georgio777
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"
• 09-19-2009
Sebastiani
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.