WRITE ONE C PROGRAM TO SATISFY THE FOLLOWING 1 Read the inte

WRITE ONE C++ PROGRAM TO SATISFY THE FOLLOWING:

1) Read the integer values from the file A1.txt and store them into an integer array. The numbers in the file are ranged from 0 to 99.

2) Write a program that will take an integer number from the user, search that number from the array using linear search algorithm, and output the latest position of that number.

3) If the user input is not available in the file, generate an error message ”Number is not available!”.

A1.txt includes:

Solution

Hi, Please find my implementaation.

Please let me know in case of any issue.

#include <iostream>
#include <fstream>

using namespace std;

// function declaration
int linearSearch(int arr[], int n, int search);
int main(){

   // declaring array of size 100
   int arr[1000];

   // opening file
   ifstream inFile(\"A.txt\");

   // error checking in file opening
   if(inFile.fail()){
       cout<<\"Erorr in opening fiel\"<<endl;
       return -1;
   }

   int count = 0;
   int num;
   // reading numbers and storing in array
   while(inFile>>num){
       arr[count++] = num;
   }

   // closing file
   inFile.close();

   int search;
   cout<<\"Enter search key: \";
   cin>>search;

   int index = linearSearch(arr, count, search);

   if(search == -1){
       cout<<\"Number is not available!\"<<endl;
   }
   else{
       cout<<\"Number is available at \"<<(index+1)<<\" position\"<<endl;
   }

   return 0;
}

// function defination
int linearSearch(int arr[], int n, int search){
   for(int i=0; i<n; i++){

       if(arr[i] == search)
           return i;
   }

   return -1; // not available in array
}

WRITE ONE C++ PROGRAM TO SATISFY THE FOLLOWING: 1) Read the integer values from the file A1.txt and store them into an integer array. The numbers in the file ar
WRITE ONE C++ PROGRAM TO SATISFY THE FOLLOWING: 1) Read the integer values from the file A1.txt and store them into an integer array. The numbers in the file ar

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site