In: Computer Science
This programming assignment will consist of a C++ program. Your program must compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting described in the Required Program Development Best Practices document that has been discussed in class and is posted to the eLearning system. Please see this descriptive file on the eLearning system for more details. The name to use in the main configuration screen text box Name: [ ] in the Visual Studio for the solution folder is ArraysSearchSolution. Linear Search Algorithm In Computer Science, it is often very important to be able to locate a specific data item inside a list or collection of data. Algorithms that perform this searching function are called searching algorithms, and there are many such algorithms in Computer Science. Although it may be inefficient, one of the most common searching algorithms is called a Linear Search. In a Linear Search we have a set of data to be searched through that is referred to as the standard, usually stored within an array, and a separate search value that we are searching for within that array data set. The search is used identify whether the search value is within the data set. The linear search scans through the data set looking for the search value, one element at a time. The linear search starts at the beginning of the array and proceeds though the array. If the search value is not found, the search will process all the way to the very last element. If the search value is found within the standard array, we return a number indicating the found index position within the array and return this index information and discontinue further search processing. Note that it is important that we terminate the loop if the value is found. If the value is not found, we return an error indicator, oftentimes a -1, that indicates the value was not in the data set. For this assignment, implement a linear search algorithm that performs a linear search function over an array. The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains integer values against which we are searching. There will be no more than 100 of these. The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. There will be no more than 50 of these. Read both files into two separate arrays. Your program should then close both input files. All subsequent processing will be done on the arrays only. Use each number in the search value array from the LSTTest.txt file as a search value to search over the standard array that contains the data set from the LSStandard data file. This search is accomplished by a function using a Linear Search algorithm on the array that contains the standard data set. Linear Array Search 2 Have your program display out a report to the console that indicates whether the number was found or not. Your output should look something like: Number 1 ( 79) was located at index 44. Number 2 ( 74) was not in the file. Number 3 ( 56) was not in the file. Number 4 (103) was located at index 75. etc. Note that the number for which we searched is indicated in parenthesis in the display report. The index number refers to the index of the element within the stdList array data where the search value was found. Your function header for the Linear Search function should look like: int searchList(int stdList [], int numElems, int searchValue) You’ll notice that this function accepts an array as input parameter. That array, called stdList in the parameter list, will be the array that contains the standard data set. The parameter numElems is the number of elements in that array, and the parameter searchValue is the element that we are searching for. Your function should search for searchValue within the stdList array and return one of two answers: • a -1 if value is not in stdList array • if searchValue is in stdList, the index position of searchValue within the stdList array. This should be a number between 0 and numElems-1. Your program should then use this result to determine what should be displayed in the report. Note that some of the numbers in LSTest appear more than once in LSStandard. This Linear Search algorithm will return the first found instance.
Program code to copy:-
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
//Function prototype
int readFile(int list[], string filename);
int searchList(int stdList [], int numElems, int searchValue);
int main()
{
//Decalre array to hold number read from standard
file
int stdList[100];
////Decalre array to hold number read from test
file
int testList[50];
//Calling function to read numbers from standard file
into array
int numElems1 = readFile(stdList,
"LSStandard.txt");
//Calling function to read numbers from test file into
array
int numElems2 = readFile(testList,
"LSTest.txt");
int index;
for(int i=0; i<numElems2; i++)
{
//Calling function to search each
element of test array in standard array
index = searchList(stdList,
numElems1, testList[i]);
if(index != -1)
{
//Display output
when number if found in an standard array
cout <<
"Number " << (i+1) << " "
<< "(" << testList[i]
<< ")"
<< " was located at index " << index
<< endl;
}
else
{
//Display output
when number does not exist in an standard array
cout <<
"Number " << (i+1) << " "
<< "(" << testList[i]
<< ")"
<< " was not in the file" <<
endl;
}
}
return 0;
}
//Function read numbers from file into array.
//Function takes reference to array and file name as parameter
and
//returns number of elements read from file.
int readFile(int list[], string filename)
{
ifstream fin;
//Open the file for reading
fin.open(filename.c_str());
//Checking whether the file is opened successfully or
not
if(!fin)
{
cout << "File failed to open
for reading" << endl;
exit(0);
}
//variable keep track of number of elements read from
file
int i=0;
//Loop will be executed till end of file
while(!fin.eof())
{
//Reading each number from file
into array
fin >> list[i];
i++;
}
//Closing file
fin.close();
//Returns numbers of elements reads from file
return i;
}
//Function accepts three parameters:an array that contains the
standard data set and
//the parameter numElems is the number of elements in that array,
and
//the parameter searchValue is the element that we are searching
for.
//Function search for searchValue within the stdList array
and
//return one of two answers: -1 if value is not in stdList array
or
//if searchValue is in stdList, the index position of searchValue
within the stdList array.
int searchList(int stdList[], int numElems, int searchValue)
{
for(int i=0; i<numElems; i++)
{
if(stdList[i]==searchValue)
//Returns index
when value found in list
return
i;
}
//Returns when value not in list
return -1;
}
Screenshot of output:-
Sample files used for the execution of above program code:-
"LSStandard.txt"
"LSTest.txt"