Question

In: Computer Science

This programming assignment will consist of a C++ program. Your program must compile correctly and produce...

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.

Solutions

Expert Solution

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"


Related Solutions

6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
Programming assignment for ENSC 3213 NIM The program is to be written in Matlab. Program must...
Programming assignment for ENSC 3213 NIM The program is to be written in Matlab. Program must be liberally commented. The program is a game played by a human versus the machine. At the start, the prompt asks the human to enter three non-zero integers. These will constitute how many counters are in each Pile. The machine then asks who goes first. A turn consists of selecting a Pile and taking a number of counters from that pile. For example, if...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your solutions for each problem solved in a Word or Google document which includes (a) the pseudocode, (b) the C# codes and (c) the execution result (screen capture just the answer part using Snipping Tool, avoiding non-related or blank spaces). Notice for readability, that the (a) & (b) are in text mode and the (c) is in image mode. Use proper titles and wording in...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your...
Complete the 3 programming problems in this assignment by using Microsoft Visual Studio Suite. Compile your solutions for each problem solved in a Word or Google document which includes (a) the pseudocode, (b) the C# codes and (c) the execution result (screen capture just the answer part using Snipping Tool, avoiding non-related or blank spaces). Notice for readability, that the (a) & (b) are in text mode and the (c) is in image mode. Use proper titles and wording in...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the average CPI, total processing time (T), and MIPS of a sequence of instructions, given the number of instruction classes, the CPI and total count of each instruction type, and the clock rate (frequency) of the machine. The following is what the program would look like if it were run interactively. Your program will read values without using prompts. Inputs: • Clock rate of machine...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the...
Programming Assignment 1 Performance Assessment Objective: To write a C program (not C++) that calculates the average CPI, total processing time (T), and MIPS of a sequence of instructions, given the number of instruction classes, the CPI and total count of each instruction type, and the clock rate (frequency) of the machine. The following is what the program would look like if it were run interactively. Your program will read values without using prompts. Inputs: • Clock rate of machine...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT