# summation of a Row

Page 1 of 1

## 5 Replies - 924 Views - Last Post: 16 November 2009 - 11:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=139263&amp;s=ee7cf587f0ecbd5c2e8e983293bcd096&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gemoney

• D.I.C Head

Reputation: 0
• Posts: 112
• Joined: 30-October 09

# summation of a Row

Posted 15 November 2009 - 03:15 PM

I need help getting and average of this data going downw, I aleady got the average going across.....Is there a way of reading the data from notepad without actually putting the #s in an array? I used this code going across
Students T1 T2 T3 T4 T5
Jonnson 85 83 77 91 76
Aniston 80 90 95 93 48
cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kenndy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

```int [] score = new int[]{score1, score2, score3, score4, score5};
double sumavg =0;
int sum=0;

for(int i=0; i < score.length; i++)
{
sum +=score[i];
sumavg = sum/score.length;
Avg = (int)Math.round(sumavg);
}
```

It isn't given me the right avg going down.
thanx

Is This A Good Question/Topic? 0

## Replies To: summation of a Row

### #2 sakshamkum

• D.I.C Head

Reputation: 19
• Posts: 232
• Joined: 09-June 09

## Re: summation of a Row

Posted 15 November 2009 - 03:22 PM

can you specify what are score1,score2,score3,score4,score5
Was This Post Helpful? 0

### #3 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: summation of a Row

Posted 15 November 2009 - 03:26 PM

1. Open up the file (FileReader, or w/e construct you want to use)
2. Have a "total" variable that is initialized to zero and it summed to the next() call or equivalent
3. print it

Let me know if you want an explicit example
Was This Post Helpful? 0

### #4 gemoney

• D.I.C Head

Reputation: 0
• Posts: 112
• Joined: 30-October 09

## Re: summation of a Row

Posted 15 November 2009 - 07:47 PM

sakshamkum, on 15 Nov, 2009 - 02:22 PM, said:

can you specify what are score1,score2,score3,score4,score5

score1 through score5 are the five testscores for each student which are the same as T1 thru T5 just different variable.
```while (inFile.hasNext())
{
lastName = inFile.next();
score1 = inFile.nextInt();
score2 = inFile.nextInt();
score3 = inFile.nextInt();
score4 = inFile.nextInt();
score5 = inFile.nextInt();

//This is getting the Avg going across
int [] score = new int[]{score1, score2, score3, score4, score5};
double sumavg =0;
int sum=0;

for(int i=0; i < score.length; i++)
{
sum +=score[i];
sumavg = sum/score.length;
Avg = (int)Math.round(sumavg);
}

//I like this one to get the Avg only on Score1 [T1] only
//going down inoder to get the class Avg, there are 10 students that took
//each  test now what is the CLASS Avg
int[] scoreA = new int[]{score1};

for(int i=0; i < scoreA.length; i++)
{
sum +=scoreA[i];
sumavg = sum/scoreA.length;
AvgScore1 = (int)Math.round(sumavg);
}

System.out.println(lastName+'\t'+'\t'+score1+'\t'+score2+'\t'+score3
+'\t'+score4+'\t'+score5+'\t'+Avg);

}
System.out.println(AvgScore1);

inFile.close();
outFile.close();
}
```

I could do this to easily get the class avg for ScoreA but I wanna have to enter the numbers
``` int[] scoreA = new int[]{85, 80, 78, 92, 23, 60, 77,93, 79, 85};

for(int i=0; i < scoreA.length; i++)
{
sum +=scoreA[i];
sumavg = sum/scoreA.length;
AvgScore1 = (int)Math.round(sumavg);
```

This post has been edited by gemoney: 15 November 2009 - 08:00 PM

Was This Post Helpful? 0

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8363
• Posts: 31,955
• Joined: 06-March 08

## Re: summation of a Row

Posted 15 November 2009 - 07:59 PM

sumavg = sum/score.length;
Avg = (int)Math.round(sumavg);

this should be done outside the loop
Was This Post Helpful? 0

### #6 gemoney

• D.I.C Head

Reputation: 0
• Posts: 112
• Joined: 30-October 09

## Re: summation of a Row

Posted 16 November 2009 - 11:09 AM

pbl, on 15 Nov, 2009 - 06:59 PM, said:

sumavg = sum/score.length;
Avg = (int)Math.round(sumavg);

this should be done outside the loop

I know that, but it isn't workin 4 me outside the loop. Inside the loop works so far.
http://www.4freeimagehost.com/show.php?i=a9648738abfa.png
I need help putting this is codes in method format.Here is the code
```public class StudentTestGrade
{
public static void main (String[] args)
throws FileNotFoundException
{
String lastName="";
int score1 = 0;
int score2 = 0;
int score3 = 0;
int score4 = 0;
int score5 = 0;
int Avg =0;
char letter;

int AvgScore1=0;int AvgScore2=0;int AvgScore3=0;int AvgScore4=0;int AvgScore5=0;
int sumA=0;int sumB=0;int sumC=0;int sumD=0;int sumE=0;
double sumavgA=0;double sumavgB=0;double sumavgC=0;double sumavgD=0;double sumavgE=0;

Scanner inFile =
new Scanner(new FileReader("c:\\grades.txt"));

PrintWriter outFile =
new PrintWriter("c:\\testavg.out");

System.out.println("Student"+'\t'+'\t'+"Test1"+'\t'+"Test2"+'\t'+"Test3"
+'\t'+"Test4"+'\t'+"Test5"+'\t'+"Average"+'\t'+"Grade");

while (inFile.hasNext())
{

lastName = inFile.next();
score1 = inFile.nextInt();
score2 = inFile.nextInt();
score3 = inFile.nextInt();
score4 = inFile.nextInt();
score5 = inFile.nextInt();

//THIS CODE WORKS FINE HERE, BUT HAVE TO BE IN A METHOD.
//This is getting the Avg going across
int [] score = new int[]{score1, score2, score3, score4, score5};
double sumavg =0;
int sum=0;

for(int i=0; i < score.length; i++)
{
sum +=score[i];
sumavg = sum/score.length;
Avg = (int)Math.round(sumavg);
}

System.out.println(lastName+'\t'+'\t'+" "+score1+'\t'+" "+score2+'\t'+" "+score3
+'\t'+" "+score4+'\t'+" "+score5+'\t'+" "+" "+Avg);

int[] scoreA = new int[]{score1};
for(int i=0; i < scoreA.length; i++)
{
sumA +=scoreA[i];
sumavgA = sumA/10;
AvgScore1 = (int)Math.round(sumavgA);
}

int[] scoreB = new int[]{score2};

for(int i=0; i < scoreB.length; i++)
{
sumB +=scoreB[i];
sumavgB = sumB/10;
AvgScore2 = (int)Math.round(sumavgB);
}

int[] scoreC = new int[]{score3};

for(int i=0; i < scoreC.length; i++)
{
sumC +=scoreC[i];
sumavgC = sumC/10;
AvgScore3 = (int)Math.round(sumavgC);
}

int[] scoreD = new int[]{score4};

for(int i=0; i < scoreD.length; i++)
{
sumD +=scoreD[i];
sumavgD = sumD/10;
AvgScore4 = (int)Math.round(sumavgD);
}

int[] scoreE = new int[]{score5};

for(int i=0; i < scoreE.length; i++)
{
sumE +=scoreE[i];
sumavgE = sumE/10;
AvgScore5 = (int)Math.round(sumavgE);
}
// calculateGrade(Avg);

}

//calculateAverage(score1, score2, score3, score4, score5);
System.out.println(" ");
System.out.println("Class Average"+"\t"+" "+AvgScore1+"\t"+" "+AvgScore2
+"\t"+" "+AvgScore3	+"\t"+" "+AvgScore4+"\t"+" "+AvgScore5);

inFile.close();
outFile.close();
}
```

The first average is what I need to put in a method....but it Isn't pass value... so how can I correct this method?
```public static int calculateAverage(int score1, int score2, int score3, int score4, int score5)
{
int [] score = new int[]{score1, score2, score3, score4, score5};
double sumavg =0;
int sum=0;
int Avg=0;

for(int i=0; i < score.length; i++)
{
sum +=score[i];
sumavg = sum/score.length;
Avg = (int)Math.round(sumavg);
System.out.print(sum+" ");
}
return Avg;
}
```

I am not sure about passing the values while in array format.
imade url:http://www.4freeimagehost.com/show.php?i=a9648738abfa.png

This post has been edited by gemoney: 16 November 2009 - 11:11 AM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }