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

Part1: Download Lab20.cpp. This program reads the candidate names and the number

ID: 3812598 • Letter: P

Question

Part1: Download Lab20.cpp. This program reads the candidate names and the number of votes received by each candidate of a local election from a file named input.txt. There will be eight lines in the input file. The program then outputs each candidate’s name and the number of votes received, in Ascending order. (The sort algorithm uses insertion sort algorithm). Show your output in the program console (check the output format from sample output). You need to implement the following function prototype:

void insertionSort(int vote[],string name[], int listLength);

Precondition: The function takes an integer array of vote[], a string array of candidate name[], and an integer listLength which represents the size of both arrays.

Post-condition: Integer array vote[], is sorted in ascending order and string array name[], is updated based on elements in sorted array vote[]. (Check the given sample output)

Part2: After showing the output of Part 1 in the program console, the program asks the user to input a number as received votes to be searched and outputs corresponding candidate’s name. (The binary search is used for searching purpose)

Complete the binarySearch function which accepts a sorted array resulted from insertion sort function in Part A, and returns the search result for the searched item. You need to implement the following function prototype:

string binarySearch(const int vote[], const string name[], int listLength, int searchItem)

Precondition: The function takes an array of sorted integer vote[], an array of string name[], an integer listLength which represents the size of the arrays, and an integer searchItem which to be searched.

Post-condition: If the searchitem is found in the vote[] its corresponding candidate’s name from the name[] is returned else the message "There is no candidate with the inputted votes!" is returned. (Check the given sample output)

input text

start of program:

Wuru File Home. Insert Design laymut Rrferences Mailings Review view Tell me what you want da PROTECTED VIEw Becarorul files from the Internat can contain vinusos. Unless you cod to edit, it's saforto stay in Protocted View. Enable diting EL CAWINDOWSMsystem321cmd.exe Candidate Votes Received C:WINDOWS system32Acmd.exe Candidate Mike 150 Votes Received Mike 150 Fire 160 Fire 160 Roger 210 210 Roger 240 Conner 240 Conner 320 Robert Robert 320 456 Ford 456 Ford 480 Andrew 480 Andrew 500 Wayne 500 Wayne The winner of the election is Wayne The winner of the election is Wayne Please enter the vote number to be searched? Please enter the vote number to be searched? 456 50 There is no candidate with the inputted votes Ford Press any key to continue Press any key to continue

Explanation / Answer

HI, I have implemented the required methods.

#include <iostream>
#include <string>

using namespace std;
#define ASIZE 8

void insertionSort(int vote[],string name[], int listLength)
{
//complete me
for (int i = 1; i < listLength; i++)
{
int key = vote[i];
string nk = name[i];
int j = i-1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && vote[j] > key)
{
vote[j+1] = vote[j];
name[j+1] = name[j];
j = j-1;
}
vote[j+1] = key;
name[j+1] = nk;
}
}
string binarySearch(const int vote[], const string name[], int listLength, int searchItem)
{
//complete me
int l= 0;
int r = listLength-1;
while (l <= r)
{
int m = l + (r-l)/2;

// Check if x is present at mid
if (vote[m] == searchItem)
return name[m];

// If x greater, ignore left half
if (vote[m] < searchItem)
l = m + 1;

// If x is smaller, ignore right half
else
r = m - 1;
}

// if we reach here, then element was not present
return "There is no candidate with the inputted votes!";
}
int main() {
  
string name[ASIZE];
int vote[ASIZE];
  
//read your input from input.txt here
  
//Part1
insertionSort(vote, name, ASIZE);
cout<<"Candidate "<<"Votes Received "<<endl;
  
for (int i = 0; i<ASIZE; i++){
cout<<name[i]<<" "<<vote[i]<<endl;
}
  
cout<<"The winner of the election is "<<name[ASIZE-1]<<endl;

//Part2
cout << "Please enter the vote number to be searched? ";
int x;
cin >> x;
cout << binarySearch(vote,name, ASIZE,x) << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote