C++ Program. Please complete the following exercise. Use the array based applica
ID: 641922 • Letter: C
Question
C++ Program.
Please complete the following exercise. Use the array based application (located below) to build an equivalent STL Vector application. Keep in mind that a vector object can be passed to functions like any other object, it is not an array.
Sample Driver Output
Here are the unsorted names:
--------------------------
Collins, Bill
Smith, Bart
Allen, Jim,
Griffin, Jim
Stamey, Marty
Rose, Geri,
Taylor, Terri
Johnson, Jill,
Allison, Jeff
Looney, Joe
Wolfe, Bill,
James, Jean
Weaver, Jim
Pore, Bob,
Rutherford, Greg
Javens, Renee,
Harrison, Rose
Setzer, Cathy,
Pike, Gordon
Holland, Beth
Here are the names sorted:
--------------------------
Allen, Jim,
Allison, Jeff
Collins, Bill
Griffin, Jim
Harrison, Rose
Holland, Beth
James, Jean
Javens, Renee,
Johnson, Jill,
Looney, Joe
Pike, Gordon
Pore, Bob,
Rose, Geri,
Rutherford, Greg
Setzer, Cathy,
Smith, Bart
Stamey, Marty
Taylor, Terri
Weaver, Jim
Wolfe, Bill,
names.dat (file)
Collins, Bill
Smith, Bart
Allen, Jim,
Griffin, Jim
Stamey, Marty
Rose, Geri,
Taylor, Terri
Johnson, Jill,
Allison, Jeff
Looney, Joe
Wolfe, Bill,
James, Jean
Weaver, Jim
Pore, Bob,
Rutherford, Greg
Javens, Renee,
Harrison, Rose
Setzer, Cathy,
Pike, Gordon
Holland, Beth
Array based application (needs to be a STL vector application):
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Function prototypes
void selectionSort(string [], int);
void displayArray(string [], int);
void readNames(string[], int);
int main()
{
const int NUM_NAMES = 20;
string names[NUM_NAMES];
// Read the names from the file.
readNames(names, NUM_NAMES);
// Display the unsorted array.
cout << "Here are the unsorted names: ";
cout << "-------------------------- ";
displayArray(names, NUM_NAMES);
// Sort the array.
selectionSort(names, NUM_NAMES);
// Display the sorted array.
cout << " Here are the names sorted: ";
cout << "-------------------------- ";
displayArray(names, NUM_NAMES);
system ("Pause");
return 0;
}
// ********************************************************
// The selectionSort function performs an ascending order *
// selection sort on an array of strings. The size *
// parameter is the number of elements in the array. *
// ********************************************************
void selectionSort(string values[], int size)
{
int startScan;
int minIndex;
string minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = values[minIndex];
for(int index = startScan + 1; index < size; index++)
{
if (values[index] < minValue)
{
minValue = values[index];
minIndex = index;
}
}
values[minIndex] = values[startScan];
values[startScan] = minValue;
}
}
// ********************************************************
// The displayArray function displays the contents of *
// the array. *
// ********************************************************
void displayArray(string values[], int size)
{
for (int i = 0; i < size; i++)
cout << values[i] << endl;
}
// ********************************************************
// The readNames function reads the contents of the *
// "names.dat" file into the array. *
// ********************************************************
void readNames(string values[], int size)
{
int index = 0; // Array index
// Open the file.
ifstream inFile;
inFile.open("names.dat");
// Test that the file was opened.
if (!inFile)
{
cout << "Error opening names.dat ";
exit(0);
}
// Read the names from the file into the array.
while (index < size)
{
// Get a line from the file.
getline(inFile, values[index]);
// Increment index.
index++;
}
// Close the file.
inFile.close();
}
Explanation / Answer
#include
#include
using namespace std;
void getFileContent(const char* file,vector &vect)
{
std::ifstream fs(file);
std::string line;
while(std::getline(fs, line)
{
if(line == "X-Mesh")
{
continue;
}
vect.push_back(split(line));
}
}
void displayNameVector(vector vect)
{
for(int i=0;i cout< }
}
int main(){
vector names;
getFileContent(names.dat,names);
cout< cout<<"Descending order of the names: "< //sorting in descending order
sort(names.begin(),names.end());
displayNameVector(names);
cout< cout<<"Ascending order of the names: "< //sorting in ascending order
sort(names.end(),names.begin());
displayNameVector(names);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.