%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22
ID: 3553722 • Letter: #
Question
%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3EGiven%20two%20arrays%20shown%20later%20rewrite%20the%20following%0Afunctions%20to%20form%20one%20complete%20program.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3E%20%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c5%22%3E%3Cb%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E1.%3Cspan%20class%3D%0A%22c2%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2Fspan%3E%3C%2Fspan%3E%3C%2Fb%3E%0A%3Cb%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c4%22%20xml%3Alang%3D%22EN-US%22%3ELinear%0ASearch%3C%2Fspan%3E%3C%2Fb%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3EModify%20the%20searchList%20function%20given%20below%20so%20that%20it%0Asearches%20for%20a%20given%20name%20rather%20than%20an%20int.%20The%20functions%20returns%0Aand%20int%20which%20is%20the%20index%20of%20the%20name%20found.%20If%20-1%20is%20returned%0Athen%20say%20name%20is%20not%20found%20otherwise%20write%20out%20the%20name%20and%20the%0Amark%20for%20that%20name.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3Eint%20searchList(int%20list%5B%5D%2C%20int%20numElems%2C%20int%20value)%3Cbr%20%2F%3E%0A%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20int%20index%20%3D%200%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20Used%0Aas%20a%20subscript%20to%20search%20array%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20int%20position%20%3D%20-1%3B%26nbsp%3B%20%2F%2F%20To%20record%20position%20of%0Asearch%20value%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20bool%20found%20%3D%20false%3B%20%2F%2F%20Flag%20to%20indicate%20if%20value%20was%0Afound%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20while%20(index%20%26lt%3B%20numElems%20%26amp%3B%26amp%3B%20!found)%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(list%5Bindex%5D%20%3D%3D%20value)%20%2F%2F%20If%20the%0Avalue%20is%20found%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20found%20%3D%20true%3B%20%2F%2F%0ASet%20the%20flag%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20position%20%3D%20index%3B%0A%2F%2F%20Record%20the%20value's%20subscript%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20index%2B%2B%3B%20%2F%2F%20Go%20to%20the%20next%0Aelement%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20%7D%3Cbr%20%2F%3E%0Areturn%20position%3B%20%2F%2F%20Return%20the%20position%2C%20or%20-1%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%20c5%22%3E%3Cb%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c3%22%20xml%3Alang%3D%0A%22EN-US%22%3E2.%3Cspan%20class%3D%0A%22c2%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2Fspan%3E%3C%2Fspan%3E%3C%2Fb%3E%0A%3Cb%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c4%22%20xml%3Alang%3D%22EN-US%22%3ESelection%0ASort%3C%2Fspan%3E%3C%2Fb%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3EModify%20the%20selectionSort%20given%20below%20so%20that%20it%20sorts%20by%0Aname%20instead%20of%20an%20int.%20Be%20sure%20to%20accept%20both%20arrays%20for%20sorting%0Apurposes.%20Write%20out%20the%20arrays%20before%20and%20after%20sorting.%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3E%26nbsp%3B%3C%2Fspan%3E%3C%2Fp%3E%0A%3Cp%20class%3D%22MsoNormal%22%3E%3Cspan%20lang%3D%22EN-US%22%20class%3D%22c1%22%20xml%3Alang%3D%0A%22EN-US%22%3Evoid%20selectionSort(int%20array%5B%5D%2C%20int%20size)%3Cbr%20%2F%3E%0A%26nbsp%3B%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20int%20startScan%2C%20minIndex%2C%20minValue%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20for%20(startScan%20%3D%200%3B%20startScan%20%26lt%3B%20(size%20-%201)%3B%0AstartScan%2B%2B)%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20minIndex%20%3D%20startScan%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20minValue%20%3D%0Aarray%5BstartScan%5D%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20for(int%20index%20%3D%20startScan%20%2B%201%3B%0Aindex%20%26lt%3B%20size%3B%20index%2B%2B)%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%0A(array%5Bindex%5D%20%26lt%3B%20minValue)%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%0AminValue%20%3D%20array%5Bindex%5D%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%0AminIndex%20%3D%20index%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20array%5BminIndex%5D%20%3D%0Aarray%5BstartScan%5D%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20array%5BstartScan%5D%20%3D%0AminValue%3B%3Cbr%20%2F%3E%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3Cbr%20%2F%3E%0A%26nbsp%3B%7D%3C%2Fspan%3E%3C%2Fp%3E%0AExplanation / Answer
3.
#include<iostream>
#include<string>
using namespace std;
int searchList(string list[], int numElems, string value)
{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position; // Return the position, or -1
}
void selectionSort(string array[], int size)
{
int startScan, minIndex;
string minValue;
cout<<"the unsorted array is ";
for(int i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
cout<<"The sorted array is ";
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
for(int i=0;i<size;i++)
{
cout<<array[i]<<" ";
} cout<<endl;
}
int main()
{
int n;
cout<<"Enter size of the array ";
cin>>n;
string a[n];
for(int i=0;i<n;i++)
{
cout<<"enter string "<<i<<endl;
cin>>a[i];
}
string s;
cout<<"Enter string to be searched "<<endl;
cin>>s;
int p=searchList(a,n,s);
if(p==-1)
cout<<"string not found "<<endl;
else
cout<<"string found at position "<<p<<endl;
selectionSort(a,n);
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.