/***************************************************************
* Name: Prof. Orta
* Course: Computer Science & Programming
* Class: CS04103 Section: 1 & 2
* Assignment Date: 10/27/20
* File Name: LinearSearchcpp
*****************************************************************
* ID: 001
* Purpose: Explain Linear Search in C++
*****************************************************************/
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main()
{
// Declaring variables
int array[10];
int value = 0, counter = -1;
// Inserting random numbers to the array of 10 elements.
srand (time(0));
for (int x = 0 ; x < 10 ; x++)
{
array[x] = rand() % 10 + 1;
}
// Displaying the array
system("clear");
cout << "\n ------ Array to search ----" << endl;
cout << "\n -----------------------------------------" << endl;
for (int x = 0 ; x < 10 ; x++)
{
cout << " | " << array[x];
}
cout << " |\n -----------------------------------------" << endl;
// Asking for the number to search for
cout << "\nPlesse enter the number you want to search for: ";
cin >> value;
cout << endl;
// Searching for the value
do
{
counter++;
cout << "Searching in the array index No: " << counter << " and the number is: " << array[counter] << endl;
if (value == array[counter])
cout << "\nFound the matching number!!!" << endl;
} while (counter < 9 and value != array[counter]);
if (counter == 9)
cout << "\nWe are sorry but that number does not exist in the array" << endl;
return 0;
}