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

i am having issues with the \"Comparisons\" being dispayed correctly the 1st pic

ID: 3625612 • Letter: I

Question

i am having issues with the "Comparisons" being dispayed correctly

the 1st picture shows the correct output


here is my code so far any help would be great


//lab12 search benchmarks


#include
#include
#include
#include
using namespace std;
// constant declerations
const int listSize = 511 ;
const int maxTestVals = 50 ;
const unsigned int defaultSeed = 15199415 ;
// funtion prototypes
void PrintNameHeader();
void FillMainList(int list[]);
void FillTestArray(const int list[], int test[]);
void PrintArray(const int ary[], int size);
void PrintTestArray(const int test[], int maxTestVals);
void SelectionSort(int list[], int maxTestVals);
int LinearSearch(const int list[], int value, int& comps);
int BinarySearch(const int list[], int value, int& comps);
void search1(const int list[], const int test[]);
int main ()
{
int list[listSize];
int test[maxTestVals];
// int comps;
cout << fixed << showpoint << setprecision(1) ;
PrintNameHeader();
char choice;
cout << "Do you want to use random values (versus default values)? [y/n]" << endl;
cin >> choice ;
if(choice == 'Y' || choice == 'y')
{
srand(static_cast(time(0))) ;
}
else
srand (defaultSeed);
cout << "The random number seed value is: " << defaultSeed << endl;
FillMainList(list);
cout<<"Elements In Main list"< cout< PrintArray(list, listSize);
cout< FillTestArray(list, test);
cout< cout<<"Elements In Test Array:"<

PrintArray( test, maxTestVals);
cout< cout<

search1( list, test);
cout< cout< SelectionSort(list, listSize);
cout< cout << "Sorted array" << endl;
PrintArray(list, listSize);
cout << endl << endl;
PrintArray( test, maxTestVals);
cout << endl << endl;
search1( list, test);
return 0;
}
void PrintNameHeader()
{
// displays my name header on the screen

}
void FillMainList(int list[])
{
bool found;
int index = 0;
while (index < listSize)
{
list[index] = rand() % 5000;
found = false;
for(int idx = 0; idx < index && !found; idx++)
{
if(list[index] == list[idx])
found = true;
}
if (!found)
{
index++;
}
}
}

void FillTestArray(const int list[], int test[])
{
int index0;
int index1 = 0;
bool found;

for(index0 = 0; index0 < listSize && index0 < maxTestVals ; index0++)
{
test[index0] = list[index1];
index1+=12;
}

while( index0 < maxTestVals)
{
test[index0] = rand() % 5000;
found = false;
for( index0=index0 ; index0 < maxTestVals && !found; index0++)
{
if(test[index0] == test[index0])
found = true;
}
if (!found)
{
index0++;
}

}
}


void PrintArray(const int list[], int listSize)
{
for (int index = 0; index < listSize; index++)
{
cout << setw(6) << right << list[index];
if(index %12 == 11)
{
cout << endl;
}
}
}

void SelectionSort(int list[], int listSize)
{
int startScan;
int minIndex;
int minVal;
for(startScan = 0; startScan < listSize; startScan++ )
{
minIndex = startScan;
minVal = list[startScan];
for (int index = startScan + 1; index < listSize; index++)
{
if (list[index] < minVal)
{
minVal = list[index];
minIndex = index ;
}
}
list[minIndex] = list[startScan];
list[startScan] = minVal;
}
}
int LinearSearch(const int list[], int value, int& comps)
{
int index ;
int position = -1;
bool found = false ;
for (index = 0; index < listSize ; index++)
{
comps++;
if (list[listSize] == value)
{
return index;
}
}
return -1;
}
int BinarySearch(const int list[], int value, int& comps)
{
int first = 0;
int last = listSize-1;
int middle ;
int count = 0;
while (first <= last)
{
middle = (first + last) / 2 ;
comps++ ;
if (list[middle] == value )
{
return middle;
}
else if (list[middle] > value )
last = middle -1;
else
first = middle + 1 ;
}
return -1;
}
void search1(const int list[], const int test[])
{
int index = 0;
cout << "Test Value"<< setw(12) << "Result" << setw(12) << "Comparisons" << endl;
cout << "----------"<< setw(12) << "------" << setw(12) << "-----------" << endl;
while( index < maxTestVals )
{

int comps = 0;
LinearSearch(list, test[index], comps);
cout < index++;
}






}

Explanation / Answer

please rate - thanks

I changed the constants 511 and 50 to 36 and 5 as in the sample

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
// constant declerations
const int listSize = 36 ;
const int maxTestVals = 5 ;
const unsigned int defaultSeed = 15199415 ;
// funtion prototypes
void PrintNameHeader();
void FillMainList(int list[]);
void FillTestArray(const int list[], int test[]);
void PrintArray(const int ary[], int size);
void PrintTestArray(const int test[], int maxTestVals);
void SelectionSort(int list[], int maxTestVals);
int LinearSearch(const int list[], int value, int& comps);
int BinarySearch(const int list[], int value, int& comps);
void search1(const int list[], const int test[]);
void search2(const int list[], const int test[]);
int main ()
{
int list[listSize];
int test[maxTestVals];
// int comps;
cout << fixed << showpoint << setprecision(1) ;
PrintNameHeader();
char choice;
cout << "Do you want to use random values (versus default values)? [y/n] ";
cin >> choice ;
if(choice == 'Y' || choice == 'y')
{
srand(static_cast<unsigned int>(time(0))) ;
}
else
srand (defaultSeed);
cout << "The random number seed value is: " << defaultSeed << endl;
FillMainList(list);
cout<<"Elements In Main list"<<endl;
cout<<endl;
PrintArray(list, listSize);
cout<<endl;
FillTestArray(list, test);
cout<<endl;
cout<<"Elements In Test Array:"<<endl;

PrintArray( test, maxTestVals);
cout<<endl;
cout<<"Linear Search version -- Before Sort. ";
search1( list, test);


SelectionSort(list, listSize);
cout<<endl;
cout << "Sorted array" << endl;
PrintArray(list, listSize);
cout << endl << endl;
cout<<"Linear Search version -- After Sort. ";
search1( list, test);
cout<<" Binary Search version ";
search2( list, test);
system("pause");
return 0;
}
void PrintNameHeader()
{
// displays my name header on the screen

}
void FillMainList(int list[])
{
bool found;
int index = 0;
while (index < listSize)
{
list[index] = rand() % 5000;
found = false;
for(int idx = 0; idx < index && !found; idx++)
{
if(list[index] == list[idx])
found = true;
}
if (!found)
{
index++;
}
}
}

void FillTestArray(const int list[], int test[])
{
int index0;
int index1 = 0;
bool found;

for(index0 = 0; index1 < listSize && index0 < maxTestVals ; index0++)
{
test[index0] = list[index1];
index1+=12;
}

while( index0 < maxTestVals)
{
test[index0] = rand() % 5000;
found = false;
for( index0=index0 ; index0 < maxTestVals && !found; index0++)
{
if(test[index0] == test[index0])
found = true;
}
if (!found)
{
index0++;
}

}
}


void PrintArray(const int list[], int listSize)
{
for (int index = 0; index < listSize; index++)
{
cout << setw(6) << right << list[index];
if(index %12 == 11)
{
cout << endl;
}
}
cout<<endl;
}

void SelectionSort(int list[], int listSize)
{
int startScan;
int minIndex;
int minVal;
for(startScan = 0; startScan < listSize; startScan++ )
{
minIndex = startScan;
minVal = list[startScan];
for (int index = startScan + 1; index < listSize; index++)
{
if (list[index] < minVal)
{
minVal = list[index];
minIndex = index ;
}
}
list[minIndex] = list[startScan];
list[startScan] = minVal;
}
}
int LinearSearch(const int list[], int value, int& comps)
{
int index ;

for (index = 0; index < listSize ; index++)
{
comps++;
if (list[index] == value)
{
return index;
}
}
return -1;
}
int BinarySearch(const int list[], int value, int& comps)
{
int first = 0;
int last = listSize-1;
int middle ;
int count = 0;
while (first <= last)
{
middle = (first + last) / 2 ;
comps++ ;
if (list[middle] == value )
{
return middle;
}
else if (list[middle] > value )
last = middle -1;
else
first = middle + 1 ;
}
return -1;
}
void search1(const int list[], const int test[])
{int result;
int tot=0,found=0;
int index = 0;
cout << "Test Value"<< setw(12) << "Result" << setw(15) << "Comparisons" << endl;
cout << "----------"<< setw(12) << "------" << setw(15) << "-----------" << endl;
while( index < maxTestVals )
{

int comps = 0;
result=LinearSearch(list, test[index], comps);
cout <<test[index]<<" ";
if(result<0)
    cout<<setw(8)<<"NOT ";
   
   
else
     { tot+=comps;
     found++;
     cout<<setw(8)<<"    ";
     }
cout<<"found"<<setw(11)<<comps<<endl;
index++;

}

cout<<" The average number of comparisons for "<<setprecision(1)<<fixed;
cout<<found<<" successful searches is "<<tot/(double)found<<endl;
cout<<"The number of unsuccessful searches is "<<maxTestVals-found;
cout<<" out of "<<maxTestVals<<endl;
}
void search2(const int list[], const int test[])
{int result;

int index = 0;
cout << "Test Value"<< setw(12) << "Result" << setw(15) << "Comparisons" << endl;
cout << "----------"<< setw(12) << "------" << setw(15) << "-----------" << endl;
while( index < maxTestVals )
{

int comps = 0;
result=BinarySearch(list, test[index], comps);
cout <<test[index]<<" ";
if(result<0)
    cout<<setw(8)<<"NOT ";   
else
    
     cout<<setw(8)<<"    ";

cout<<"found"<<setw(11)<<comps<<endl;
index++;
}
}