Program displaying random number from finding the max of an array

This is a discussion on Program displaying random number from finding the max of an array within the C Programming forums, part of the General Programming Boards category; Ok so I am trying to find the maximum number from an array in my program in it is showing ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    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!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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:

    1) Enter your grades

    2) Set your minimum and maximum var's

    3) Do the rest of your computation

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    ahhhhh you know i was thinking about that all along but was too lazy to just move the code.

    thank you sir!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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