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 >>

It's up to your professor what s/he will accept. You'll be a better judge than any of us.

Looking at your code quickly my biggest concern is I'm not sure how you are keeping track of the original indexes of each last name within your program sp you can match the correct last name with the correct original indexes holding the other data after the sorting.

Here's an example of what I might do it if I had to use multiple arrays.

//say my test file contains
Sally Smith A
Tom Jones B

//my program flow might be something like:


//declare variables 
string fNames[2];
string lNames[2];
char grades[2];
int i;
ifstream fin("myFile.txt");

  //step 1: read into variables
  for(i = 0; i < 2; ++i)
  {
    fin >> fNames[i];
    fin >> lNames[i];
    fin >> grades[i];
  }


  //step 2: sort arrays based on putting lNames in alphabetical order
  i = 0;
  if(lNames[i] > lNames[i + 1])
  {
     swap(lNames, i); //swap lNames
     swap(fNames, i); //swap fNames
     swap(grades, i); //swap grades
     //now I don't have to keep track of indexes because I swap appropriate values in each array based on the indexes in lName that need to be swapped.
  }

  //step 3: display sorted arrays
  for(i = 0; i < 2; ++i)
    cout << lNames[i] << ", " << fNames[i] << ' ' << grades[i] << endl;


//where the swap functions were defined as something like this:
void swap(string name[], int i)
{
   string temp;
   temp = name[i];
   name[i] = name[i + 1];
   name[i + 1] = temp;
}
//and swap() was overloaded to handle int (and double and char, if desired) arrays in addition to string arrays

To specialize these general techniques to your case you would need to:
1) expand the number of arrays used to include all the exam scores and maybe the average and grade (depending on when you calculate those results)
2) expand the sorting protocol to allow you to sort more than just a 2 student array (you've already done one example of that in your posted code)
3) swap appropriate indexes in all arrays used rather than just three arrays like I did
4) format the output as you desire (which you seem to have a handle on already)

If you know about multidimensional arrays and how to convert strings to ints and visa versa, then you might be able to do this all in a single two dimensional array of strings (and just swap rows within the array based on last name) rather than multiple individual arrays.

But, when you learn about struct/classes, come back and redo this project using them. It will be impressive how much easier it is to accomplish a given task when you have the correct tools.

Restricted as you are by your current status on the learning curve, this will be a tedious process no matter how you set it up. Good luck.

Is it wrong to have a very long program if i did not find another way?

It is if you should have found another way.

Why do you have your offsets in some crazy order? (15, 5, 9, 14, ...)

This article has been dead for over six months. Start a new discussion instead.