i don't want you to do it for me but i don't know where i went wrong.
can someone plz guide me through how to finish this without getting errors.
this is the original 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.
insert
Code:
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.
insert
Code:
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.
insert
Code:
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;
this is what i have insert
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int classsize = 20;
string names[classsize];
int marks[classsize];
int count;
int displayData(string [],int [],int);
void binarySearch(string [],int [],int,string);
void selectionSortnames(string [],int [],int);
int selectionSortmarks(int [],int);
int main()
{
string names[classsize] = {
"Bill Collins", "Bart Smith", "Jim Allen", "Jim Griffen", "Marty Stamey", "Geri Rose", "Terri Taylor", "Jill Johnson", "Jeff Allison", "Joe Looney", "Bill Wolfe", "Jean James", "Jim Weaver", "Bob Pore", "Greg Rutherford", "Renee Javens", "Rose Harrison", "Cathy Setzer", "Gordon Pike", "Beth Holland"};
int marks[classsize] = {80,75,82,55,90,78,56,77,45,89,63,72,77,91,42,74,58,93,48,79};
selectionSortnames(names,marks,classsize);
}
void selectionSortnames(string names[],int marks[],int class_size)
{
int startScan;
int minIndex;
string minValue;
int minValue2;
displayData(names,marks,count);
for (startScan = 0; startScan < (classsize - 1); startScan++)
{
minIndex = startScan;
minValue = names[startScan];
for(int index = startScan + 1; index < classsize; index++)
{
if (names[index] < minValue)
{
minValue = names[index];
minIndex = index;
}
}
names[minIndex] = names[startScan];
names[startScan] = minValue;
}
for (startScan = 0; startScan < (classsize - 1); startScan++)
{
minIndex = startScan;
minValue2 = marks[startScan];
for(int index = startScan + 1; index < classsize; index++)
{
if (marks[index] < minValue2)
{
minValue2 = marks[index];
minIndex = index;
}
}
marks[minIndex] = marks[startScan];
marks[startScan] = minValue2;
}
displayData(names,marks,count);
}