In: Computer Science
Array based 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:\n";
cout << "--------------------------\n";
displayArray(names, NUM_NAMES);
// Sort the array.
selectionSort(names, NUM_NAMES);
// Display the sorted array.
cout << "\nHere are the names sorted:\n";
cout << "--------------------------\n";
displayArray(names, NUM_NAMES);
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\n";
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();
Program 1:
Use the array based application above 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.
Use the following as the input data for the 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
Hi
I have updated the code vector and it is working fine.
#include <iostream>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
// Function prototypes
void selectionSort(vector<string> &, int);
void displayArray(vector<string> &, int);
void readNames(vector<string> &);
int main()
{
vector<string> names;
// Read the names from the file.
readNames(names);
// Display the unsorted array.
cout << "Here are the unsorted names:\n";
cout << "--------------------------\n";
displayArray(names, names.size());
// Sort the array.
selectionSort(names, names.size());
// Display the sorted array.
cout << "\nHere are the names sorted:\n";
cout << "--------------------------\n";
displayArray(names, names.size());
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(vector<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(vector<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(vector<string>& values)
{
// Open the file.
ifstream inFile;
inFile.open("names.dat");
string name;
// Test that the file was opened.
if (!inFile)
{
cout << "Error opening names.dat\n";
exit(0);
}
// Read the names from the file into the array.
while (!inFile.eof())
{
// Get a line from the file.
getline(inFile,name);
values.push_back(name);
// Increment index.
}
// Close the file.
inFile.close();
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
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,