Reputation: 177
This is for Java. I just finished parsing each column from String to int. However, now I am having a tough time figuring out how to calculate each column and finding the average.
For example, this is what I have after I parsed each column:
int Q1 = Integer.parseInt(columns[1]);
This is what I tried to find the average, but was unsuccessful.
int Q1s = Q1;
int i;
int total = 0;
for (i = 0; i < 6; i++) {
total = total + Q1s;
}
double avg = total / Q1s;
return avg;
I know how to find the average the normal way (Example: int Q1[] = {1,2,3};) But it is not the same for parsing an array of integers.
Any hints on how to proceed would be greatly appreciated!
P.S. I don't want the answer...I just want a direction on where to go from here. That is why I didn't put the complete code that I currently have.
Upvotes: 0
Views: 339
Reputation: 653
double avg = total / Q1s;
Calculating average is wrong here. It should be
double avg = total / 6;
It should be divided by the total number of elements here you are using 6 as a constant.
Upvotes: 0
Reputation: 5629
Your logic to calculate total
is wrong. You are not adding all the elements, what you're doing is adding the first element n
times.
Assuming Q1
is your array of Strings
.
for(int i = 0; i < Q1.length; ++i)
{
total += Integer.parseInt(Q1[i]);//this is what you want to do. Parse it here
}
double avg = (double) total / Q1.length;
Upvotes: 3
Reputation: 3326
two things:
1) you should use:
double avg = total / <ColumnCount>;
2) you need to casting, So
double avg = ((double)total) / <ColumnCount>;
Upvotes: 0