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.

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
  

Comments

Popular posts from this blog

A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required. If the requested copies book details and requests for the number of copies required. If the requested copies are available, the total cost of the requested copies is displayed; otherwise the message ?Required copies not in stock? is displayed. Design a system using a class called books with suitable member functions and Constructors. Use new operator in constructors to allocate memory space required. Implement C++ program for the system

Write C++ program for storing appointment schedule for day. Appointments are booked randomly using linked list. Set start and end time and min and max duration for visit slot. Write functions for- a)Display free slots b)Book appointment c)Cancel appointment ( check validity, time bounds, availability etc) d)Sort list based on time e)Sort list based on time using pointer manipulation

Imagine a publishing company which does marketing for book and audiocassette versions. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Write a program that instantiates the book and tape classes, allows user to enter data and displays the data members.