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

I\'m supposed to alter my code to read the array from a file, but I\'m not sure

ID: 3530507 • Letter: I

Question

I'm supposed to alter my code to read the array from a file, but I'm not sure how to do it. This is what I have so far. #include #include using namespace std; //Function Prototype int linearSearch(int [], int, int), binarySearch(int [], int, int); const int SIZE=20; int main() { int tests[SIZE] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; int count1,count2; //array search count1=linearSearch(tests,SIZE,319); count2=binarySearch(tests,SIZE,319); //output data cout<<"# Comparison in Linear: "<<count1<<endl; cout<<"# Comparison in Binary: "<<count2<<endl; return 0; } //************************* // Linear Search Function * //************************* int linearSearch(int list[],int numElems,int value) { int index=0; int position=-1; bool found=false; int count=0; while (index < numElems && !found) { if (list[index] == value) { found=true; position=index; } index++; count++; } return count; } //************************* // Binary Search Function * //************************* int binarySearch(int array[], int size, int value) { int first=0, last=size-1, middle, position=-1; bool found=false; int count=0; while (!found && first<=last) { middle=(first+last)/2; if (array[middle]==value) { found=true; position=middle; } else if (array[middle]>value) { last=middle-1; count++; } else { first=middle+1; } count++; } return count; }

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
//Function Prototype
int linearSearch(int [], int, int),
binarySearch(int [], int, int);
const int SIZE=20;
int main() {
int tests[SIZE] ;

int index=0;


int i;

char *inname = "test.txt";

ifstream infile(inname);


if (!infile) {

cout << "There was a problem opening file "

<< inname

<< " for reading."

<< endl;

return 0;

}

cout << "Opened " << inname << " for reading." << endl;

while (infile >> i) {

tests[index] = i;

index++;

}


int count1,count2; //array search
count1=linearSearch(tests,SIZE,319);
count2=binarySearch(tests,SIZE,319); //output data
cout<<"# Comparison in Linear: "<<count1<<endl;
cout<<"# Comparison in Binary: "<<count2<<endl;
return 0;
}
//************************* // Linear Search Function * //*************************
int linearSearch(int list[],int numElems,int value) {
int index=0;
int position=-1;
bool found=false;
int count=0;
while (index < numElems && !found) {
if (list[index] == value) {
found=true;
position=index;
}
index++;
count++;
}
return count;
}
//************************* // Binary Search Function * //*************************
int binarySearch(int array[], int size, int value) {
int first=0,last=size-1, middle, position=-1;
bool found=false;
int count=0;
while (!found && first<=last) {
middle=(first+last)/2;
if (array[middle]==value) {
found=true;
position=middle;
}
else if (array[middle]>value) {
last=middle-1;

}
else {
first=middle+1;
}
count++;
}
return count;
}