HI:
Maybe some of you read my thread about how to sort a file that contained 17 students data:
ID LASTNAME FIRSTNAME 10 SCORES
My program had to read this file, sort the names alphabetically, then calculate average score and letter grades.
Finally send all data to a new file that contained the Lastname, a comma, firstname, 10 scores, average and letter grade for each student.
Also the file should look formatted( all justified).
I tried to do it in many ways, some of you gave me great ideas.
This is the program I created, but it is very long.
1) I read file and input data into arrays
2) concatenate the last name + , + firstname;
3)while reading i calculate average and grades
4) create a swap and sort function to sort names
5) Call function
6) output each student individually (i wrote on a paper the index each student had before sorting names, so after sorting i just add scores, grades and average using the idex the student had before to not loose the student original data)
While processing each student, i format each one of them
"i know it is a very tedious program, but it does what it is suppossed to do"
so, i just wanted to know if you think that the teacher would accept this kind of programs or not!
THIS IS MY LONG LONG PROGRAM!
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
// Swap (): swaps two names if not in order.
void Swap (string& x, string& y)
{ string temp;
temp=x;
x=y;
y=temp;
}
// Sort(): function that calls the names from the main program and
// sorts them alphabetically.
void Sort (string full[])
{int small_pos;
string small;
for (int k=0; k<17; k++)
{small =full[k];
for (int n=k; n<17; n++)
if (full[n]<=small)
{small_pos=n;
small=full[n];
}
Swap (full[k], full[small_pos]);
}
}
int main(int argc, char *argv[])
{
ifstream infile;
ofstream outfile;
int ID;
infile.open("c:\\students.txt");
outfile.open("c:\\grades.txt");
string lastN[17], firstN[17], fullname[17];
int scores[17][10];
float ave[17];
char grade[17];
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
for(int count=0; count<17; count++)
{
float sum=0; // the cout statements inside this for
infile>>ID; // loop are just to see it on the screen
infile>>lastN[count]; // before sorting. I also calculate
infile>>firstN[count]; // average and grades inside this loop.
fullname[count]=lastN[count]+","+firstN[count];
cout.precision(3);
cout << showpoint;
cout.width(20);
cout<<left<<fullname[count]<<" ";
for(int n=0; n<10; n++)
{infile>>scores[count][n];
cout.precision(3);
cout << showpoint;
cout.width(3);
cout<<right<<scores[count][n]<<" ";
sum+=scores[count][n];
}
ave[count]=sum/10;
cout.precision(1);
cout << showpoint;
cout.width(3);
cout<<ave[count]<<" ";
if (ave[count]>=90.0)
{grade[count]='A';
cout<<grade[count];
}
else
{ if (ave[count]>=80.0)
{grade[count]='B';
cout<<grade[count];
}
else
{ if (ave[count]>=70.0)
{grade[count]='C';
cout<<grade[count];
}
else
{ if (ave[count]>=60.0)
{grade[count]='D';
cout<<grade[count];
}
else
{grade[count]='F';
cout<<grade[count];
}
}
}
}
cout<<endl;
sum=0;
} // END OF LOOP FOR READING
cout<<endl<<endl;
Sort (fullname); // CALLING MY SORT FUNCTION
// FORMATTING, " STILL WORKING ON THE LAST 16 STUDENTS"
outfile.precision(3);
outfile << showpoint;
outfile.width(20);
// I kept track of the number the student was before
// sorting so I could then put the rigth scores, average
// and grades in the right person!
outfile<<left<<fullname[0]<<" "; // FIRST STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<right<<scores[15][s]<<" ";
}
outfile<< showpoint;
outfile.width(1);
outfile<<ave[15]<<" "<<grade[15]<<endl;
outfile.precision(3);
outfile << showpoint;
outfile.width(20);
outfile<<left<<fullname[1]<<" "; // SECOND STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<right<<scores[5][s]<<" ";
}
outfile<< showpoint;
outfile.width(1);
outfile<<ave[5]<<" "<<grade[5]<<endl;
outfile.precision(3);
outfile << showpoint;
outfile.width(20);
outfile<<left<<fullname[2]<<" "; // THIRD STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<right<<scores[9][s]<<" ";
}
outfile<< showpoint;
outfile.width(1);
outfile<<ave[9]<<" "<<grade[9]<<endl;
outfile.precision(3);
outfile << showpoint;
outfile.width(20);
outfile<<left<<fullname[3]<<" "; // FOURTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<right<<scores[14][s]<<" ";
}
outfile<< showpoint;
outfile.width(1);
outfile<<ave[14]<<" "<<grade[14]<<endl;
outfile.precision(3);
outfile << showpoint;
outfile.width(20);
outfile<<left<<fullname[4]<<" "; // FIFTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<right<<scores[2][s]<<" ";
}
outfile<<ave[2]<<" "<<grade[2]<<endl;
outfile<<left<<fullname[5]<<" "; // SIXTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[7][s]<<" ";
}
outfile<<ave[7]<<" "<<grade[7]<<endl;
outfile<<fullname[6]<<" "; // SEVENTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[11][s]<<" ";
}
outfile<<ave[11]<<" "<<grade[11]<<endl;
outfile<<fullname[7]<<" "; // EIGTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[12][s]<<" ";
}
outfile<<ave[12]<<" "<<grade[12]<<endl;
outfile<<fullname[8]<<" "; // NINTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[10][s]<<" ";
}
outfile<<ave[10]<<" "<<grade[10]<<endl;
outfile<<fullname[9]<<" "; // TENTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[0][s]<<" ";
}
outfile<<ave[0]<<" "<<grade[0]<<endl;
outfile<<fullname[10]<<" "; // ELEVENTH STUDENT
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[4][s]<<" ";
}
outfile<<ave[4]<<" "<<grade[4]<<endl;
outfile<<fullname[11]<<" "; // STUDENT 12
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[16][s]<<" ";
}
outfile<<ave[16]<<" "<<grade[16]<<endl;
outfile<<fullname[12]<<" "; // STUDENT 13
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[6][s]<<" ";
}
outfile<<ave[6]<<" "<<grade[6]<<endl;
outfile<<fullname[13]<<" "; // STUDENT 14
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[13][s]<<" ";
}
outfile<<ave[13]<<" "<<grade[13]<<endl;
outfile<<fullname[14]<<" "; // STUDENT 15
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[3][s]<<" ";
}
outfile<<ave[3]<<" "<<grade[3]<<endl;
outfile<<fullname[15]<<" "; // STUDENT 16
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[8][s]<<" ";
}
outfile<<ave[8]<<" "<<grade[8]<<endl;
outfile<<fullname[16]<<" "; // STUDENT 17
for (int s=0; s<10; s++)
{ outfile.precision(3);
outfile<< showpoint;
outfile.width(3);
outfile<<scores[1][s]<<" ";
}
outfile<<ave[1]<<" "<<grade[1]<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
<< moderator edit: added [code][/code] tags >>