In Computer Science it is often very important to be able to

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 function are called searching algorithms, and there are many such algorithms in Computer Science. Although it is inefficient, one of the most common searching algorithms is called Linear Search. In Linear Search we have a set of data that serves as the standard, usually stored within an array, and a separate value that we are searching for within that data set. We’d like to know whether the value is within the data set, so we scan through the data set looking for it, one element at a time, starting at the beginning of the array and proceeding, if necessary, to the very last element. If the value is found within the standard array, we return a number indicating its index position within the array. 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 problem, please implement a linear search algorithm that performs this function. You will be given two input files, “LSStandard.txt” and “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 of these into separate arrays and then determine which of the numbers in the LSTest file are included in the LSStandard data set by using a Linear Search algorithm. Have your program print out a report (to the console only is sufficient) 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 report. The “index” number refers to the index of the element within the LSStandard data. Your function header for the Linear Search function should look like: int searchList(int stdList [], int numElems, int value) 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 “value” is the element that we are searching for. Your function should search for “value” within the “stdList” array and return one of two answers: (a) a -1 if “value” is not in “stdList”, or (b) the index position of “value” within “stdList” if “value” is in “stdList”. (This should be a number between 0 and (numElems-1).) Your program should then use that result to determine what should be printed in the report. Note that some of the numbers in LSTest appear more than once in LSStandard. The Linear Search algorithm as we discussed in class will return the first found instance, and that’s perfectly okay for the basic assignment. However, you can get 5 bonus points if your program can find and report the indices of all instances of a number that were found in LSStandard. In this case, your output could look something like: Number 1 ( 79) was located at index(es) 44, 47. Number 2 ( 74) was not in the file. Number 3 ( 56) was not in the file. Number 4 (103) was located at index 75, 82, 93. etc. If you have to modify the input parameters of searchList to solve this bonus problem that would be okay. However, as a hint, one could use a static local variable to solve the problem without changing the prototype as given.

Solution

#include<stdio.h>
#include<stdlib.h>
static int n=0;
int searchList(int stdList [], int numElems, int value){


        int     i, flag = 0, index;
        for(i=0; i<numElems; i++){

                if(stdList[i] == value){

                        flag = 1;
                        index = i;
                        printf(\"Number %d (%d) was located at index %d\ \", ++n, value, index);
                }
        }
        if(flag == 0)
                printf(\"Number %d (%d) was not in the file\ \", ++n, value);

}
int main()
{

        FILE    *fp, *fp1;
        fp = fopen(\"LSStandard.txt\", \"r\");
        fp1 = fopen(\"LSTest.txt\", \"r\");

        int     num, a[100], b[100], i=0;
        char    word[10];
        while(fscanf(fp,\"%s\", word)!= EOF){

                a[i++] = atoi(word);
        }
        num = i;
        i = 0;
        while(fscanf(fp1,\"%s\", word)!=EOF)
        {
                b[i] = atoi(word);
                searchList(a, num, b[i++]);
        }

}

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 func
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 func

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site