Skip to main content

DSL

14.Problem statement

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 and sentinel search.  b) Searching whether particular student attended training program or not using binary search and Fibonacci search.   

program

#include<iostream>
using namespace std;
class search
{  public:
        int n,i,data[50],son[50],sortdata[50],s;
 void intake()
{     cout<<"Enter No. of Elements=";
  cin>>n;
  cout<<"\nEnter Elements=\n";
  for(i=1;i<=n;i++)
  {   cin>>data[i];
      son[i]=data[i];
  }
}
void search_lini(int item)
{   s=0;
  for(i=1;i<=n;i++)
  {  if(data[i]==item)
     {   s=1;
         break;
     }
  }
  if(s==1)
  cout<<"attended training program:\n";
  else
 cout<<"didn't attended training program:\n";
}
void search_senti(int a)
{  int i=0;
  while(data[i]!=a && i<n)
  { i++; }
  if(i<n)
     cout<<a<<" attended Training Program.\n";
  else
     cout<<a<<" didn't attend Training Program.\n";
}
void search_bin(int x)
{   int y=0;
    int left, right,mid;
    left=0;
    right=n-1;
    while(left<=right)
    {   mid=(left+right)/2;
        if(data[mid]==x)
        {   y=1;
            break;
        }
        else if(data[mid]>x)
            right = mid-1;
        else if(data[mid]<x)
            left=mid+1;
    }
    if(y==1)
        { cout<<x<<" attended Training Program.\n"; }
    else
        { cout<<x<<" didn't attend Training Program.\n"; }
}
int fibsearch(int x)
{   int inf = 0, pos, k;
   static int kk= -1, nn = -1;
    static int fib[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 98,
    1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811,
    514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817,
    39088169, 63245986, 102334155, 165580141};
    if (nn != n)
    {      k = 0;
        while (fib[k] < n)
            k++;
        kk = k;
        nn = n;
    }
    else
        k = kk;
    while (k > 0)
    {    pos = inf + fib[--k];
        if ((pos >= n) || (x < data[pos]));

        else if (x > data[pos])
        {    inf = pos + 1;
             k--;
        }
        else
           return pos;
    }
    return -1;
}
void sort()
{   int p,j,temp;
for(p=1;p<=n-1;p++)
{   for(j=1;j<=n-1;j++)
    {   if(data[j]>data[j+1])
        {
             temp=data[j];
             data[j]=data[j+1];
             data[j+1]=temp;
        }
    }
    }
}
};
int main()
{ int s,r,pos;
char y='y';
search t;
t.intake();
t.sort();
while(y=='y')
{
    cout<<"\n::Menu::\n"<<"1. Linear Search.\n2. Sentinel Search.\n3. Binary Search.\n4.fibonacci search";
    cin>>s;
    cout<<"Enter Roll no. to search:";
    cin>>r;
switch(s)
{ case 1: cout<<"\tUsing Linear Search :\n";
            t.search_lini(r);
            break;
    case 3: cout<<"\tUsing Binary Search :\n";
            t.search_bin(r);
            break;
    case 2: cout<<"\tUsing Sentinel Search :\n";
            t.search_senti(r);
                break;
    case 4: cout<<"\tUsing fibonacci search:\n";
     pos=t.fibsearch(r);
     if(pos>=0)
  cout<<"attended training program"<<endl;
 else
 cout<<"didn't attended training program"<<endl;
            break;
}
cout<<"do u want to continue(y/n)";
cin>>y;
   }
return 0;
}

Comments

Popular posts from this blog

DSL

9: Program Setatement A palindrome is a string of character that‘s the same forward and backward. Typically, punctuation, capitalization, and spaces are ignored. For example, ‖Poor Dan is in a droop‖ is a palindrome, as can be seen by examining the characters ―poor danisina droop‖ and observing that they are the same forward and backward. One way to check for a palindrome is to reverse the characters in the string and then compare with them the original-in a palindrome, the sequence will be identical. Write C++ program with functions- 1. to check whether given string is palindrome or not that uses a stack to determine whether a string is a palindrome. 2. to remove spaces and punctuation in string, convert all the Characters to lowercase, and then call above Palindrome checking function to check for a palindrome 3. to print string in reverse order using stack Program: #include<iostream> #include<string.h> #define MAX 100 using namespace std;  struct stack ...

OOP

6.Problem statement Develop an object oriented program in C++ to create a database of student  information system containing the following information: Name, Roll number, Class, division, Date of Birth, Blood group, Contact address, telephone number, driving license no. etc Construct the database with suitable member functions for initializing and destroying the data viz constructor, default constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete. Program #include<iostream> #include<stdio.h> #include<cstring> using namespace std; class personal; static int count; class person {       char *name;       int *rollno,*classno;       char *bloodgroup,*div;                   public:person()              {     ...

DSL

8: Problem statement: Write C++ program for storing binary number using doubly linked lists. Write functions- a) to compute 1‘s and 2‘s complement b) add two binary numbers #include <iostream> #include<vector> using namespace std; struct node { int data; node* next; node* prev; }; node* Create(int x) { node* temp=new node; temp->data=x; temp->next=NULL; temp->prev=NULL; return temp; } void Insert(node** head,int x) { node *temp=Create(x); if(*head==NULL) { *head=temp; } else { (*head)->prev=temp; temp->next=*head; *head=temp; } } void print(node* head) { node* temp=head; cout<<"\ndata: "; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; } cout<<endl; } int sum(int a,int b,int c) { return a^b^c; } int carry(int a,int b,int c) { int x=a*b; int y=b*c; int z=c*a; int s=sum(x,y,z); return s; } ...