# Program displaying random number from finding the max of an array

• 02-08-2009
kisiellll
Program displaying random number from finding the max of an array
Ok so I am trying to find the maximum number from an array in my program in it is showing some random very large number instead of the one that should be showing.

Heres the code:

Code:

```#include <stdio.h> main() {         /* Variable Declarations */         int x = 0, grades[100], maximum, minimum, average, sum, numGrades, temp;         char changed = 'T';                 /* Prompt user to enter the number of grades to process */         printf ("Enter the numer of grades to process (0 - 100): ");         scanf ("%i", &numGrades);                 /* Check to make sure number of grades entered is between 0 - 100 */         while (numGrades < 0 || numGrades > 100)         {                 printf ("*** Invalid number of grades. Please enter 0 - 100. ***\n");                 printf ("Enter the numer of grades to process (0 - 100): ");                 scanf ("%i", &numGrades);         } /* end while */                 /* If user enters 0 grades to be entered, end program */         if (numGrades == 0)         {                 return 0;         } /* end if */                 /* Set minimum, maximum, and sum values         to default values for correct ouput */         minimum = grades[0];         maximum = grades[0];         sum = 0;                 /* Prompt user to enter each grade per number of grades entered         using a for loop to continue through the array */         for (x = 0; x < numGrades; x++)         {                 printf ("Enter grade for student #%i: ", x + 1);                 scanf ("%i", &grades[x]);                                 /* Check to make sure each grade is between 0 - 100 */                 while (grades[x] < 0 || grades[x] > 100)                 {                         printf ("*** Invalid grade entered. Please enter 0 - 100. ***\n");                         printf ("Enter grade for student #%i: ", x + 1);                         scanf ("%i", &grades[x]);                 } /* end while */                                 /* Determine minimum grade by comparing to current                 minimum (default set as first grade entered) */                 if (grades[x] < minimum)                 {                         minimum = grades[x];                 } /* end if */                                 /* Determine maximum grade by comparing to current                 maximum (default set as first grade entered) */                 if (grades[x] > maximum)                 {                         maximum = grades[x];                 } /* end if */                                 /* Keep track of sum of grades to calculate average later */                 sum = sum + grades[x];         } /* end for */                 /* Calculate average of grades */         average = sum / numGrades;                 /* Display the minimum, maximum, and class average grades */         printf ("\nThe minimum grade is %i\n", minimum);         printf ("The maximum grade is %i\n", maximum);         printf ("The class average is %i\n", average);                 /* Start while loop to sort array in ascending order         by keeping track if grades place is changed or not */         while (changed == 'T')         {                 changed = 'F';                                 /* Start for loop to check if grades need to be resorted                 if current grade in the array is larger than the next */                 for (x = 0; x < numGrades; x++)                 {                         /* If current grade is larger than the next, swap their places */                         if (grades[x] > grades[x + 1])                         {                                 temp = grades[x];                                 grades[x] = grades[x + 1];                                 grades[x + 1] = temp;                                                                 changed = 'T';                         } /* end if */                 } /* end for */         } /* end while */                 /* Display the grades entered in ascending order */         printf ("The %i Grades entered were:\n", numGrades);                 /* Start for loop to show each grade in order */         for (x = 0; x < numGrades; x++)         {                 printf ("%i ", grades[x]);         } /* end for */                 printf ("\n");                 return 0; } /* end main */```
And the lame output:
Code:

```Enter the numer of grades to process (0 - 100): 2 Enter grade for student #1: 14 Enter grade for student #2: 21 The minimum grade is 14 The maximum grade is 856136 The class average is 17 The 2 Grades entered were: 14 21 Press any key to continue . . .```
notice how the max is 856136 when it should be 21. i cant figure out what the heck is causing this.

any help is appreciated!
• 02-08-2009
You're setting the minimum and maximum grades to grade[0], which is fine - except you haven't given that array location, any value yet.

By good fortune, your program works fine for me - one time. But if you'll set minimum and maximum AFTER you have entered your grades, then it will work fine, all the time.

So: