Question

C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains...

C++ code

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.

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.

Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy:-

#include <iostream>   //Used for standard input/output
#include <cstdlib>   //used for exit() function
#include <fstream>   //used for input file stream

using namespace std;

//Function prototype
int searchList(int stdList [], int numElems, int searchValue);

int main()
{
   //Decalring array to store data set read from standard file
    int stdArray[100];
    //Decalring array to store data set read from test file
    int testArray[50];

    //Decalring input file stream object
    ifstream stdFile, testFile;
   
   //Open the standard file for reading
    stdFile.open("LSStandard.txt");
    //Checking whether the file is opened successfully or not
    if(!stdFile)
   {
   cout << "LSStandard.txt file can not opened for reading" << endl;
    exit(0);
    }
   
    int i=0;
    //Loop will be executed till end of file
   while(!stdFile.eof())
    {
    //Reading each number from standard file into array
stdFile >> stdArray[i];
i++;
    }
   //Closing standard file
   stdFile.close();
   //Storing total numbers read from standard file
   int numElemsStd = i;
   
  
   //Open the test file for reading
    testFile.open("LSTest.txt");
    //Checking whether the file is opened successfully or not
    if(!testFile)
   {
   cout << "LSTest.txt file can not opened for reading" << endl;
    exit(0);
    }
   
    i=0;
    //Loop will be executed till end of file
   while(!testFile.eof())
    {
    //Reading each number from test file into array
testFile >> testArray[i];
i++;
    }
   //Closing test file
   testFile.close();
   //Storing total numbers read from test file
   int numElemsTest = i;
  
  
int index;
for(int i=0; i<numElemsTest; i++)
{
//Calling function for each element of test array to search it in standard array
index = searchList(stdArray, numElemsStd, testArray[i]);
  
if(index != -1)
{
//Display details of each number of test array if found in an standard array
cout << "Number " << (i+1) << " " << "(" << testArray[i] << ")"
<< " was located at index " << index << endl;
}
else
{
//Display details of each number of test array if not found in an standard array
cout << "Number " << (i+1) << " " << "(" << testArray[i] << ")"
<< " was not in the file" << endl;
}
  
}
  
return 0;
}

//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.
//Function accepts three parameters:-
//1. An array that contains the standard data set.
//2. The parameter numElems is the number of elements in that array.
//3. The parameter searchValue is the element that we are searching for.
int searchList(int stdList[], int numElems, int searchValue)
{
for(int i=0; i<numElems; i++)
{
if(stdList[i]==searchValue)
//Returns index value of number if found in standard array
return i;
}
//Returns -1 if not found in standard array
return -1;
}

Screenshot of output:-

Number 1 (33) was located at index 2 Number 2 (16) was not in the file Number 3 (40) was located at index 5 Number 4 (76) was

Screenshot of content of file named "LSStandard.txt" used in the program:-

LSStandard - Notepad File Edit Format View Help 44 78 33 10 20 40 100 110 70 45 19 51 23 37 97 82 88 77 11 5

Screenshot of content of file named "LSTest.txt" used in the program:-

LSTest - Notepad File Edit Format View Help 33 16 40 76 45 51 37 82 77 39

Add a comment
Know the answer?
Add Answer to:
C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile...

    Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting rules we discussed in class. Please see the descriptive ile on eLearning for details. Program 1- 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...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT