Write a C++ program to store the numbers of students in an array who have attended a training program in random order. Enrollment function for- a) Finding whether a particular student has attended a tutorial or not using linear search b) Finding whether a particular student has attended a tutorial or not using binary search.

Write C++ program to store roll numbers of student in array who attended training program
in random order. Write function for-
a) Searching whether particular student attended training program or not using linear search
b) Searching whether particular student attended training program or not using binary search.

#include<iostream>
using namespace std;

int List[10];
int size;
void input()
{
cout<<"Enter the No. of students";
cin>>size;
cout<<"Enter the Roll no. of Students attending training program \n";
for(int i=0; i<size; i++)
OUTPUT FOR THE PROGRAM
{
cin>>List[i];
}

}
void linearSearch()
{
    int i, Key, cnt = 0;
 
    cout<<"\n\n Enter the Key to Search: ";
    cin>>Key;
 
    for(i=0; i<size; i++)
    {
        cnt++;
        if(Key == List[i])
        {
            cout<<"\n\n Key Found.....!!!";
            cout<<"\n\n No of Comparisons: "<<cnt;
            break;
        }
    }
    if(i == size)
    {
        cout<<"\n\n Key Not Found.....!!!";     
        cout<<"\n\n No of Comparisons: "<<cnt;
    }
}


void binarySearch()
{
    int Low, High, Mid, Key, cnt = 0;
 
    cout<<"\n\n Enter the Key to Search: ";
    cin>>Key;
 
    Low = 0;
    High = size - 1;
 
    while(Low <= High)
    {
        cnt++;
        Mid = (Low + High)/2;
        if(Key == List[Mid])
        {
            cout<<"\n\n Key Found.....!!!";
            cout<<"\n\n No of Comparisons: "<<cnt;
            break;
        }
        else if(Key < List[Mid])
           High = Mid - 1;
        else
           Low = Mid + 1; 
    }
    if(Low > High)
    {
        cout<<"\n\n Key Not Found.....!!!";     
        cout<<"\n\n No of Comparisons: "<<cnt;
    }
}


int main()
{
input();
int s;
char t;
do
    {
    cout<<"\t\t1. Linear Search\n";
cout<<"\t\t2. Binary Search\n";
    cout<<"\t\tEnter The Choice ::::: ";
    cin>>s;
 
 
    switch(s)
    {
    case 1:
    linearSearch();
    break;
    case 2:
    binarySearch();
    break;
    default:
    cout<<"Wrong choice";   
    }
    cout<<"\n\nDo U wish to Continue (y/n) ::: ";
    cin>>t;
    }while(t == 'y' || t == 'Y');
 
     cout<<"\n\n";
     return 0;
}

For more such posts click the link:-http://svencrai.com/G8W
  

Post a Comment

Previous Post Next Post