- /*
-
Write a program that reads names and gpas from a text file. The file looks like:
-
-
James 3.9
-
Margaret 3.5
-
Charles 1.2
-
Jennifer 4.0
-
-
Your program sorts the students ascending by gpa, and prints the sorted list including names.
-
To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers.
-
Sort the numbers. Whenever you swap 2 numbers also swap their matching names in the names array.
-
Include a structure intt as in the model lab.
-
-
*/
-
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <vector>
-
using std::string;
-
using namespace std;
-
-
void sort(vector<double>(gpaAry), vector<string>(names), int size);
-
int smallest(vector<double>(gpaAry), int first, int last);
-
void swap(vector<double>(gpaAry), vector<string>(names), int i, int j);
-
-
-
int main(){
-
-
ifstream in ("gpa.txt");
-
if(!in.is_open ()){ // if file did not open die.
-
cout << "Failed to open.\n " << endl;
-
cout << "Now exiting...\n ";
-
exit(-1);
-
}
-
-
string name;
-
double gpa;
-
vector<string>(names);
-
int j = 0;
-
vector<double>(gpaAry);
-
-
while(in>>name>>gpa){
-
names.push_back(name);
-
gpaAry.push_back(gpa);
-
j++;
-
}
-
-
// printing the values
-
for(int i = 0; i<4; i++){
-
sort(gpaAry,names,4);
-
-
cout<<names.at(i)<<" "<<gpaAry.at(i)<<endl;
-
}
-
-
return 0;
-
}
-
-
-
void sort(vector<double>(gpaAry), vector<string>(names), int size){
-
-
for (int i = 0; i < size-1; i++){
-
int j = smallest(gpaAry,i,size-1);
-
swap(gpaAry,names,i,j);
-
}
-
}
-
-
int smallest(vector<double>(gpaAry), int first, int last){
-
// returns index of cell with smallest value in arr[first .. last]
-
-
int small = first;
-
for (int i = first + 1; i<= last; i++){
-
if (gpaAry.at(i) < gpaAry.at(small)){
-
small = i;
-
}
-
}
-
return small;
-
}
-
-
void swap(vector<double>(gpaAry), vector<string>(names), int i, int j){
-
// swaps values in cells i and j
-
-
double temp = gpaAry.at(i);
-
string tempname = names.at(i);
-
gpaAry.at(i) = gpaAry.at(j);
-
names.at(i) = names.at(j);
-
gpaAry.at(j) = temp;
-
names.at(j) = tempname;
-
}
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?
4 2025
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: - vector<string>(names);
-
vector<double>(gpaAry);
It looks like you are declaring the variables, but usually this is done like this: - vector<string> names;
-
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.
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: - vector<string>(names);
-
vector<double>(gpaAry);
It looks like you are declaring the variables, but usually this is done like this: - vector<string> names;
-
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
- #include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <vector>
-
using std::string;
-
using namespace std;
-
-
void printvector(vector<double>gpaAry, vector<string>names, int size);
-
void sort(vector<double> & gpaAry, vector<string> & names, int size);
-
int smallest(vector<double>gpaAry, int start, int size);
-
-
-
-
int main(){
-
-
ifstream in ("gpa.txt");
-
if(!in.is_open ()){ // if file did not open die.
-
cout << "Failed to open.\n " << endl;
-
cout << "Now exiting...\n ";
-
exit(-1);
-
}
-
-
string name;
-
double gpa;
-
vector<string>names;
-
vector<double>gpaAry;
-
-
while(!in.eof()){
-
in >> name >> gpa;
-
gpaAry.push_back(gpa);
-
names.push_back(name);
-
}
-
-
-
sort(gpaAry,names,4);
-
printvector(gpaAry,names,4);
-
in.close();
-
-
return 0;
-
}
-
-
void printvector(vector<double>gpaAry, vector<string>names, int size){
-
for(int i = 0; i < size; i++){
-
cout << names[i] << " " << gpaAry[i] <<endl;
-
}
-
cout << endl;
-
}
-
-
void sort(vector<double>& gpaAry, vector<string> & names, int size){
-
-
for (int i = 0; i < size; i++){
-
int j = smallest(gpaAry,i,size);
-
-
double temp = gpaAry[i];
-
gpaAry[i] = gpaAry[j];
-
gpaAry[j] = temp;
-
-
string tempname = names[i];
-
names[i] = names[j];
-
names[j] = tempname;
-
}
-
}
-
-
int smallest(vector<double>gpaAry, int start, int size){
-
int small = start;
-
for(int i = start+1; i < size; i++){
-
if(gpaAry[i] < gpaAry[small]){
-
small = i;
-
}
-
}
-
return small;
-
}
- //Charles 1.2
-
//Margaret 3.5
-
//James 3.9
-
//Jennifer 4
-
//
-
//Press any key to continue . . .
i answered my own question.
but thanks for the help
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: -
sort(myvector.begin(), myvector.end(), mycompare);
-
and you are done.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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),
|
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...
|
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...
|
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:
...
|
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
|
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...
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |