Code: Select all
/**
/**
Marcel Girgis
11/24/10
1:57pm
PROGRAM ASSIGNMENT 3
Problem:
Write a program to
- Store test grades for all students in a CS class
- Store data in a 2d array
- 50 rows by 5 columns
Input Formatting:
<# telling how many students>
<grade1><space><grade2><space><grade3><space><grade4><space><grade 5>
Output:
Via console
Additional Notes:
- Adjust constants and recompile to change settings
**/
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
const unsigned short int MAX_STUDENT_SIZE=50, PADDING_WIDTH=8;
using namespace std;
void InitializeArray (int sudentRecords[][5]);
int ReadStudentData (string file_name, int sudentRecords[][5]);
void ShowStudentRecords(int sudentRecords [][5], int num);
void ShowResults(int sudentRecords [][5], int num);
int main() {
int sudentRecords[MAX_STUDENT_SIZE][5];
int student_counter=0;
InitializeArray(sudentRecords);
//Prompt for file names
string file_name;
cout << "Enter the file name you would like to read data from: ";
cin>>file_name;
//read student data and fetch # read
student_counter = ReadStudentData(file_name, sudentRecords);
if(student_counter==-1) return -1; //signifies no students to be read, so exit.
//echo records
ShowStudentRecords(sudentRecords, student_counter);
//do all requested processing and output
cout << "\nProcessing data...\n"<<endl;
ShowResults(sudentRecords, student_counter);
cout << "\nProgram terminating. \n\nSo long, and thanks for the fish! =)"
<<endl;
system("pause");
}
/*
Simply initializes our all student data to default values.
This function is part of the program requirements.
Pre:Takes a 2d array of size MAX_STUDENT_SIZE x 5
Post: Array values have been set to zero
*/
void InitializeArray(int studetRecords[][5]) {
for(int i=0;i<MAX_STUDENT_SIZE;i++) {
for(int j=0;j<5;j++) {
studetRecords[i][j]=0;
}
}
}
/*
This function uses fstream to read from a file with argument 1's name
and populate our student's data based on the results.
pre: constant MAX_STUDENT_SIZE has been properly set
post: array student records now contain all student records.
*/
int ReadStudentData(string file_name, int studentRecords[][5]) {
fstream file_in;
int student_counter=0;
//double total_salary=0;
file_in.open(file_name.data());
if(!file_in) {
cout << "There was an error opening your file!"<<endl;
return -1; //to signify an error
}
cout << "Successfully opened file."<<endl;
//parse file
while(!file_in.eof()) {
//read amount first
file_in>>student_counter;
for(int i=0;i<student_counter&&i<MAX_STUDENT_SIZE;i++) {
for(int j=0;j<5;j++) {
file_in>>studentRecords[i][j];
file_in.ignore();
//file_in.put('\n');
if(!file_in)return -1; //there aren't as many available
//grades as there should be.
}
}
//ignore the extra newline read after the ID
file_in.ignore();
}
cout << "\nSuccessfully read " << student_counter <<
" students."<<endl;
file_in.close();
return student_counter;
}
//pre: sudent records row size > num
//post: displays all requested results
void ShowResults(int sudentRecords [][5], int num) {
cout << setw(PADDING_WIDTH)<<"STUDENT #"
<< setw(PADDING_WIDTH)<<"Average"
<< setw(PADDING_WIDTH)<<"Grade\n";
double average;
int averageCounter[5]={0,0,0,0,0}; //index 1 corresponds to A, 2 corresponds to B, etc;
double highestAverage=0;
int highestAverageIndex=-1;
for(int i=0;i<num;i++) {
average=0;
cout << endl;
cout << setw(PADDING_WIDTH)<< i;
//count each grade for the current student (j)
for(int j=0;j<5;j++) {
average+=sudentRecords[i][j];
}
average/=5; //5 grades
//check if we have a new high average
if(average > highestAverage) {
highestAverage=average;
highestAverageIndex=i;
}
cout << setw(PADDING_WIDTH)<<fixed << setprecision(1)<< average;
//determine where they fall in average
char letter = ((average >= 90 && average<= 100 ) ? 'A' :
( average >= 80 && average< 90 ) ? 'B' :
( average >= 70 && average< 80 ) ? 'C' :
( average >= 60 && average< 70 ) ? 'D' :
( average < 60 ) ? 'F' : 'X') ;
cout << setw(PADDING_WIDTH)<< letter;
//increment out counter
averageCounter[letter-(letter !='F' ? 'A' : 'B' )]++;
}
//display averages
for(int i =0;i<5;i++)
cout << endl << "Number of " << char( (i==4? 5 : i)+'A')<<"'s: " << averageCounter[i];
cout << endl << "Highest Average: Student # : " << highestAverageIndex << ", " << highestAverage;
}
//pre: array studentRecords contains grades for rows j to num
//post: displays to console the contents of student records
void ShowStudentRecords(int sudentRecords [][5], int num) {
cout << setw(PADDING_WIDTH)<<"STUDENT #"
<< setw(PADDING_WIDTH)<<"TEST1"
<< setw(PADDING_WIDTH)<<"TEST2"
<< setw(PADDING_WIDTH)<<"TEST3"
<< setw(PADDING_WIDTH)<<"TEST4"
<< setw(PADDING_WIDTH)<<"TEST5\n";
for(int i=0;i<num;i++) {
cout << endl;
cout << setw(PADDING_WIDTH)<< i;
for(int j=0;j<5;j++) {
// cout << setw(PADDING_WIDTH)<< i;
cout << setw(PADDING_WIDTH)<<sudentRecords[i][j];
}
}
}