C++ Help: Sorting & Searching String Arrays
I am taking an Intro C++ class, and am having difficulties with the following assignment.
Any assistance would be most appreciated! Thanks!
ASSIGNMENT:
Modify the binary search function so it searches an array of strings
instead of integers. Use the following as a skeleton to complete the program:
string names[20] = { "Collins, Bill",
"Smith, Bart",
"Allen, Jim",
"Griffin, Jim",
"Stamey, Martin",
"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", };
MY CODE:
#include <iostream>
#include <iomanip>
# include <cmath>
using namespace std;
int main()
{
string names[20] = { "Collins, Bill",
"Smith, Bart",
"Allen, Jim",
"Griffin, Jim",
"Stamey, Martin",
"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", };
cout << "The unsorted names are " << endl;
showArray(names, 20);
selectionSort(names, 20);
cout << "The sorted names are " << endl;
showArray(name, 20);
return 0;
}
//
void sortArray(string names[], int elems)
{
string temp;
bool swap;
do
{ swap = false;
for (int count = 0; count < (elems - 1); count++)
{
if (names[count] > names[count +1])
{
temp = names[count];
names[count] = names[count + 1];
names[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void selectionSort(string names[], int elems)
{
int startScan; //I know this must be a string, do not know how to convert.
int minIndex, maxIndex;
for (startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
minValue = names[startScan];
for (int index = startScan + 1; index < elems; index++)
{
if (names[index] < minValue)
{
minValue = names[index];
minIndex = index;
}
}
names[minIndex] = names[startScan];
names[startScan] = minValue;
}
}
void showArray(string names[], int elems)
{
for (int count = 0; count < elems; count++)
cout << names[count] << " " << endl;
}
****Obviously, there are a lot of errors.****
Any assistance would be most appreciated! Thanks!
ASSIGNMENT:
Modify the binary search function so it searches an array of strings
instead of integers. Use the following as a skeleton to complete the program:
string names[20] = { "Collins, Bill",
"Smith, Bart",
"Allen, Jim",
"Griffin, Jim",
"Stamey, Martin",
"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", };
MY CODE:
#include <iostream>
#include <iomanip>
# include <cmath>
using namespace std;
int main()
{
string names[20] = { "Collins, Bill",
"Smith, Bart",
"Allen, Jim",
"Griffin, Jim",
"Stamey, Martin",
"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", };
cout << "The unsorted names are " << endl;
showArray(names, 20);
selectionSort(names, 20);
cout << "The sorted names are " << endl;
showArray(name, 20);
return 0;
}
//
void sortArray(string names[], int elems)
{
string temp;
bool swap;
do
{ swap = false;
for (int count = 0; count < (elems - 1); count++)
{
if (names[count] > names[count +1])
{
temp = names[count];
names[count] = names[count + 1];
names[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void selectionSort(string names[], int elems)
{
int startScan; //I know this must be a string, do not know how to convert.
int minIndex, maxIndex;
for (startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
minValue = names[startScan];
for (int index = startScan + 1; index < elems; index++)
{
if (names[index] < minValue)
{
minValue = names[index];
minIndex = index;
}
}
names[minIndex] = names[startScan];
names[startScan] = minValue;
}
}
void showArray(string names[], int elems)
{
for (int count = 0; count < elems; count++)
cout << names[count] << " " << endl;
}
****Obviously, there are a lot of errors.****
0
Comments
#include <iostream>
#include <string>
using namespace std;
void selectionSort(string [], int);
void showArray(string [], int);
int main()
{
const int SIZE = 20;
string name[SIZE] =
{ "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
"Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
"Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
"Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
"Moretti, Bella", "Wu, Hong", "Patel, Renee",
"Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
"Kelly, Sean", "Holland, Beth" };
cout << "The unsorted string is: \n";
showArray(name, SIZE);
selectionSort(name, SIZE);
cout << "The sorted string is: \n";
showArray(name, SIZE);
return 0;
}
//*******selectionSort****************************************//
void selectionSort(string name[], int elems)
{
int startScan, minIndex;
string strName;
for (startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
strName = name[startScan];
for(int index = startScan + 1; index < elems; index++)
{
if (name[index] < strName)
{
strName = name[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
name[startScan] = strName;
}
}
//*******showArray*********************************** //
void showArray(string name[], int elems)
{
for (int count = 0; count < elems; count++)
cout << name[count] << " "<<endl;
cout << endl;
}