Accumulator

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 6
Reputation: cat8882 is an unknown quantity at this point 
Solved Threads: 0
cat8882's Avatar
cat8882 cat8882 is offline Offline
Newbie Poster

Accumulator

 
0
  #1
Nov 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Accumulator

 
0
  #2
Nov 14th, 2007
For one, you have an extra right parenthesis after the "c". Why are you multiplying by each average by c though?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: cat8882 is an unknown quantity at this point 
Solved Threads: 0
cat8882's Avatar
cat8882 cat8882 is offline Offline
Newbie Poster

Re: Accumulator

 
0
  #3
Nov 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: cat8882 is an unknown quantity at this point 
Solved Threads: 0
cat8882's Avatar
cat8882 cat8882 is offline Offline
Newbie Poster

Re: Accumulator

 
0
  #4
Nov 14th, 2007
basically all of this

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

could be changed
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 798
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Accumulator

 
0
  #5
Nov 15th, 2007
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;
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Accumulator

 
0
  #6
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: cat8882 is an unknown quantity at this point 
Solved Threads: 0
cat8882's Avatar
cat8882 cat8882 is offline Offline
Newbie Poster

Re: Accumulator

 
0
  #7
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Accumulator

 
0
  #8
Nov 15th, 2007
Initialize sum to 0 when you declare it. The average is just
  1. for (int i = 0; i < average.length; i++)
  2. {
  3. sum += average[i];
  4. }
  5. double overallAvg = sum / c;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC