# Producing a Table with Data from File

• 08-04-2008
Vulpecula
Producing a Table with Data from File
Hi everyone,

I'm currently working on a program that will give me the averages of scores and stores them into a one-dimensional arrray. I'm having trouble in regards to where I should be putting the code that will give me the table for my calculations.
Before or after my calculations?

This is what the table should look like:
http://i33.photobucket.com/albums/d8...00000Table.jpg

So far for the table I have the following:
Code:

```cout << left;         cout << setw(25) << "Global Navigation\n";                 << setw(21) << "Grade Report - Summer 2008\n";                 << setw(12) << "Instructor : C. Columbus    Section No. 0104\n\n";                 << setw (0) << "Name" << setw (20) << "#```
I'm not sure how to continue it. To show #1, #2 etc depending on the amount of scores per student. (As it will not always be 4)
I hope I've made myself clear.
Any help would be appreciated!
• 08-04-2008
tabstop
You certainly won't be able to print the averages until after you have calculated them.

As to printing, if you have to do something repeatedly, that is a good time to use a loop. If you have a fixed number of times to repeat the loop, you can use a for loop for that.
• 08-04-2008
Vulpecula
Yes, I have the code to calculate the average. So far it looks like this:

Code:

```double sum;         for (int row =0; row < i; row++) //For each student, calculate the average score. Average for each row.         {                 sum= 0;                 for (int col =0; col < num; col++)                         sum= sum + Scores[row][col];                 AvgStudentScore[row]= (float) sum/num;                 if (AvgStudentScore [row] > 90)                         Grade[row]= 'A';                 else if (AvgStudentScore [row] > 80)                         Grade[row]= 'B';                 else if (AvgStudentScore [row] > 67.5)                         Grade[row]= 'C';                 else if (AvgStudentScore [row] > 55)                         Grade[row]= 'D';                 else                         Grade[row]= 'F';         }```
I can have 2-7 numbers of scores. This particular example has 4, but I have to write the code so that it is able to some me score #1, #2, #3-#7. Can I used a for loop for this as well?

ALSO, I'm having trouble with this part:
Code:

`AvgStudentScore[row]= (float) sum/num;`
Since I'm supposed to store the average (sum being double and num being int) into a float array (AvgStudentScore[])
How can I convert the calculation to a float?
• 08-04-2008
tabstop
Quote:

Originally Posted by Vulpecula
Yes, I have the code to calculate the average. So far it looks like this:

Code:

```double sum;         for (int row =0; row < i; row++) //For each student, calculate the average score. Average for each row.         {                 sum= 0;                 for (int col =0; col < num; col++)                         sum= sum + Scores[row][col];                 AvgStudentScore[row]= (float) sum/num;                 if (AvgStudentScore [row] > 90)                         Grade[row]= 'A';                 else if (AvgStudentScore [row] > 80)                         Grade[row]= 'B';                 else if (AvgStudentScore [row] > 67.5)                         Grade[row]= 'C';                 else if (AvgStudentScore [row] > 55)                         Grade[row]= 'D';                 else                         Grade[row]= 'F';         }```
I can have 2-7 numbers of scores. This particular example has 4, but I have to write the code so that it is able to some me score #1, #2, #3-#7. Can I used a for loop for this as well?

ALSO, I'm having trouble with this part:
Code:

`AvgStudentScore[row]= (float) sum/num;`
Since I'm supposed to store the average (sum being double and num being int) into a float array (AvgStudentScore[])
How can I convert the calculation to a float?

You're worrying me here. Why, exactly, do you think that you have NOT "writ[ten] the code so that it is able to some me score #1, #2, #3-#7"? (I am assuming that the part I have quoted should say "writ[ten] the code so that it is able to sum up scores #1, #2, and #3-#7 if they exist".)

Also, converting a double value (which you say sum/num is) into a float variable happens automatically for you.
• 08-04-2008
Vulpecula
I'm aware that it should be doing it for me, however, I was receiving an error/warning (don't recall which) that said that it could not be converted. I compiled my program again and it shows zero errors and warnings... what do ya know...

My question still is, where should I be placing the code for the table... this code:
Code:

```cout << left;         cout << setw(25) << "Global Navigation\n";                 << setw(21) << "Grade Report - Summer 2008\n";                 << setw(12) << "Instructor : C. Columbus    Section No. 0104\n\n";                 << setw (0) << "Name" <<;```
Before or after my calculation?
• 08-04-2008
tabstop
You have to print the header before you print any of the rows. Since it appears you are printing the rows inside the for loop, the code would have to go before that.

Edit: Actually I misread that -- no printing inside the for loop. In that case it doesn't make a bit of difference when you print the header, as long as it's before the first row is printed. (I would probably do all the printing at once, but nobody cares, really.)