Thread: Producing a Table with Data from File

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10

    Exclamation 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!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10
    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?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Vulpecula View Post
    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.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10
    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?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

Tags for this Thread