You are on page 1of 8

OUTPUT

Q1.WAP to perform linear and binary search on a 1-D


array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ch,l,u,mid,c=0,i,n,e,a[50];
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<endl<<"Enter the elemnts"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
cout<<" ";
}
cout<<"Enter the element to be searched for:"<<endl;
cin>>e;
cout<<"Enter Your Choice 1:Linear 2:Binary Search :";
cin>>ch;
switch(ch)
{
case 1 :
for(i=0;i<n;i++)
{
if(a[i]==e)
cout<<endl<<"Element is at "<<i+1<<" Position";
c=c+1;
}
if (c==0)
cout<<"Element Not Present";
break;
case 2:
c=0;
l=0;
u=n-1;
while(l<u)
{
mid=(l+u)/2;
if (a[mid]==e)
{
cout<<"Element is at "<<mid+1<<" Position "<<endl;
c++;
break;
}
if(e<a[mid])
u=mid-1;

if (e>a[mid])
l=mid+1;
}
if (c==0)
cout<<"Element not Present"<<endl;
break;

default :
cout<<"Wrong Choice"<<endl;
break;

}
getch();
}
OUTPUT
Q2.WAP to make progress report using data file
handling.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
struct stud
{
public:
int rno;
char name[50];
int cls;
float marks[5];
};

struct res
{
int rnos;
float total;
char grade;
float perc;
};

void main()
{
clrscr();
float tm;
int i,ma;
stud st;
res rt;

ofstream gpj("stud.dat",ios::binary|ios::trunc);
ofstream jpg("result.dat",ios::binary|ios::trunc);

if(!gpj||!jpg)
cout<<"File Cannot Be Created"<<endl;
int n;
if(gpj&&jpg)
{
cout<<"How many Students U Want to Enter ???? "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name"<<endl;
gets(st.name);
cout<<"Enter Roll No"<<endl;
cin>>st.rno;
cout<<"Enter Class"<<endl;
cin>>st.cls;
for(ma=0;ma<5;ma++)
{
cout<<"Enter Marks in subj"<<ma+1<<endl;
cin>>st.marks[ma];
}
gpj.write((char*)&st,sizeof(st));

tm=st.marks[0]+st.marks[2]+st.marks[1]+st.marks[3]+st.marks[4];
rt.perc=tm/5;
if(rt.perc>=75)
rt.grade='A';
if((rt.perc>=60)&&(rt.perc<75))
rt.grade='B';
if((rt.perc>=50)&&(rt.perc<60))
rt.grade='C';
if((rt.perc>=40)&&(rt.perc<50))
rt.grade='D';
if(rt.perc<40)
rt.grade='F';
rt.rnos=st.rno;
jpg.write((char*)&rt,sizeof(rt));
}}
gpj.close();
jpg.close();
int r,k=0;
ifstream god("result.dat",ios::binary);
if(!god)
{
cout<<"File Error"<<endl;
}
else
{
god.seekg(0);
cout<<endl<<"ENTER ROLL NO"<<endl;
cin>>r;
while(god.read((char*)&rt,sizeof(rt)))
{
if(r==rt.rnos)
{
k++;
cout<<" Roll no :"<<rt.rnos<<endl<<" Percentage :"<<rt.perc<<endl<<" Grade
:"<<rt.grade;
}
}
if(k==0)
cout<<"Roll no not present"<<endl;
god.close();
}
getch();
}

You might also like