Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

%25253Cp%252520class%25253D%252522c1%252522%25253ECreate%252520a%252520program%2

ID: 3553779 • Letter: #

Question

%25253Cp%252520class%25253D%252522c1%252522%25253ECreate%252520a%252520program%252520for%252520this%252520assignment%252520in%252520the%252520file%25250A%25253Ctt%25253ESortAndSearchStrings.cpp%25253C%25252Ftt%25253E%25253C%25252Fp%25253E%25250A%25253Cp%252520class%25253D%252522c1%252522%25253E%25253Ctt%25253E%25253Cbr%252520%25252F%25253E%25253C%25252Ftt%25253E%25253C%25252Fp%25253E%25250A%25253Cp%252520class%25253D%252522c1%252522%25253ECreate%252520a%252520program%252520that%252520will%252520perform%252520the%252520following%25250Aoperations%25253A%25253C%25252Fp%25253E%25250A%25253Cul%252520class%25253D%252522c3%252522%25253E%25250A%25253Cli%252520class%25253D%252522c2%252522%25253ERead%252520a%252520series%252520of%252520names%252520from%252520the%252520user.%252520The%252520names%25250Ashould%252520be%252520stored%252520into%252520a%252520partially-filled%252520array%25252C%252520and%252520the%252520user%252520should%25250Abe%252520allowed%252520to%252520continue%252520entering%252520names%252520until%252520either%252520the%252520maximum%25250Aarray%252520size%252520is%252520exhausted%252520or%252520the%252520user%252520enters%252520a%252520blank%252520value.%252520The%25250Anumber%252520of%252520names%252520the%252520user%252520has%252520entered%252520will%252520be%252520used%252520as%252520the%252520%252522size%252522%252520of%25250Athe%252520array%252520from%252520this%252520point%252520forward.%252520%25253Ci%25253E%25253Csmall%25253EHint%25253A%252520Creating%252520a%25250Afunction%252520for%252520this%252520purpose%252520would%252520keep%252520your%252520code%25250Aclean...%25253C%25252Fsmall%25253E%25253C%25252Fi%25253E%25253C%25252Fli%25253E%25250A%25253Cli%252520class%25253D%252522c2%252522%25253ESort%252520the%252520array%252520of%252520names%252520so%252520that%252520they%252520are%252520in%25250Aalphabetical%252520order.%252520For%252520this%252520purpose%25252C%252520you%252520may%252520assume%252520that%252520all%252520names%25250Aeither%252520start%252520with%252520a%252520capital%252520letter%252520or%252520a%252520lowercase%252520letter%252520(that%252520is%25252C%25250Ayou%252520need%252520not%252520worry%252520about%252520mixed%252520case).%252520You%252520should%252520create%252520a%252520function%25250Athat%252520performs%252520%25253Ci%25253EBubble%252520Sort%25253C%25252Fi%25253E%252520for%252520this%252520purpose.%25253C%25252Fli%25253E%25250A%25253Cli%252520class%25253D%252522c2%252522%25253EIn%252520a%252520loop%25252C%252520allow%252520the%252520user%252520to%252520search%252520for%252520names%252520in%252520the%25250Alist.%252520The%252520user%252520should%252520be%252520allowed%252520to%252520enter%252520a%252520name%25252C%252520then%252520you%252520should%25250Adisplay%252520a%252520message%252520to%252520the%252520user%252520indicating%252520whether%252520or%252520not%252520the%252520name%25250Awas%252520found%252520in%252520the%252520array.%252520This%252520should%252520continue%252520until%252520the%252520user%252520chooses%25250Ato%252520stop%252520by%252520entering%252520a%252520blank%252520value%252520when%252520prompted%252520for%252520the%252520next%252520search%25250Atarget.%252520The%252520search%252520itself%252520should%252520be%252520performed%252520by%252520a%252520function%252520that%25250Aimplements%252520the%252526nbsp%25253B%25253Ci%25253EBinary%252520Search%25253C%25252Fi%25253E%252526nbsp%25253Balgorithm.%25253C%25252Fli%25253E%25250A%25253C%25252Ful%25253E%25250A%25253Ch3%252520class%25253D%252522c4%252522%25253E%25253Cspan%252520class%25253D%252522mw-headline%252522%25253EImplementation%25250ADetails%25253C%25252Fspan%25253E%25253C%25252Fh3%25253E%25250A%25253Cp%252520class%25253D%252522c1%252522%25253EIn%252520order%252520to%252520simplify%252520the%252520data%252520handling%252520in%252520this%25250Aprogram%25252C%252520you%252520are%252520encouraged%252520to%252520use%252520the%252526nbsp%25253B%25253Ccode%252520class%25253D%25250A%252522c5%252522%25253Estring%25253C%25252Fcode%25253E%252526nbsp%25253Btype%252520for%252520the%252520name%252520data.%25250AType%252526nbsp%25253B%25253Ccode%252520class%25253D%252522c5%252522%25253Estring%25253C%25252Fcode%25253E%252526nbsp%25253Bhas%252520the%252520advantages%252520of%25250Abeing%252520able%252520to%252520compare%252520directly%252520with%252520the%252520usual%252520relational%252520operators%25252C%25250Aand%252520being%252520able%252520to%252520assign%252520directly%252520using%252520the%252520normal%252520assignment%25250Aoperator.%25253C%25252Fp%25253E%25250A%25253Cp%25253E%25253Ca%252520name%25253D%252522Implementation_Details%252522%252520id%25253D%252522Implementation_Details%252522%25250Aclass%25253D%252522c6%252520mceItemAnchor%252522%25253E%25253C%25252Fa%25253E%25253C%25252Fp%25253E%25250A%25253Cp%252520class%25253D%252522c1%252522%25253EYou%252520should%252520allow%252520for%252520full%252520names%252520to%252520be%252520entered%25253B%252520that%25250Ais%25252C%252520you%252520must%252520plan%252520on%252520allowing%252520spaces%252520within%252520a%252520name.%252520This%252520means%252520that%25250Athe%252520stream%252520extraction%252520operator%252520('%25253Ccode%252520class%25253D%25250A%252522c5%252522%25253E%252526nbsp%25253B%252526gt%25253B%252526gt%25253B%252526nbsp%25253B%25253C%25252Fcode%25253E')%252520is%252520not%252520appropriate%252520for%252520this%25250Atask.%252520Instead%25252C%252520use%252520the%252526nbsp%25253B%25253Ccode%252520class%25253D%25250A%252522c5%252522%25253Estring%25253C%25252Fcode%25253E%252526nbsp%25253Bversion%252520of%252520the%252526nbsp%25253B%25253Ccode%252520class%25253D%25250A%252522c5%252522%25253Egetline()%25253C%25252Fcode%25253E%252526nbsp%25253Bfunction.%25253C%25252Fp%25253E%25250A

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 10;
int readNames(string names[], int maxSize)
{
int size = 0;
while (size < maxSize)
{
cout << "Enter a name(blank to end): ";
getline(cin, names[size]);
if (names[size] == "")
break;
size++;
}
return size;
}
void sortArray(string names[], int size)
{
for (int i = size - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (names[j] > names[j+1])
{
string tmp = names[j];
names[j] = names[j+1];
names[j+1] = tmp;
}
int binarySearch(string names[], int size, string val)
{
int left = 0;
int right = size - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (names[mid] == val)
return mid;
else if (names[mid] < val)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
int main()
{
string names[MAXSIZE];
string val;
int size = readNames(names, MAXSIZE);
sortArray(names, size);
do
{
val = "";
cout << "Enter a name to search(blank to quit): ";
getline(cin, val);
if (val != "")
{
int pos = binarySearch(names, size, val);
if (pos >= 0)
cout << val << " is in the array." << endl;
else
cout << val << " is not in the array." << endl;
}
} while (val != "");
return 0;
}