I just learned about bubble sort and still don't know how to make it work in my program I hope someone can help me.
This is my task
I should use parallel arrays (of size 20) for student name, student number, test score, and letter grade.
The list must be sorted in alphabetical order by student name.
I have the program up and running...I just can't get it to sort at all.
This is what I got
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int size=20;
void ReadInfo(ifstream &, int[], string[], float[]);
void calcScore(float[], int &);
void calcLgrade(float[], char []);
void Avg(float [], double &);
void bubbleSort(string []);
void printClass(int [], string [], float [], char [], double, int &);
int main()
{
ifstream ifile;
int count=0;
int id[size];
float grade[size];
string name[size];
double Average;
char letter[size];
ifile.open("grades.txt");
ReadInfo(ifile,id,name,grade);
calcScore(grade,count);
calcLgrade(grade,letter);
Avg(grade,Average);
bubbleSort(name);
printClass(id,name,grade,letter,Average,count);
ifile.close();
system ("pause");
return 0;
}
void ReadInfo(ifstream & infile, int ID[], string N[], float G[])
{
for(int i=0; i<size; i++)
{
infile>>ID[i]>>N[i]>>G[i];
}
}
void calcScore(float G[],int & count)
{
for(int i=0; i<size; i++)
{
G[i]=G[i]*2;
if (G[i] >= 75)
count++;
}
cout<<endl;
}
void calcLgrade(float G[], char L[])
{
for(int i=0; i<size; i++)
{
if (G[i]>=90)
L[i]='A';
else if (G[i]>=80)
L[i]='B';
else if (G[i]>=70)
L[i]='C';
else if (G[i]>=60)
L[i]='D';
else
L[i]='F';
}
}
void Avg(float G[], double & avg)
{
float sum=0;
for(int i=0; i<size; i++)
sum+=G[i];
avg=sum/size;
}
void bubbleSort(string N[])
{
int temp;
int iteration;
int index;
for(iteration=1; iteration < size; iteration++)
{
for(index = 0; index < size - iteration; index++)
if (N[index]> N[index+1])
{
temp=N[index];
N[index]=N[index+1];
N[index + 1] = temp;
}
}
}
void printClass(int ID[], string N[], float G[], char L[], double avg, int & count)
{
for(int i=0; i<size; i++)
cout<<ID[i]<<" "<<setw(15)<<N[i]<<" "<<setw(15)<<G[i]<<" "<<setw(5)<<L[i]<<endl;
cout<<endl;
cout<<fixed<<showpoint;
cout<<setprecision(2);
cout<<"The class average is "<<avg<<endl;
cout<<"There are "<<count<<" students with atleast a 75% average."<<endl;
}