Question

In C++ Create a program that uses only Selection Sort and Insertion Sort for the National...

In C++

Create a program that uses only Selection Sort and Insertion Sort for the National Football League list of current players

Inputting data from a text file named "NFLplayers.txt"

Only want the name of the player(first name then last name), their team name, and position they play.

Example is: Patrick Mahomes, Chiefs, Quarterback

Output lists to a text file along with how long it took to go through the Selection Sort and Insertion Sort and how many iterations it took for each

Thanks in advance.

Homework Answers

Answer #1

#include <iostream>
#include <string>
using namespace std;

int main(){
// String holding each name
string word;

// Temp value
string temp;

// Empty vector holding all names from file
vector<string> names;

// Read names from file LineUp.txt
ifstream in(NFLplayers.txt);
if(in.is_good()){
while(in.good()){
getline(in, word);
names.push_back(word);
}
in.close();
}else{
cout << "Unable to open file";
}

int size = names.size();

int i;

// Loop to sort vector name values
for( i = 1 ; i < size ; ++i){
// new value to be inserted into temp location
temp = names.at(i);

// index to the left of i
int k;

// sort names
for(k = i-1; k>=0 && names.at(k) > temp; k--){
names.at(k+1) = names.at(k);
}
names.at(k+1) = temp;
}

// Loop to print names
for (i = 0; i < size; i++)
cout << names.at(i) << endl;

return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions