// quizScores3.cpp
// Illustrates a 2-dimensional of integers.
// Calculates row-averages and column-averages
// for a file containing 5 quiz scores for
// up to 30 students.
// Also calculates curved scores and
// averages of curved scores.
// Needs a 2-dimensional array.
#include "textUtility2.h" // includes <iostream>
#include <string>
#include <fstream>
int main()
{
const int MAX_NUMBER_OF_STUDENTS = 30;
const int NUMBER_OF_QUIZZES = 5;
const char INPUT_FILENAME[] = "quizScores.txt";
const char OUTPUT_FILENAME[] = "quizAveragesCurved.txt";
// Prepare to read from input file:
ifstream inputFile;
inputFile.open(INPUT_FILENAME);
if ( !inputFile ) {
cout << "Could not read file "
<< INPUT_FILENAME << "." << endl;
return 1;
} // if
// Prepare to write to output file:
ofstream outputFile;
outputFile.open(OUTPUT_FILENAME);
if ( !outputFile ) {
cout << "Could not write to file "
<< OUTPUT_FILENAME << "." << endl;
return 1;
} // if
outputFile << "Raw scores and their averages:"
<< endl << endl;
// Define arrays to hold quiz scores (0 to 10),
// the student's ID numbers, and maxima for each quiz:
int scores[MAX_NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES];
string iDs[MAX_NUMBER_OF_STUDENTS];
int maxScores[NUMBER_OF_QUIZZES];
// Lowest possible maximum is zero:
for ( int i = 0; i < NUMBER_OF_QUIZZES; i++ )
maxScores[i] = 0;
// Read scores, calculate raw average for each student,
// and print student ID numbers, scores, and averages
// to output file:
int numberOfStudents = 0; // Number of rows in array
// with assigned values
float totalSum = 0;
inputFile >> iDs[numberOfStudents]; // begin reading FIRST line
while ( inputFile ) {
outputFile << iDs[numberOfStudents] << " ";
int rowSum = 0;
for ( int i = 0; i < NUMBER_OF_QUIZZES; i++ ) {
inputFile >> scores[numberOfStudents][i];
printRightJustified(scores[numberOfStudents][i],
6, outputFile);
rowSum += scores[numberOfStudents][i];
if ( scores[numberOfStudents][i] > maxScores[i] )
maxScores[i] = scores[numberOfStudents][i];
} // for i
float rowAverage = (float) rowSum / NUMBER_OF_QUIZZES;
printRightJustified(rowAverage, 1, 10, outputFile);
outputFile << endl;
totalSum += rowAverage;
numberOfStudents++;
inputFile >> iDs[numberOfStudents]; // begin reading NEXT line
} // while
// Print blank line and suitable spacing in output file:
outputFile << endl << " ";
// Calculate average of all students' scores for
// each individual quiz, and print to output file:
for ( int column = 0; column < NUMBER_OF_QUIZZES; column++ )
{
int columnSum = 0;
for ( int row = 0; row < numberOfStudents; row++ )
columnSum += scores[row][column];
float columnAverage =
(float) columnSum / numberOfStudents;
printRightJustified(columnAverage, 1, 6, outputFile);
} // for column
// Calculate total class average (uncurved):
float average = totalSum / numberOfStudents;
// Print total class average, aligned with column
// containing averages for each student,
// and finish the line:
printRightJustified(average, 1, 8, outputFile);
outputFile << endl;
// Curve the scores:
outputFile << endl
<< "Curved scores and their averages:"
<< endl << endl;
totalSum = 0;
for ( int i = 0; i < numberOfStudents; i++ ) {
outputFile << iDs[i] << " "; // print student ID
int rowSum = 0; // initialize for row average
for ( int j = 0; j < NUMBER_OF_QUIZZES; j++ ) {
scores[i][j] = scores[i][j] + 10 - maxScores[j];
printRightJustified(scores[i][j], 6, outputFile);
rowSum += scores[i][j];
} // for j
float rowAverage = (float) rowSum / NUMBER_OF_QUIZZES;
printRightJustified(rowAverage, 1, 10, outputFile);
outputFile << endl;
totalSum += rowAverage;
} // for i
// Print blank line and suitable spacing in output file:
outputFile << endl << " ";
// Calculate average of all students' curved scores for
// each individual quiz, and print to output file:
for ( int column = 0; column < NUMBER_OF_QUIZZES; column++ ) {
int columnSum = 0;
for ( int row = 0; row < numberOfStudents; row++ )
columnSum += scores[row][column];
float columnAverage =
(float) columnSum / numberOfStudents;
printRightJustified(columnAverage, 1, 6, outputFile);
} // for column
// Calculate total curved class average:
average = totalSum / numberOfStudents;
// Print total class average, aligned with column
// containing averages for each student,
// and finish the line:
printRightJustified(average, 1, 8, outputFile);
outputFile << endl;
cout << "File " << OUTPUT_FILENAME
<< " has been created." << endl;
return 0;
} // function main