Vous êtes sur la page 1sur 4

//grades.

cxx 1/31/2012
//Brenden Hogan brenden.hogan@colorado.edu
//A grades program designed to take in a
//student id and 5 quiz scores then sort
//them in the desired order and display
//with averages included.
#include <iostream>
#include <cassert>
using namespace std;

//Prototypes
//Takes in a 2 demensional double array with a ize and a column to focus on.
//Then sorts them from smallest to largest.
void bubbleSort( double students[50][7], int size, int col);
//Computes the avarage for all quizes in the 7th column for each student
void computeAvg( double students[50][7], int size);
//A helper meathod that returns the average of a array of doubles
//Used to compute the class average
double computeAvgG( double nums[50], int size);
//Takes in all number data and stores it in a double array asserting the first
//number is a real num >0. Then takes in chars and stores them in a array
//until T is entered.
void inputData( double students[50][7], int& size,
char cmnds[100], int& sizeC);
//Prints in the proper format the expected output
void printList(double students[50][7], int size);
//Exacutes the commands in the cmnds array and decides how to sort using
//a switch statement.
void runCmnds( double students[50][7], int size, char cmnds[100], int sizeC);
//Swaps two students at positions a and b by switching all values.
void swap( double students[50][7], int a, int b);

//-----------------------------------------------------------------------------int main()
{
double students[50][7];
int size;
char cmnds[100];
int sizeC;
inputData( students, size, cmnds, sizeC);
computeAvg( students, size);
runCmnds( students, size, cmnds, sizeC);
return 0;
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------void bubbleSort(double students[50][7], int size, int col){
for(int i=0; i<size; i++){
for(int j=0; j<size-1; j++){
if(students[j][col]>students[j+1][col]) swap(students, j, j+1);
}

}
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------void computeAvg(double students[50][7], int size){
double temp=0;
for(int i=0; i<size; i++){
for(int j=1; j<6; j++){
temp+=students[i][j];
}
students[i][6]=(temp/5);
temp=0;
}
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------double computeAvgG(double nums[50], int size){
double temp=0;
double tmpsize=size;
for(int i=0; i<size; i++){
temp+=nums[i];
}
temp=temp/tmpsize;
return temp;
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------void inputData(double students[50][7], int& size, char cmnds[100], int& sizeC){
char temp;
cin >> size;
assert(size>0);
for(int i=0; i < size; i++){
for(int j=0;j<6;j++){
cin >> students[i][j];
}
}
int i=0;
while (temp != 'T'){
cin >> temp;
cmnds[i]=temp;
i++;
}
sizeC=(i-1);
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------void printList(double students[50][7], int size){
double q1[50];
double q2[50];
double q3[50];

double q4[50];
double q5[50];
for(int i=0; i < size; i++){
q1[i]=students[i][1];
q2[i]=students[i][2];
q3[i]=students[i][3];
q4[i]=students[i][4];
q5[i]=students[i][5];
}
cout <<"Student Id\t" << "Quiz 1\t" << "Quiz 2\t" << "Quiz 3\t"
<< "Quiz 4\t" << "Quiz 5\t" << "Average" << endl;
for(int i=0; i<size; i++){
cout <<" "<<students[i][0] <<"\t" << students[i][1] <<"\t" <<
students[i][2] <<"\t" << students[i][3] <<"\t" << students[i][4] <<
"\t" << students[i][5] <<"\t" << students[i][6] << endl;
}
cout << "Quiz Average\t" << computeAvgG(q1, size) << "\t" <<
computeAvgG(q2, size) << "\t" << computeAvgG(q3, size) << "\t" <<
computeAvgG(q4, size) << "\t" << computeAvgG(q5, size) << endl;
cout << endl;
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------void runCmnds(double students[50][7], int size, char cmnds[100], int sizeC){
for(int i=0; i<sizeC; i++){
switch (cmnds[i]){
case 'S':bubbleSort(students,
break;
case 'A':bubbleSort(students,
break;
case 'B':bubbleSort(students,
break;
case 'C':bubbleSort(students,
break;
case 'D':bubbleSort(students,
break;
case 'E':bubbleSort(students,
break;
case 'F':bubbleSort(students,
break;
}
printList( students, size);

size, 0);
size, 1);
size, 2);
size, 3);
size, 4);
size, 5);
size, 6);

}
}
//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------void swap(double students[50][7], int a, int b){
double temp[7];
for(int i=0; i< 7; i++) temp[i]=students[a][i];
for(int i=0; i< 7; i++) students[a][i]=students[b][i];

for(int i=0; i< 7; i++) students[b][i]=temp[i];


}

Vous aimerez peut-être aussi