473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sorting with vectors

Expand|Select|Wrap|Line Numbers
  1. /*
  2. Write a program that reads names and gpas from a text file.  The file looks like:
  3.  
  4. James        3.9
  5. Margaret     3.5
  6. Charles         1.2
  7. Jennifer    4.0
  8.  
  9. Your program sorts the students ascending by gpa, and prints the sorted list including names.
  10. To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers.  
  11. Sort the numbers.  Whenever you swap 2 numbers also swap their matching names in the names array.  
  12. Include a structure intt as in the model lab. 
  13.  
  14. */
  15.  
  16. #include <iostream>
  17. #include <fstream>
  18. #include <string>
  19. #include <vector>
  20. using std::string;
  21. using namespace std;
  22.  
  23. void sort(vector<double>(gpaAry), vector<string>(names), int size);
  24. int smallest(vector<double>(gpaAry), int first, int last);
  25. void swap(vector<double>(gpaAry), vector<string>(names), int i, int j);
  26.  
  27.  
  28. int main(){
  29.  
  30.     ifstream in ("gpa.txt");
  31.     if(!in.is_open ()){ // if file did not open die.
  32.         cout << "Failed to open.\n " << endl;
  33.         cout << "Now exiting...\n ";
  34.         exit(-1);
  35.     }
  36.  
  37.     string name;
  38.     double gpa;
  39.     vector<string>(names);    
  40.     int j = 0;
  41.     vector<double>(gpaAry);
  42.  
  43.     while(in>>name>>gpa){
  44.         names.push_back(name);
  45.         gpaAry.push_back(gpa);
  46.         j++;
  47.     }
  48.  
  49.     // printing the values 
  50.     for(int i = 0; i<4; i++){
  51.         sort(gpaAry,names,4);
  52.  
  53.         cout<<names.at(i)<<"  "<<gpaAry.at(i)<<endl;
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59.  
  60. void sort(vector<double>(gpaAry), vector<string>(names), int size){
  61.  
  62.     for (int i = 0; i < size-1; i++){
  63.         int j = smallest(gpaAry,i,size-1);
  64.         swap(gpaAry,names,i,j);
  65.     }
  66. }
  67.  
  68. int smallest(vector<double>(gpaAry), int first, int last){
  69. // returns index of cell with smallest value in arr[first .. last]
  70.  
  71.     int small = first;
  72.     for (int i = first + 1; i<= last; i++){
  73.         if (gpaAry.at(i) < gpaAry.at(small)){
  74.             small = i;
  75.         }
  76.     }
  77.     return small;
  78. }
  79.  
  80. void swap(vector<double>(gpaAry), vector<string>(names), int i, int j){
  81. // swaps values in cells i and j
  82.  
  83.     double temp = gpaAry.at(i);
  84.     string tempname = names.at(i);
  85.     gpaAry.at(i) = gpaAry.at(j);
  86.     names.at(i) = names.at(j);
  87.     gpaAry.at(j) = temp;
  88.     names.at(j) = tempname;
  89. }

my problem is in my sort function. but i cant figure it out...
if i use arrays this works but obviously vectors are different.
help?
Mar 16 '08 #1
4 2025
Ganon11
3,652 Expert 2GB
I would use a struct or a class to store the name and GPA in the same container. That way, you are passing around and swapping within a single vector, rather than two.

I also have no idea what you are doing with the statements:

Expand|Select|Wrap|Line Numbers
  1. vector<string>(names);
  2. vector<double>(gpaAry);
It looks like you are declaring the variables, but usually this is done like this:

Expand|Select|Wrap|Line Numbers
  1. vector<string> names;
  2. vector<double> gpaAry;
The same thing happens in your functions. I don't know if this causes your problems, but it impacts readability. Using structs will simplify matters significantly.
Mar 16 '08 #2
I would use a struct or a class to store the name and GPA in the same container. That way, you are passing around and swapping within a single vector, rather than two.

I also have no idea what you are doing with the statements:

Expand|Select|Wrap|Line Numbers
  1. vector<string>(names);
  2. vector<double>(gpaAry);
It looks like you are declaring the variables, but usually this is done like this:

Expand|Select|Wrap|Line Numbers
  1. vector<string> names;
  2. vector<double> gpaAry;
The same thing happens in your functions. I don't know if this causes your problems, but it impacts readability. Using structs will simplify matters significantly.

the assignment was to use two vectors.
also i fixed the declarations but to no avail.

in class we haven't learned structs and we are just now learning classes.
so im supposed to figure this out but doing it like this...

ps. thanks for quick reply
Mar 16 '08 #3
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using std::string;
  6. using namespace std;
  7.  
  8. void printvector(vector<double>gpaAry, vector<string>names, int size);
  9. void sort(vector<double> & gpaAry, vector<string> & names, int size);
  10. int smallest(vector<double>gpaAry, int start, int size);
  11.  
  12.  
  13.  
  14. int main(){
  15.  
  16.     ifstream in ("gpa.txt");
  17.     if(!in.is_open ()){ // if file did not open die.
  18.         cout << "Failed to open.\n " << endl;
  19.         cout << "Now exiting...\n ";
  20.         exit(-1);
  21.     }
  22.  
  23.     string name;
  24.     double gpa;
  25.     vector<string>names;    
  26.     vector<double>gpaAry;
  27.  
  28.     while(!in.eof()){
  29.         in >> name >> gpa;
  30.         gpaAry.push_back(gpa);
  31.         names.push_back(name);
  32.     }
  33.  
  34.  
  35.     sort(gpaAry,names,4);
  36.     printvector(gpaAry,names,4);
  37.     in.close();
  38.  
  39.     return 0;
  40. }
  41.  
  42. void printvector(vector<double>gpaAry, vector<string>names, int size){
  43.     for(int i = 0; i < size; i++){
  44.         cout << names[i] << " " << gpaAry[i] <<endl;
  45.     }
  46.     cout << endl;
  47. }
  48.  
  49. void sort(vector<double>& gpaAry, vector<string> & names, int size){
  50.  
  51.     for (int i = 0; i < size; i++){
  52.         int j = smallest(gpaAry,i,size);
  53.  
  54.         double temp = gpaAry[i];
  55.         gpaAry[i] = gpaAry[j];
  56.         gpaAry[j] = temp;
  57.  
  58.         string tempname = names[i];
  59.         names[i] = names[j];
  60.         names[j] = tempname;
  61.     }
  62. }
  63.  
  64. int smallest(vector<double>gpaAry, int start, int size){
  65.     int small = start;
  66.     for(int i = start+1; i < size; i++){
  67.         if(gpaAry[i] < gpaAry[small]){
  68.             small = i;
  69.         }
  70.     }
  71.     return small;
  72. }
Expand|Select|Wrap|Line Numbers
  1. //Charles 1.2
  2. //Margaret 3.5
  3. //James 3.9
  4. //Jennifer 4
  5. //
  6. //Press any key to continue . . .
i answered my own question.
but thanks for the help
Mar 16 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Just a second, there.

Is your class assignment to sort a vector or to write a sort?

If is it to sort a vector, then use the STL sort. You just need to write a binary predicate to use on the sort call:

Expand|Select|Wrap|Line Numbers
  1. sort(myvector.begin(), myvector.end(), mycompare);
  2.  
and you are done.
Mar 16 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
0
by: hypernihl | last post by:
Hello, I have two vectors "vector<int> x,y;" I want to sort vector x "sort(x.begin(),x.end());" I want to sort vector y with respect to x. I need the new index of vector x after it has been...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
13
by: JoeC | last post by:
I am completly lost. I would like to create a function that takes two vectors. These two vectors have objects with x and y coords, what I want to do find all the objects in the same x and y...
3
by: Pino | last post by:
HI all, I am learning c++ ( just 1 month) and would want an aid for a code. I have a integer vector of length N. ie 3 3 3 4 4 1
5
by: xtheendx | last post by:
In my program i have three sperate vectors that store the students last name, first and score. I need to sort it by last name and print the contents of the vectors lastname, firstname: score. Well...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.