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

You are going to have to add more parameters to the bubble sort function. When the primary array members are exchanged, all the other array members must also be exchanged so that all the arrays are kept in sync with each other.

Example:

void bubbleSort(string Name[], int id[], float grade[], char letter[])
{
<snip>
 if (Name[index]> Name[index+1])                                {
{
       string temp=Name[index];
       Name[index]=Name[index+1];
       Name[index + 1] = temp; 

       int idtemp = id[index];
       id[index] = id[index+1];
       id[index+1] = idtemp;

        // now do the same think with the other arrays
}

}

What is <snip>? and thank you i will try it

<snip> means I intentially omitted code, such as snip it with a pair of sizzers.

ok when ever i compile and try to run this program my computer windows cuts the program off...but without this bubble thing my computer runs it fine...I don't understand?
windows says that the program has stopped working...then it trys to find solution to problem...?

This is the updated version, see if you can run it.

#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 [], int [], float [], char []);
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,id,grade,letter);
    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 Name[], int id[], float grade[], char letter[])
{
 int index;
 if (Name[index]> Name[index+1])                                
  {
       string temp=Name[index];
       Name[index]=Name[index+1];
       Name[index + 1] = temp; 

       int idtemp = id[index];
       id[index] = id[index+1];
       id[index+1] = idtemp;
       
       float gradetemp=grade[index];
       grade[index]=grade[index+1];
       grade[index+1]=gradetemp;
       
       char lettertemp=letter[index];
       letter[index]=letter[index+1];
       letter[index+1]=lettertemp;
  }
}

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

below is the info that is in the file "grades.txt" i had it in notepad


134 Johnson 50
156 Murray 33
145 Lowes 45
111 Adams 15
678 Wright 46
890 Smith 10
433 Price 50
344 Poirier 28
100 Pratt 44
888 Gilbert 48
677 Muroff 20
455 Malik 49
788 Halls 33
432 Scholl 38
670 Seitz 40
801 Booth 5
906 Ruffolo 43
440 Pond 47
999 Harrison 15
596 Gibbs 29

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.