This lab is to give you more experience with C++ Searching and Sorting Arrays Gi
ID: 3634876 • Letter: T
Question
This lab is to give you more experience with C++ Searching and Sorting Arrays
Given two arrays shown later rewrite the following functions:
1. Linear Search
Modify the searchList function given below so that it searches for a given name rather than an int. The functions returns and int which is the index of the name found. If -1 is returned then say name is not found otherwise write out the name and the mark for that name.
int searchList(int list[], int numElems, int 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
2. Selection Sort
Modify the selectionSort given below so that it sorts by name instead of an int. Be sure to accept both arrays for sorting purposes. Write out the arrays before and after sorting.
void selectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
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;
}
}
3. Binary Search
Modify the binarySearch function given below so that it searches for a given name rather than an int. The functions returns and int which is the index of the name found. If -1 is returned then say name is not found otherwise write out the name and the mark for that name.
int binarySearch(int array[], int size, int value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
4. Display Data
Write a function to read and display the contents of names and marks. You then ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.
Next, sort the arrays, write them out and then ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student’s name and mark if found.
void getNames(ifstream& inStream, string names[], int marks[], int numElts);
int linearSearch(const string names[], int numElts,string who);
int binarySearch(const string names[], int numElts,string who);
void selectionSort(string names[], int marks[],int numElts);
void displayData(const string names[], const int marks[], int numElts);
int main()
{
string names[NUM_NAMES];
int marks[NUM_NAMES];
ifstream inStream;
int index;
string searchWho;
File for names and marks:
Collins,Bill 80
Smith,Bart 75
Allen,Jim 82
Griffin,Jim 55
Stamey,Marty 90
Rose,Geri 78
Taylor,Terri 56
Johnson,Jill 77
Allison,Jeff 45
Looney,Joe 89
Wolfe,Bill 63
James,Jean 72
Weaver,Jim 77
Pore,Bob 91
Rutherford,Greg 42
Javens,Renee 74
Harrison,Rose 58
Setzer,Cathy 93
Pike,Gordon 48
Holland,Beth 79
plz dont copy paste it from the internet, or i wont rate
Explanation / 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.