944,092 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3551
  • Java RSS
Nov 14th, 2007
0

Accumulator

Expand Post »
I'm supposed to be taking all the averages of the students grades and create an overall average...but it's not working

import java.io.*;
import java.util.*;

public class Grade
{
public static void main(String[] args) throws IOException
{

String []name = new String[50];

int num1, num2, num3;

Scanner inFile =
new Scanner(new FileReader("Average.txt"));

int []average = new int[50];

double sum;

int c = 0;

while(inFile.hasNext())
{
name[c] = inFile.next();
num1 = inFile.nextInt();
num2 = inFile.nextInt();
num3 = inFile.nextInt();

average[c]=((num1 + num2 + num3)/3);

c++;
}

for (int i = 0; i < c; i++)
{ System.out.println(name[i] + " " + average[i]);
}

for (int i = 0; i < average.length; i++)
{
sum += average[i] * (c));
}



}
}

it's the sum that isn't working...any ideas?...sorry to be so vague
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cat8882 is offline Offline
6 posts
since Oct 2007
Nov 14th, 2007
0

Re: Accumulator

For one, you have an extra right parenthesis after the "c". Why are you multiplying by each average by c though?
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 14th, 2007
0

Re: Accumulator

Um C is how many people are in the class...like there is the average of each person and then the class average

actually I'm not complete sure what I'm doing with that but it makes sense I think
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cat8882 is offline Offline
6 posts
since Oct 2007
Nov 14th, 2007
0

Re: Accumulator

basically all of this

for (int i = 0; i < average.length; i++)
{
sum += average[i] * (c));
}

could be changed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cat8882 is offline Offline
6 posts
since Oct 2007
Nov 15th, 2007
0

Re: Accumulator

I think what you probably need to do is sum all of the students' scores and then divide by the number of students. The average should be either a float or a double. Since all the scores are presumably int's and the number of students is an int. In Java an int/int calculation equals an int, so you will need to perform a cast.

double average = (double) sum / numStudents;
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Nov 15th, 2007
0

Re: Accumulator

Yes, average[] could be made a double unless you really need it to be the int value. A cast on the final calc isn't needed though because "sum" is already a double and the double/int result will still be a double.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 15th, 2007
0

Re: Accumulator

I need all the averages I collected in the array to be added together and divided by the number of students (c) to equal the sum. I just don't know how to write it out.

The problem isn't the logic it's just how to do it...whenever I type in something like sum += average[i] / c it says sum may not be initialized...I don't know how to put it.
Last edited by cat8882; Nov 15th, 2007 at 4:57 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cat8882 is offline Offline
6 posts
since Oct 2007
Nov 15th, 2007
0

Re: Accumulator

Initialize sum to 0 when you declare it. The average is just
Java Syntax (Toggle Plain Text)
  1. for (int i = 0; i < average.length; i++)
  2. {
  3. sum += average[i];
  4. }
  5. double overallAvg = sum / c;
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Testing something with classes
Next Thread in Java Forum Timeline: Sorting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC