adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C++ to search given data using function

Program in C++ to search given data using function



  • Using advance linear search technique


#include<iostream>
 using namespace std;
 void sort(int arr[], int n){ // Insertion sort
    int i, j, temp;
    for(i = 1; i < n; i++){
        temp = arr[i];
        for(j = i - 1; j >= 0 && temp < arr[j]; j--){
            arr[j + 1] = arr[j];
        }
        arr[j + 1] = temp;
    }
 }
 void Find(int arr[], int n){ // find data using sorted array
    int i;
    sort(arr,n);
    cout<<"\nEnter data which you want to search :- ";
    int data;
    cin>>data;
    for(i = 0; i < n; i++){
        if(data == arr[i] || data == arr[n - 1 - i]){
            cout<<endl<<"Data is found.";
            return;
        }
        else if(data < arr[i] || data > arr[n - 1 - i]){
            cout<<endl<<"Data is not found.";
            return;
        }
    }
 }
 void show(int arr[], int n){
    cout<<endl<<"Elements of array is ";
    for(int i = 0; i < n; i++){
        cout<<"   "<<arr[i];
    }
 }
 int main()
 {
    int arr[] = { 64, 10, 15, 45, 60, 24, 55};
    int n = 7;
    show(arr,7);
    Find(arr,n);
    return 0;
 }
Output :-









Related Programs


Post a Comment

0 Comments