[CODE=cpp]/*
Write a program that reads names and gpa’s 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<dou ble>(gpaAry), vector<string>( names), int size);
int smallest(vector <double>(gpaAry ), int first, int last);
void swap(vector<dou ble>(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_bac k(gpa);
j++;
}
// printing the values
for(int i = 0; i<4; i++){
sort(gpaAry,nam es,4);
cout<<names.at( i)<<" "<<gpaAry.at(i) <<endl;
}
return 0;
}
void sort(vector<dou ble>(gpaAry), vector<string>( names), int size){
for (int i = 0; i < size-1; i++){
int j = smallest(gpaAry ,i,size-1);
swap(gpaAry,nam es,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<dou ble>(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;
}[/CODE]
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?
Write a program that reads names and gpa’s 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<dou ble>(gpaAry), vector<string>( names), int size);
int smallest(vector <double>(gpaAry ), int first, int last);
void swap(vector<dou ble>(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_bac k(gpa);
j++;
}
// printing the values
for(int i = 0; i<4; i++){
sort(gpaAry,nam es,4);
cout<<names.at( i)<<" "<<gpaAry.at(i) <<endl;
}
return 0;
}
void sort(vector<dou ble>(gpaAry), vector<string>( names), int size){
for (int i = 0; i < size-1; i++){
int j = smallest(gpaAry ,i,size-1);
swap(gpaAry,nam es,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<dou ble>(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;
}[/CODE]
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?
Comment