944,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5684
  • C++ RSS
Apr 5th, 2006
0

Selection Sort and String

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. void sortData(string lName[], int noOfRows);
  10.  
  11. int main ()
  12. {
  13. // step 1
  14. string lName[15];
  15. string fName;
  16. int age, wTime, i = 0, j, k, num = 0;
  17. char sex;
  18. ifstream inFile; // input stream variable for data file
  19. ofstream outFile; // output stream variable for result data
  20.  
  21. inFile.open("data.txt");
  22.  
  23. if (!inFile) // step 3
  24. {
  25. cout << "Cannot open the input file." << endl;
  26. return 1;
  27. }
  28.  
  29. outFile.open("results.txt");// step 4
  30.  
  31. while (!inFile.eof())
  32. {
  33. inFile >> fName >> lName[i++] >> age >> sex >> wTime;
  34. }
  35.  
  36. sortData(lName, i);
  37.  
  38. for (k = 0; k < i; k++)
  39. {
  40. outFile << lName[k] << " " << endl;
  41. outFile << fName << endl;
  42. }
  43. return 0;
  44. }
  45.  
  46.  
  47. void sortData(string lName[], int noOfRows)
  48. {
  49. int i, j;
  50. int min;
  51.  
  52. // selection sort
  53. for (i = 0; i < noOfRows - 1; i++)
  54. {
  55. // step a
  56. min = i;
  57.  
  58. for (j = i + 1; j < noOfRows; j++)
  59. if (lName[j] < lName[min])
  60. min = j;
  61.  
  62.  
  63. if(min!=i)// step b
  64. lName[i].swap(lName[min]);
  65. }
  66. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 5th, 2006
0

Re: Selection Sort and String

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.
C++ Syntax (Toggle Plain Text)
  1. struct person
  2. {
  3. string lName;
  4. string fName;
  5. int age;
  6. // etc etc
  7. };
  8.  
  9. struct person array[MaxPeople];
  10.  
  11. //put the next two lines inside the read-data loop.
  12. nFile >> array[i].fName >> array[i].lName>> array[i].age >> array[i].sex >> array[i].wTime;
  13. ++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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Apr 5th, 2006
0

Re: Selection Sort and String

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?
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 6th, 2006
0

Re: Selection Sort and String

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)
  1.  
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. void sortData(string lName[], string fName[], int noOfRows);
  10.  
  11. int main ()
  12. {
  13. // step 1
  14. string lName[15];
  15. string fName[15];
  16. int age, wTime, i = 0, j, k, num = 0;
  17. char sex;
  18. ifstream inFile; // input stream variable for data file
  19. ofstream outFile; // output stream variable for result data
  20.  
  21. inFile.open("data.txt");
  22.  
  23. if (!inFile) // step 3
  24. {
  25. cout << "Cannot open the input file." << endl;
  26. return 1;
  27. }
  28.  
  29. outFile.open("results.txt");// step 4
  30.  
  31. while (!inFile.eof())
  32. {
  33. inFile >> fName[i] >> lName[i] >> age >> sex >> wTime;
  34. i++;
  35. }
  36. sortData(lName, fName, i);
  37.  
  38. for (i = 0; i < 15; i++)
  39. {
  40. outFile << lName[i] << " " << fName[i] << endl;
  41. }
  42.  
  43. return 0;
  44. }
  45.  
  46.  
  47. void sortData(string lName[], string fName[], int noOfRows)
  48. {
  49. int i, j;
  50. int min;
  51.  
  52. // selection sort
  53. for (i = 0; i < noOfRows - 1; i++)
  54. {
  55. // step a
  56. min = i;
  57.  
  58. for (j = i + 1; j < noOfRows; j++)
  59. if (lName[j] < lName[min])
  60. min = j;
  61.  
  62. if(min!=i)// step b
  63. lName[i].swap(lName[min]);
  64. }
  65. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 6th, 2006
0

Re: Selection Sort and String

I got it, thanks for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006
Apr 6th, 2006
0

Re: Selection Sort and String

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.

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int cal_Fitness_Level(int age[], char sex[], int wTime[]);
  9. void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows);
  10.  
  11. int main ()
  12. {
  13. // step 1
  14. string lName[15];
  15. string fName[15];
  16. int age[15], wTime[15], i = 0, level, num = 0;
  17. char sex[15];
  18. ifstream inFile; // input stream variable for data file
  19. ofstream outFile; // output stream variable for result data
  20.  
  21. inFile.open("data.txt");
  22.  
  23. if (!inFile) // step 3
  24. {
  25. cout << "Cannot open the input file." << endl;
  26. return 1;
  27. }
  28.  
  29. outFile.open("results.txt");// step 4
  30.  
  31. outFile << setfill(' ') << left << setw(15) << "Last Name" //header for output file
  32. << setfill(' ') << left << setw(22) << "First Name"
  33. << setfill(' ') << left << setw(7) << "Age"
  34. << setfill(' ') << left << setw(7) << "Gender"
  35. << setfill(' ') << left << setw(10) << "Walk Time"
  36. << setfill(' ') << left << setw(13) << "Fitness Level" << endl;
  37.  
  38. while (!inFile.eof())
  39. {
  40. inFile >> fName[i] >> lName[i] >> age[i] >> sex[i] >> wTime[i]; // step 5
  41. i++;
  42. level = cal_Fitness_Level(age, sex, wTime);// step 6
  43. num++;
  44. }
  45.  
  46. sortData(lName, fName, age, sex, wTime, i);// step 7
  47.  
  48. for (i = 0; i < 9; i++)// step 8
  49. {
  50. outFile << setfill(' ') << left << setw(15) << lName[i]
  51. << setfill(' ') << left << setw(22) << fName[i]
  52. << setfill(' ') << left << setw(9) << age[i]
  53. << setfill(' ') << left << setw(7) << sex[i]
  54. << setfill(' ') << left << setw(11) << wTime[i]
  55. << setfill(' ') << left << setw(7) << level << endl;
  56. }
  57.  
  58. outFile << '\n' << "Number of records: " << num << endl;
  59.  
  60. return 0;
  61. }
  62.  
  63. int cal_Fitness_Level(int age[], char sex[], int wTime[])
  64. {
  65. int fL;
  66. int i = 0;
  67.  
  68. if (age[i] >= 13 && age[i] <= 19)
  69. {
  70. if (wTime[i] >= 48)
  71. fL = 1;
  72. else if (wTime[i] > 43 && wTime[i] <= 47)
  73. fL = 2;
  74. else if (wTime[i] > 39 && wTime[i] <= 43)
  75. fL = 3;
  76. else if (wTime[i] > 35 && wTime[i] <= 39)
  77. fL = 4;
  78. else if (wTime[i] < 35)
  79. fL = 5;
  80. }
  81. else
  82. fL = 0;
  83.  
  84. return fL;
  85. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Saint48198 is offline Offline
9 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Shell Sort w/ fibonacci numbers...HELP!!! Due Friday
Next Thread in C++ Forum Timeline: pointer conversion issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC