1,105,334 Community Members

average of an array

Member Avatar
Bowsan22
Newbie Poster
4 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So ive been trying to get an average of this array needed, and I seem to be having trouble with it. So far, my current code just adds the numbers up, but doesnt really average them out at all:

import java.util.Scanner;
import java.util.Arrays;

public class Grades {
	
	public static void main (String[] args)
	{
		double result = 0;
		Scanner scan = new Scanner(System.in);
		
		float[] grades = new float[100];
		
		for (int i = 0; i < grades.length; i++) {
			System.out.print("Enter a grade between 0.0 - 100.0 (enter -1 to quit): ");
			grades[i] = scan.nextFloat();
			if(grades[i] == -1)break;
			result = result + grades[i];
	
		}
		System.out.println("Average of Grades: " + result / grades.length);
		
}
}

and the result is as follows:

Enter a grade between 0.0 - 100.0 (enter -1 to quit): 30.0
Enter a grade between 0.0 - 100.0 (enter -1 to quit): 50.0
Enter a grade between 0.0 - 100.0 (enter -1 to quit): 60.0
Enter a grade between 0.0 - 100.0 (enter -1 to quit): -1
Average of Grades: 1.4

any help would be appreciated.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

You are dividing by the length of the grades array, but not all of those elements have a value. You need to divide by the actual number of entries the user has added.

Member Avatar
Bowsan22
Newbie Poster
4 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How would I go about doing that? (sorry, noobie programmer)

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Seems like counting them might be useful?

Member Avatar
Bowsan22
Newbie Poster
4 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I mean is there a way to count the number of grades entered? I can enter however many I want up to 100, but im clueless on how to get it to see the amount of grades that were entered.

Member Avatar
hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 164 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
1
 

30 + 50 + 60 = 140
140 / 100 (the number of elements in your array) = 1.4
Keep track of how many grades are being entered.
Line 12: int count = 0;
Line 18: count++;
Line 20: result / count

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

> im clueless on how to get it to see the amount of grades that were entered
Seems like a fairly simple task for a variable.

Edit: Ah, well so much for letting you see the solution for yourself, since hfx642 has decided to just hand you a fix.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Define an int as a counter, then add one to a counter every time a new number is entered. Look at about line 16 to do this

Question Answered as of 2 Years Ago by Ezzaral, NormR1 and hfx642
Member Avatar
Xinen
Newbie Poster
12 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
    double[] grades = new double[100];

        System.out.print("Enter a grade between 0.0 - 100.0 (enter -1 to quit): ");
    System.out.println();

    double sum = 0;
    double ave = 0;


    for (int i = 0; i < grades.length; i++)
    {           grades[i] = input.nextDouble();




        if(grades[i] == -1)
        {
            break;
        }

        sum = sum + grades[i];
        ave = sum / (i+1);
    }

    System.out.println("The Average the of Grades: " + ave);
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: