# Thread: Invalid Output of Minimum and Maximum Value in Array

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

sum = 0;

for (x = 0; x < 10; x++)
{
printf ("Enter grade #%i: ", x + 1);

{
}

{
}

}
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:
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!

2. 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. 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?

4. How you do what?

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

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

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

sum = 0;

for (x = 0; x < 10; x++)
{
printf ("Enter grade #%i: ", x + 1);

if(x == 0){
}

{
}

{
}

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