| | |
Using Bubble sort to sort Arrays please help
Thread Solved
![]() |
•
•
Join Date: Dec 2008
Posts: 14
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon; Dec 4th, 2008 at 9:59 pm. Reason: add code tags
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:
Example:
C++ Syntax (Toggle Plain Text)
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 } }
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.
<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.
•
•
Join Date: Dec 2008
Posts: 14
Reputation:
Solved Threads: 0
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...?
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.
•
•
Join Date: Dec 2008
Posts: 14
Reputation:
Solved Threads: 0
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;
}•
•
Join Date: Dec 2008
Posts: 14
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Bubble Sort in Linked List- Help Greatly Appreciated (Java)
- Hey Just started new topic with arrays, and having some trouble please help (C++)
- help with sorting arrays from hightest to lowest (C++)
- i need help with arrays (Java)
- Sorting arrays of pointers with function? (C)
- Why Doesn't My Sort Work!? (Java)
- Arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: find string in txt file
- Next Thread: your help is needed (loops)
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






