| | |
Selection Sort and String
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 9
Reputation:
Solved Threads: 0
I'm having trouble with a program. The program needs to do a selection sort of last names. I have the program sorting the names correctly, but I can not get the correct first name to appear next to its last name. I've tried a loop and either get the same name or an incorrect name. The code and data follows.
Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> #include <iomanip> #include <iostream> using namespace std; void sortData(string lName[], int noOfRows); int main () { // step 1 string lName[15]; string fName; int age, wTime, i = 0, j, k, num = 0; char sex; ifstream inFile; // input stream variable for data file ofstream outFile; // output stream variable for result data inFile.open("data.txt"); if (!inFile) // step 3 { cout << "Cannot open the input file." << endl; return 1; } outFile.open("results.txt");// step 4 while (!inFile.eof()) { inFile >> fName >> lName[i++] >> age >> sex >> wTime; } sortData(lName, i); for (k = 0; k < i; k++) { outFile << lName[k] << " " << endl; outFile << fName << endl; } return 0; } void sortData(string lName[], int noOfRows) { int i, j; int min; // selection sort for (i = 0; i < noOfRows - 1; i++) { // step a min = i; for (j = i + 1; j < noOfRows; j++) if (lName[j] < lName[min]) min = j; if(min!=i)// step b lName[i].swap(lName[min]); } }
Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47
Did you post your exact program? Because the code you posted doesn't fit the results you posted.
>>inFile >> fName >> lName[i++] >> age >> sex >> wTime;
lName array is ok, but on every iteration of that loop the program just simply overwrites whatever was read on the previous loop for the other variables. I'd suggest you use a structure to contain all the information, then sort the structures.
Now all you have to do is use selection sort to sort the structures by last name. use memcpy() to exchange the entire structure, not just the individual elements of the structure will make it work faster.
>>inFile >> fName >> lName[i++] >> age >> sex >> wTime;
lName array is ok, but on every iteration of that loop the program just simply overwrites whatever was read on the previous loop for the other variables. I'd suggest you use a structure to contain all the information, then sort the structures.
C++ Syntax (Toggle Plain Text)
struct person { string lName; string fName; int age; // etc etc }; struct person array[MaxPeople]; //put the next two lines inside the read-data loop. nFile >> array[i].fName >> array[i].lName>> array[i].age >> array[i].sex >> array[i].wTime; ++i;
Now all you have to do is use selection sort to sort the structures by last name. use memcpy() to exchange the entire structure, not just the individual elements of the structure will make it work faster.
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 273
If you aren't allowed to use struct/classes to this as suggested by Ancient Dragon, then it can be done by using parallel arrays. This type of problem seems to be commonly assigned by instructors as a mechanism to get you working with arrays and as a mechanism to get you thinking in a problem solving mode. In particular if you had two arrays, one with last names and one with first names and each first name is related to the last name with the same index and you sort the last name array, how would you assure that the first names are changed at the same time so they remain assoicated with the correct last name?
•
•
Join Date: Apr 2006
Posts: 9
Reputation:
Solved Threads: 0
I think I understand the parallel array, but I can't get the first name to stay with the correct last name. The parallel array is based on the i variable. but some how the first name still does not stay where it needs to be. I think I may need to add the fName variable to the sort function. Your also correct, I can not use struct in this program. I wish I could I understand it better.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> #include <iomanip> #include <iostream> using namespace std; void sortData(string lName[], string fName[], int noOfRows); int main () { // step 1 string lName[15]; string fName[15]; int age, wTime, i = 0, j, k, num = 0; char sex; ifstream inFile; // input stream variable for data file ofstream outFile; // output stream variable for result data inFile.open("data.txt"); if (!inFile) // step 3 { cout << "Cannot open the input file." << endl; return 1; } outFile.open("results.txt");// step 4 while (!inFile.eof()) { inFile >> fName[i] >> lName[i] >> age >> sex >> wTime; i++; } sortData(lName, fName, i); for (i = 0; i < 15; i++) { outFile << lName[i] << " " << fName[i] << endl; } return 0; } void sortData(string lName[], string fName[], int noOfRows) { int i, j; int min; // selection sort for (i = 0; i < noOfRows - 1; i++) { // step a min = i; for (j = i + 1; j < noOfRows; j++) if (lName[j] < lName[min]) min = j; if(min!=i)// step b lName[i].swap(lName[min]); } }
•
•
Join Date: Apr 2006
Posts: 9
Reputation:
Solved Threads: 0
I need a little more help on this program.
I need to determine the fitness level of the people in the file. I wrote a function that returns a int value based on thier fitness level. This worked fine with the normal loop, but now the code can not determine between the different record sets.
Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47
I need to determine the fitness level of the people in the file. I wrote a function that returns a int value based on thier fitness level. This worked fine with the normal loop, but now the code can not determine between the different record sets.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <string> #include <iomanip> #include <iostream> using namespace std; int cal_Fitness_Level(int age[], char sex[], int wTime[]); void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows); int main () { // step 1 string lName[15]; string fName[15]; int age[15], wTime[15], i = 0, level, num = 0; char sex[15]; ifstream inFile; // input stream variable for data file ofstream outFile; // output stream variable for result data inFile.open("data.txt"); if (!inFile) // step 3 { cout << "Cannot open the input file." << endl; return 1; } outFile.open("results.txt");// step 4 outFile << setfill(' ') << left << setw(15) << "Last Name" //header for output file << setfill(' ') << left << setw(22) << "First Name" << setfill(' ') << left << setw(7) << "Age" << setfill(' ') << left << setw(7) << "Gender" << setfill(' ') << left << setw(10) << "Walk Time" << setfill(' ') << left << setw(13) << "Fitness Level" << endl; while (!inFile.eof()) { inFile >> fName[i] >> lName[i] >> age[i] >> sex[i] >> wTime[i]; // step 5 i++; level = cal_Fitness_Level(age, sex, wTime);// step 6 num++; } sortData(lName, fName, age, sex, wTime, i);// step 7 for (i = 0; i < 9; i++)// step 8 { outFile << setfill(' ') << left << setw(15) << lName[i] << setfill(' ') << left << setw(22) << fName[i] << setfill(' ') << left << setw(9) << age[i] << setfill(' ') << left << setw(7) << sex[i] << setfill(' ') << left << setw(11) << wTime[i] << setfill(' ') << left << setw(7) << level << endl; } outFile << '\n' << "Number of records: " << num << endl; return 0; } int cal_Fitness_Level(int age[], char sex[], int wTime[]) { int fL; int i = 0; if (age[i] >= 13 && age[i] <= 19) { if (wTime[i] >= 48) fL = 1; else if (wTime[i] > 43 && wTime[i] <= 47) fL = 2; else if (wTime[i] > 39 && wTime[i] <= 43) fL = 3; else if (wTime[i] > 35 && wTime[i] <= 39) fL = 4; else if (wTime[i] < 35) fL = 5; } else fL = 0; return fL; }
Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47
![]() |
Similar Threads
- Selection Sort in java (Java)
- Problem converting Selection Sort to Class (C++)
- hw assignment help on selection & merge sort (C)
- Some help needed with Selection Sort in java, Please!! (Java)
Other Threads in the C++ Forum
- Previous Thread: Shell Sort w/ fibonacci numbers...HELP!!! Due Friday
- Next Thread: pointer conversion issue
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






