Using Bubble sort to sort Arrays please help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 14
Reputation: Awebb999 has a little shameless behaviour in the past 
Solved Threads: 0
Awebb999 Awebb999 is offline Offline
Newbie Poster

Using Bubble sort to sort Arrays please help

 
0
  #1
Dec 4th, 2008
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

  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. const int size=20;
  8.  
  9. void ReadInfo(ifstream &, int[], string[], float[]);
  10. void calcScore(float[], int &);
  11. void calcLgrade(float[], char []);
  12. void Avg(float [], double &);
  13. void bubbleSort(string []);
  14. void printClass(int [], string [], float [], char [], double, int &);
  15.  
  16.  
  17. int main()
  18. {
  19. ifstream ifile;
  20.  
  21. int count=0;
  22. int id[size];
  23. float grade[size];
  24. string name[size];
  25. double Average;
  26. char letter[size];
  27.  
  28. ifile.open("grades.txt");
  29.  
  30. ReadInfo(ifile,id,name,grade);
  31. calcScore(grade,count);
  32. calcLgrade(grade,letter);
  33. Avg(grade,Average);
  34. bubbleSort(name);
  35. printClass(id,name,grade,letter,Average,count);
  36.  
  37.  
  38. ifile.close();
  39.  
  40. system ("pause");
  41. return 0;
  42. }
  43.  
  44.  
  45.  
  46. void ReadInfo(ifstream & infile, int ID[], string N[], float G[])
  47. {
  48. for(int i=0; i<size; i++)
  49. {
  50. infile>>ID[i]>>N[i]>>G[i];
  51. }
  52. }
  53.  
  54. void calcScore(float G[],int & count)
  55. {
  56. for(int i=0; i<size; i++)
  57. {
  58. G[i]=G[i]*2;
  59. if (G[i] >= 75)
  60. count++;
  61. }
  62. cout<<endl;
  63. }
  64.  
  65. void calcLgrade(float G[], char L[])
  66. {
  67. for(int i=0; i<size; i++)
  68. {
  69. if (G[i]>=90)
  70. L[i]='A';
  71. else if (G[i]>=80)
  72. L[i]='B';
  73. else if (G[i]>=70)
  74. L[i]='C';
  75. else if (G[i]>=60)
  76. L[i]='D';
  77. else
  78. L[i]='F';
  79. }
  80. }
  81.  
  82. void Avg(float G[], double & avg)
  83. {
  84. float sum=0;
  85. for(int i=0; i<size; i++)
  86. sum+=G[i];
  87. avg=sum/size;
  88. }
  89.  
  90. void bubbleSort(string N[])
  91. {
  92. int temp;
  93. int iteration;
  94. int index;
  95. for(iteration=1; iteration < size; iteration++)
  96. {
  97. for(index = 0; index < size - iteration; index++)
  98. if (N[index]> N[index+1])
  99. {
  100. temp=N[index];
  101. N[index]=N[index+1];
  102. N[index + 1] = temp;
  103. }
  104. }
  105. }
  106.  
  107. void printClass(int ID[], string N[], float G[], char L[], double avg, int & count)
  108. {
  109. for(int i=0; i<size; i++)
  110. cout<<ID[i]<<" "<<setw(15)<<N[i]<<" "<<setw(15)<<G[i]<<" "<<setw(5)<<L[i]<<endl;
  111. cout<<endl;
  112. cout<<fixed<<showpoint;
  113. cout<<setprecision(2);
  114. cout<<"The class average is "<<avg<<endl;
  115. cout<<"There are "<<count<<" students with atleast a 75% average."<<endl;
  116. }
Last edited by Ancient Dragon; Dec 4th, 2008 at 9:59 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using Bubble sort to sort Arrays please help

 
0
  #2
Dec 4th, 2008
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:
  1. void bubbleSort(string Name[], int id[], float grade[], char letter[])
  2. {
  3. <snip>
  4. if (Name[index]> Name[index+1]) {
  5. {
  6. string temp=Name[index];
  7. Name[index]=Name[index+1];
  8. Name[index + 1] = temp;
  9.  
  10. int idtemp = id[index];
  11. id[index] = id[index+1];
  12. id[index+1] = idtemp;
  13.  
  14. // now do the same think with the other arrays
  15. }
  16.  
  17. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 14
Reputation: Awebb999 has a little shameless behaviour in the past 
Solved Threads: 0
Awebb999 Awebb999 is offline Offline
Newbie Poster

Re: Using Bubble sort to sort Arrays please help

 
0
  #3
Dec 4th, 2008
What is <snip>? and thank you i will try it
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Using Bubble sort to sort Arrays please help

 
0
  #4
Dec 4th, 2008
<snip> means I intentially omitted code, such as snip it with a pair of sizzers.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 14
Reputation: Awebb999 has a little shameless behaviour in the past 
Solved Threads: 0
Awebb999 Awebb999 is offline Offline
Newbie Poster

Re: Using Bubble sort to sort Arrays please help

 
0
  #5
Dec 4th, 2008
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...?
Last edited by Awebb999; Dec 4th, 2008 at 10:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 14
Reputation: Awebb999 has a little shameless behaviour in the past 
Solved Threads: 0
Awebb999 Awebb999 is offline Offline
Newbie Poster

Re: Using Bubble sort to sort Arrays please help

 
0
  #6
Dec 4th, 2008
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;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 14
Reputation: Awebb999 has a little shameless behaviour in the past 
Solved Threads: 0
Awebb999 Awebb999 is offline Offline
Newbie Poster

Re: Using Bubble sort to sort Arrays please help

 
0
  #7
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC