0% found this document useful (0 votes)
31 views

Searching in C++

The code takes an array of numbers as input from the user along with the number of elements. It also takes a target number to search for. It then linearly searches the array from index 0 to the last element to check if the target number is present. If found, it prints the index where it is located, otherwise it prints that the number is not present in the array. The code is commented to explain the linear search process and is intended to be easy to understand.

Uploaded by

varun utkarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Searching in C++

The code takes an array of numbers as input from the user along with the number of elements. It also takes a target number to search for. It then linearly searches the array from index 0 to the last element to check if the target number is present. If found, it prints the index where it is located, otherwise it prints that the number is not present in the array. The code is commented to explain the linear search process and is intended to be easy to understand.

Uploaded by

varun utkarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//This code is created by Varun

#include <iostream>
using namespace std;

int main(){
int input[100], count, i, num;

cout << "Enter Number of Elements in Array\n";


cin >> count;

cout << "Enter " << count << " numbers \n";

// Read array elements


for(i = 0; i < count; i++){
cin >> input[i];
}

cout << "Enter a number to serach in Array\n";


cin >> num;

// search num in inputArray from index 0 to elementCount-1


for(i = 0; i < count; i++){
if(input[i] == num){
cout << "Element found at index " << i;
break;
}
}
//Basic program of linear search

if(i == count){
cout << "Element Not Present in Input Array\n";
}

return 0;
}//Easy to understand

You might also like