Can't seem to sort this Array

Hello,

I am working on an assignment to take input from a file, populate several arrays, then sort the data. I am getting hung up on the proper way to alphabetize the strings within the array Name[]. This is a homework assignment so just a nudge in the right direction would be helpful. I have searched online and tried a few solutions with no luck. Code is below. The function that is to sort is called AlphaStudents.

Thank You,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

double CalcAverage(int classGrade[]);
double CalcPass(int classGrade[]);
void AlphaStudents(string StudentID[], string Name[], int ClassGrade[]);
int main()
{
    
    ifstream inFile;
    ofstream outFile;
    inFile.open("grades.txt");
    outFile.open("gradereport.txt");
    string StudentID[15];
    string Name[15];
    int ClassGrade[15];
    int ClassGrade100[15];
    string ClassGradeLtr[15];
    int i = 0;
    while(inFile)
   {
    inFile >> StudentID[i];
    inFile >> Name[i];
    inFile >> ClassGrade[i];
    i++;
    }
   AlphaStudents(StudentID,Name,ClassGrade);
   for(int i=0; i<15; i++)
    { 
      cout << StudentID[i] <<"    "<< Name[i] <<"     "<< ClassGrade[i]<< endl<<endl;
    }
          
    double average = CalcAverage(ClassGrade);  //Calls function to average grades
    cout << "The Class Average is:" << average <<"%"<< "" << endl;  //Prints average to screen
    double passNum = CalcPass(ClassGrade);//Calls function to calculate grades over 75%
    cout << "The Number of Students receiving 75% or more is:" << passNum <<""<< endl;//Prints number of students over 75%
    system("PAUSE");

    return EXIT_SUCCESS;
}

double CalcAverage(int Classgrade[])
{    
     int sum=0;
     double average=0;
     for(int i = 0; i < 15; i++)
     {
             sum = Classgrade[i] + sum;
     }
     average = sum/15;
     average = average/50;
     average = average * 100;
     return average;
}
double CalcPass(int classGrade[])
{
       int numPass = 0;
       for (int i =0; i < 15; i++)
       {
           if (classGrade[i] > 32)
           {
              numPass = numPass + 1;
           }
       }
       return numPass;
}
void AlphaStudents(string StudentID[], string Name[], int ClassGrade[])
{
        for (int count = 0; count < 15; count++)
         {
         int lowest = count;
             for (Name[count] = Name[count + 1]; count < 15; count++)
             {
                 if(Name[count + 1] < Name[count])
                 {
                  Name[lowest] = Name[count +1];
                  }
             }
             swap(Name[count], Name[lowest]);
             }           
      
}
I'm not going to change what your are doing, just suggest what you might want to do.
It looks like a bubble sort:
http://en.wikipedia.org/wiki/Bubble_sort
Take a look at the pseudocode for that and compare it to what you are doing.
Notice that your array is only 15 but your are accessing element count+1 which would SegFault. Not seeing your input file, I think your Name can only be one word, otherwise things get jacked.
Also, with the way you are doing it now after the names are sorted the StudentID and ClassGrade wont match any more if you swapped any values. When you swap names you are going to have to swap StudentID and ClassGrade as well. Get this working and then think about putting all of the information about a student into one class with theses three classes inside. That way when you swap you swap all of the elements at once.

Hope I made sense.

EDIT: Also, take a look at your CalcAverage. Why the 50 and why the 100?
EDIT: CalcPass, check your code against what you print in the main.
Last edited on
Topic archived. No new replies allowed.