Anda di halaman 1dari 5

//preprocessor directives & headers.

#include <iostream> //utilized to instate basic I/O functionality.


#include <iomanip> //Required to use various useful manipulators.
#include <string> //Required for input/output file processing.
#include <fstream> //Required for input/output file processing.

//Standard library.
using namespace std; //Allows the use of cout & endl without "std::" prefix.

//Global constant variables.


const int NUMBER_OF_STUDENTS = 10; //Global constant integer which stores the number
of students.
const int NUMBER_OF_TESTS = 5; //Global constant integer which stores the number of
tests for each individual.

//Defining I/O file streams.


ifstream infile; //Declare input file stream variable.
ofstream outfile; //Declare output file stream variable.

//Function Prototypes
void read(string names[], int numOfNames, double scores[][NUMBER_OF_TESTS]);
// This functions purpose is to read/save the data from the input file. It
// passes a string array of student names, an integer of the number of names,
// and a two-dimensional array, including each students indidual test scores.

void calculate(double NumGrades[], double scores[][NUMBER_OF_TESTS], int rowSize, char


letterGrades[]);
// This functions purpose is to calculate the average test score and the respective
number/letter
// grade for each student. It passes an array of students average grade as a number, a
two-
// dimensional array which carries each students individual test scores, an integer which
// states the size of the rows, and an array which carries each students letter grade.

void print(double numGrades[], string names[], int numOfNames, char letterGrades[]);


// This functions purpose is to print the data stored in the passed variables in an
organized manner.
// It passes an array of students average grade as a number, a string array of student
names, an
// integer which states the number of names, and an array which carries each students
letter grade.

void bubbleSort(double numGrades[], string names[], int numOfNames, char letterGrades[]);


// This functions purpose is to "bubble" sort the passed data in alphabetical order. It
passes
// an array of students average grade as a number, a string array of student namesan
integer
// which states the number of names, and an array which carries each students letter
grade.

//Function 'main'
int main(){ //Start of function 'main'
//declaring local variables.
string names[NUMBER_OF_STUDENTS]; //String array labled 'names' which
holds each students name. Its size = 'NUMBER_OF_Students' or '10'
double scores[NUMBER_OF_STUDENTS][NUMBER_OF_TESTS]; //Two-dimensional array of
each students individual test scores. Its size = 'NUMBER_OF_Students' by
'NUMBER_OF_TESTS'
double numGrades[NUMBER_OF_STUDENTS]; //Array which stores an average
test number grade for each student. Its size = 'NUMBER_OF_Students' or '10'
char letterGrades[NUMBER_OF_STUDENTS]; //Character array which stores a letter
grade for each student. Its size = 'NUMBER_OF_Students' or '10'

//opens input and output files.


infile.open("input.txt"); //opens input file.
outfile.open("output.txt"); //creates & opens output file.

//outputs user-friendly program introduction to the console.


cout << "Hi there... This program calculates students’ average test scores\n"
<< "and their grades. It then sorts students’ names so that students’\n"
<< "data is outputted in ascending order according to their name.\n"
<< "..." << endl;

read(names, 10, scores); //Reads and archives data from input file.
calculate(numGrades, scores, 10, letterGrades); //Calulates the average test scores and
letter grade.

outfile << "Before Sorting: " << endl; //Prints parenthasized text to output file.
print(numGrades, names, 10, letterGrades); //Prints the data stored in the passed
variables in an organized manner to the output file.

bubbleSort(numGrades, names, 10, letterGrades); //Sorts the data in alphabetical order


and updates the passed variables.

outfile << "\nAfter Sorting in Ascending order:" << endl; //Prints parenthasized text to
output file.
print(numGrades, names, 10, letterGrades); //Prints the updated data in an
organized manner to the output file.

//closes the input and output files.


infile.close(); //closes input file.
outfile.close(); //closes output file.

//user friendly message that directs the user to observe the manipulated data.
cout << "data processed! Open the output file to the view the results!" << endl;
cout << "-------------------------------------------------------------" << endl;

return 0; //Returns value '0' & exits function main().


} //End of function main

//function 'read'
void read(string names[], int numOfNames, double scores[][NUMBER_OF_TESTS]){ //start of
function 'read'
//declaring local variables.
int nameCounter = 0; //Initializes integer named 'nameCounter' as 0.
int scoreCounter = 0; //Initializes integer named 'scoreCounter' as 0.

while(nameCounter < numOfNames){ //start of while loop.


infile >> names[nameCounter];

while(scoreCounter < NUMBER_OF_TESTS){ //start of nested-while loop.


infile >> scores[nameCounter][scoreCounter];
scoreCounter++;
} //End of nested-while loop.

scoreCounter = 0;
nameCounter++;
} //End of while loop.
} //End of function 'read'

//function 'calculate'
void calculate(double numGrades[], double scores[][NUMBER_OF_TESTS], int rowSize, char
letterGrades[]){ //start of function 'calculate'

//declaring local variables.


int counter = 0; //Initializes integer named 'counter' as 0.
int num = 0; //Initializes integer named 'num' as 0.
int averageInt = 0; //Initializes integer named 'averageInt' as 0.
double total = 0; //Initializes double datatype named 'total' as 0.
double average = 0; //Initializes double datatype named 'average' as 0.

while(counter < rowSize){ //Start of while loop.

while(num < NUMBER_OF_TESTS){ //Start of nested-while loop.


total = total + scores[counter][num];
num++;
} //End of nested-while loop.

average = (total/NUMBER_OF_TESTS);
numGrades[counter] = average;
averageInt = average + .5;

//Switch statement determines letter grade.


switch(averageInt/10){ //Start of nested-switch statement.
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
letterGrades[counter] = 'F';
break;
case 6:
letterGrades[counter] = 'D';
break;
case 7:
letterGrades[counter] = 'C';
break;
case 8:
letterGrades[counter] = 'B';
break;
case 9:
case 10:
letterGrades[counter] = 'A';
break;
default:
cout << "something doesn't add up! review & update test scores";
} //End of nested-switch statement.

total = 0;
average = 0;
averageInt = 0;
num = 0;
counter++;
} //End of while loop.
} //End of function 'calculate'

//Function 'print'
void print(double numGrades[], string names[], int rowSize, char letterGrades[]){ //start of
function 'print'

//Declaring local variables.


int counter = 0; //Initializes integer named 'counter' as 0.

//output formatting.
outfile << left << setw(10) << "Name";
outfile << right << setw(11) << "Grade";
outfile << right << setw(20) << "Letter Grade" << endl;
outfile << "-----------------------------------------" << endl;

while(counter < rowSize){ //Start of while loop.


outfile << left << setw(10) << names[counter];
outfile << setprecision(2) << fixed << showpoint << right << setw(10) <<
numGrades[counter] << " %";
outfile << right << setw(13) << letterGrades[counter] << endl;
counter++;
} //End of while loop.
} //End of function 'print'

//Function 'bubbleSort'
void bubbleSort(double numGrades[], string names[], int rowSize, char letterGrades[]){
//start of function 'bubbleSort'

//Declaring local variables.


string tempName; //Initializes string named 'tempName' as a temporary place holder
for updated name.
double tempGrade; //Initializes double datatype named 'tempGrade' as a temporary
place holder for updated number grade.
char tempLetterGrade; //Initializes char named 'tempLetterGrade' as a temporary place
holder for updated letter grade.

for(int iteration=0; iteration < rowSize-1; iteration++){ //Start of for loop.


for(int index = 0; index < rowSize-iteration-1; index++){ //Start of nested-for loop.
if(names[index]>names[index+1]){ //Start of nested-if loop.

//'bubble sorts' names.


tempName = names[index];
names[index] = names[index + 1];
names[index + 1] = tempName;

//'bubble sorts' number grade.


tempGrade = numGrades[index];
numGrades[index] = numGrades[index+1];
numGrades[index+1]=tempGrade;

//'bubble sorts' letter grade.


tempLetterGrade = letterGrades[index];
letterGrades[index] = letterGrades[index+1];
letterGrades[index+1]=tempLetterGrade;

} //End of nested-if loop.


} //End of nested-for loop.
} //End of for loop.
} //End of function 'bubbleSort'

Anda mungkin juga menyukai