0% found this document useful (0 votes)
6 views

Computer Science Program File

This document contains the file for a computer science project made by Srishiti Ray of class XII-A. It includes an acknowledgement section thanking her teacher and parents for their support. The file also contains an index listing 33 questions related to programming concepts and data structures along with their corresponding task signatures. Sample code is provided for the first 10 questions as examples.

Uploaded by

Srishti Ray
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Computer Science Program File

This document contains the file for a computer science project made by Srishiti Ray of class XII-A. It includes an acknowledgement section thanking her teacher and parents for their support. The file also contains an index listing 33 questions related to programming concepts and data structures along with their corresponding task signatures. Sample code is provided for the first 10 questions as examples.

Uploaded by

Srishti Ray
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 78

COMPUTER

SCIENCE
PROGRAM FILE

MADE BY:

SRISHTI RAY__
__

____XII-A___
__________________
ACKNOWLEDGEMENT

I would like to express my gratitude to my


teacher _Mrs shaista khan _ who gave me the
golden opportunity to do this _project__
which also helped me in doing a lot of
research and I came to know about many
things. I am really thankful to _my computer
science teacher_. Secondly, I would also like
to thank my parents who helped me in
finalizing this project within the limited time
frame.

INDEX
S.NO. QUESTION T.SIGN.
1 Create a class travel plan and enter travel plans
details by const/dest.
2 Create a class clothing to display garment details by
using const/dest.
3 Create a class Tour and enter tour details and display
by using const/dest.
4 Create a class Play and display play details using
const/dest.
5 Enter a string and store it into file char by char.
6 Count total number of spaces from a file.
7 Enter 3 words, store them into a file and display
contents of the file.
8 Count total number of words starting with ‘a’.
9 Count total number of line starting with ‘A’.
10 Enter 3 lines and display them.
11 Write a program having a class student having
roll no, name and age.Enter data and display
the contents of file using structure.
12 Enter employee code, name and salary, store
it into file and display contents from file by
using struct.
13 Enter 5 elements in an array and display them.
14 Enter 5 elements in an array and display twice of
each element.
15 Enter 5 elements and display even numbers.
16 Enter two arrays of 5 elements each, and display
common values of both the arrays.
17 Enter an array and display it in ascending order.

18 Enter a matrix and display.


19 Enter a matrix and display left diagonal
20 Enter a matrix and display right diagonal
21 Enter a matrix and display lower triangle.
22 Enter a matrix and display upper triangle.
23 Enter n number of elements and sort them by
using bubble sort technique.
24 Enter n number of elements and sort them by
using insertion sort technique.
25 Enter n number of elements and sort them by using
selection sort technique.

26 Enter n number of elements and search them by


using binary search technique.
27 Enter n number of elements and search them by
using linear search technique.
28 Insertion sort + Binary search
29 Insertion sort + Binary search
30 bubble sort + Binary search
31 Selection sort+binary search
32 Write a function in C++ to count and display the
number of lines not starting with alphabet 'A' present
in a binary text file "STORY.TXT".
33 Write a definition for function Economic() in C++ to
read each record of a binary file ITEMS.DAT, find and
display those items, which costs less than 2500.
34 Write a definition for function COSTLY() in C++ to read
each record of a binary file GIFTS.DAT, find and
display those gifts, which are priced more that 200.
35 . Define a class RESORT in C++ with following
description
36 . Define a class SUPPLY in C++ with following
description:

37 . Define a class FLIGHTFUEL in C++ with following


description:

38 . Define a class APPLICANT in C++ with following


description:

39 . Define a class CARRENTAL in C++ with following


description:

40 . Define a class CANDIDATE in C++ with following


description:

1. Create a class travel plan and enter travel plans details by const/dest.
#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

classTravelPlan

longPlanCode;

char Place[25];

intNumber_of_travellers;

intNumber_of_busses;

public:

TravelPlan()

PlanCode=1001;

Number_of_travellers=5;

Number_of_busses=1;

strcpy(Place,"Agra");

voidNewPlan()

{ cout<<"\nEnter the travel plan code : ";

cin>>PlanCode;

cout<<"\nEnter the place where you want to travel : ";

gets(Place);
cout<<"\nEnter the number of travellers : ";

cin>>Number_of_travellers;

if(Number_of_travellers<20)

Number_of_busses=1;

else if(Number_of_travellers>20&&Number_of_travellers<40)

Number_of_busses=2;

else if(Number_of_travellers>=40)

Number_of_busses=3;

voidShowPlan()

{ cout<<"\nThe travel plan code is: ";

cout<<PlanCode<<"\n";

cout<<"\nThe place where you want to travel is: ";

puts(Place);

cout<<"\nThe no. of travellers are: "<<Number_of_travellers<<"\n";

cout<<"\nThe number of busses are: "<<Number_of_busses;

~TravelPlan()

{ cout<<"\nValues Destroyed";

};

void main()

{ clrscr();
TravelPlan X;

X.NewPlan();

X.ShowPlan();

getch();

2. Create a class clothing to display garment details by using const/dest.


#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class Clothing

private:

char code[30];
char type[30];

int size;

char material[30];

float price;

void Calc_Price()

if(strcmp(material,"COTTON")==0)

price=1100;

if(strcmp(type,"TROUSER")==0)

price=1500;

else if(strcmp(type,"SHIRT")==0)

price=1200;

public :

Clothing()

strcpy(code,"Not assigned");

strcpy(type,"Not assigned");

size=0;

strcpy(material,"Not assigned");

price=0;

void Enter()
{

cout<<"Enter Details";

cout<<"Enter Code ";

gets(code);

cout<<"Enter Type(SHIRT/TROUSER) ";

gets(type);

cout<<"Enter Size";

cin>>size;

cout<<"Enter Material: ";

gets(material);

Calc_Price();

void Show()

{ cout<<"Showing Details";

cout<<"Code "<<code;

cout<<"Type "<<type;

cout<<"Size "<<size;

cout<<"Material "<<material;

cout<<"Price"<<price;

~Clothing()

cout<<"Destructor at work";
}

};

void main()

Clothing C;

C.Enter();

C.Show();

getche();

3. Create a class Tour and enter tour details and display by using const/dest.
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iostream.h>
class Tour
{

char TCode[2o];
int NoofAdults,NoofKids,Kilometres;
float TotalFare;
public:
Tour( )
{

strcpy(TCode,"NULL");
NoofAdults=NoofKids=Kilometres=TotalFare=0;
}
void AssignFare( )
{

if(Kilometres>=1000)
TotalFare=NoofAdults*500+NoofKids*250;
else if(Kilometres>=500)
TotalFare=NoofAdults*300+NoofKids*150;
else
TotalFare=NoofAdults*200+NoofKids*100;
}
void EnterTour( )
{

cout<<"\nEnter the Tour Code: ";


gets(TCode);
cout<<"\nEnter the Number of Adults: ";
cin>>NoofAdults;
cout<<"\nEnter the Number of Kids: ";
cin>>NoofKids;
cout<<"\nEnter the Number of Kilometres: ";
cin>>Kilometres;
AssignFare( );
}
void ShowTour( )
{
cout<<"\nThe Tour Code: "<<TCode;
cout<<"\nThe Number of Adults:” <<NoofAdults;
cout<<"\nThe Number of Kids: "<<NoofKids;
cout<<"\nThe Number of Kilometres: “ <<Kilometres;
cout<<"\nThe Total Fare: "<<TotalFare;
}
};
void main( )
{

clrscr();
tour T;
T.EnterTour( );
T.ShowTour( );
getche();
}

4. Create a class Play and display play details using const/dest.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Play
{

int Playcode;
char playtitle[25];
float Duration;
int Noofscenes;
public:
Play( )
{

Duration=45;
Noofscenes=5;
}
void Newplay( )
{

cout<<"\nEnter the Play Code: ";


cin>>Playcode;
cout<<"\nEnter the Play Title: ";
gets(Playtitile);
}
void Moreinfor(float D,int N)
{

Duration = D;
Noofscenes = N;
}
void Showplay( )
{

cout<<"\nThe Play Code : “<<Playcode;


cout<<"\nThe Play Title : “ <<Playtitle;
cout<<"\nThe Duration :” <<Duration;
cout<<"\nThe No ofScenes:"<<Noofscenes;
}
};
void main( )
{
Play P;
P.Newplay( );
float Dur;
int NS;
cout<<"\nEnter the Duration andNumber of Scenes: ";
cin>>Dur>>NS;
P.Moreinfor(Dur,NS);
P.Showplay( );
getche( );
}

5. Enter a string and store it into file char by char.

#include<fstream.h>
#include<stdio.h>
void main()
{
ofstream afile("SALONI.txt");
char str[20];
int i;
cout<<"Enter any string "<<endl;
gets(str);
for(i=0;str[i]!='\0';i++)
{
afile.put(str[i]);
}
afile.close();
}

6. Count total number of spaces from a file.


#include<fstream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
ifstream afile("SALONI.txt");
char ch;
int i=0;
while(afile)
{
afile.get(ch);
if(ch==' ')
i++;
}
cout<<"total no of spaces="<<i<<endl;
afile.close();
getche();
}

7.Enter 3 words, store them into a file and display contents of the file.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i;
ofstream afile("hello.txt",ios::app);
cout<<"Enter 3 words"<<endl;
for(i=0;i<3;i++)
{
gets(str);
afile<<str<<" ";
}
afile.close();
ifstream bfile("ball.txt",ios::app);
while(bfile)
{
bfile>>str;
cout<<str<<" ";
}
bfile.close();
getche();
}

8.Count total number of words starting with ‘a’.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i=0;
ifstream afile("Ram.txt");
while(afile)
{
afile>>str;
if(str[0]=='a')
i++;
}
cout<<"Total occurence "<<i<<endl;
afile.close();
getche(); }

9.Count total number of line starting with ‘A’.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i=0;
ifstream afile("Ram.txt");
while(afile)
{
afile.getline(str,79);
if(str[0]=='A')
i++;
}
cout<<"Toal lines starting with 'A' are = "<<i<<endl;
afile.close();
getche();
}
10.Enter 3 lines and display them.
#include<fstream.h>

#include<conio.h>

void main()

char str[80];

int i;

ofstream afile("saloni.txt");

for(i=0;i<3;i++)

cout<<"Enter any line";

gets(str);

afile<<str<<'\n';
}

afile.close();

ifstream bfile("saloni.txt");

while(bfile)

bfile.getline(str,79);

cout<<str<<'\n';

bfile.close();

getche();

11.Write a program having a class student having roll no, name and
age.Enter data and display the contents of file using structure.

#include<fstream.h>

#include<conio.h>
#include<stdio.h>

struct student

char name[20];

int roll no;

int age;

}s;

void main()

ofstream afile("stud.dat",ios::binary);

cout<<"enter roll no";<<"\t";

cin>>s.rollno;

cout<<"enter name\t";

gets(s.name);

cout<<"enter age\t";

cin>>s.age;

afile.write((char*)&s,sizeof(s));

afile.close();

ifstream bfile("stud.dat",ios::binary);

while(bfile.read(char*)&s,sizeof(s)))

cout<<s.rollno<<"\t"<<s.name<<"\t"<<s.age<<endl;

}
bfile.close();

getche();

12.Enter employee code, name and salary, store it into file and display
contents from file by using struct.

#include<fstream.h>

#include<conio.h>

#include<stdio.h>

struct student

char name[20];

int code;

int salary;

}e;

void main()

ofstream afile("emp.dat",ios::binary);
cout<<"enter code";<<"\t";

cin>>e.code;

cout<<"enter name\t";

gets(e.name);

cout<<"enter salary\t";

cin>>e.salary;

afile.write((char*)&e,sizeof(e));

afile.close();

ifstream bfile("emp.dat",ios::binary);

while(bfile.read(char*)&e,sizeof(e)))

cout<<e.rollno<<"\t"<<e.name<<"\t"<<e.age<<endl;

bfile.close();

getche();

}
13.Enter 5 elements in an array and display them.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, ar[5];
cout<<"Enter 5 elements"<<endl;
for(i=0;i<5;i++)
{
cin>>ar[i];
}
for(i=0;i<5;i++)
{
cout<<ar[i]<<"\t";
}
getche();
}

14.Enter 5 elements in an array and display twice of each element.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, ar[5];
cout<<"Enter 5 elements"<<endl;
for(i=0;i<5;i++)
{
cin>>ar[i];
}
for(i=0;i<5;i++)
{
cout<<"Twice="<<2*ar[i]<<"\t";
}
getche();
}

15.Enter 5 elements and display even numbers.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, ar[5];
cout<<"Enter 5 elements"<<endl;
for(i=0;i<5;i++)
{
cin>>ar[i];
}
for(i=0;i<5;i++)
{
if(ar[i]%2==0)
{
cout<<ar[i]<<"\t";
}
}
getche();
}

16.Enter two arrays of 5 elements each, and display common values of


both the arrays.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, ar[5], b[5];
cout<<"Enter first array"<<endl;
for(i=0;i<5;i++)
{
cin>>ar[i];
}
cout<<"Enter second array"<<endl;
for(i=0;i<5;i++)
{
cin>>b[i];
}
for(i=0;i<5;i++)
{
if(ar[i]==b[i])
{
cout<<"Common elements are="<<ar[i]<<"\t";
}
}
getche();
}
17.Enter an array and display it in ascending order.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, ar[5], j, k;
cout<<"Enter first array"<<endl;
for(i=0;i<5;i++)
{
cin>>ar[i];
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(ar[i]<ar[j])
{
k=ar[i];
ar[i]=ar[j];
ar[j]=k;
}
}
}
cout<<"The sorted order is="<<endl;
for(i=0;i<5;i++)
{
cout<<ar[i]<<"\t";
}
getche();
}

18. Enter a matrix and display.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, a[3][3], j;
cout<<"Enter a matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"Matrix ="<<a[i][j]<<"\t";
}
cout<<endl;
}
getche();
}

19.Enter a matrix and display left diagonal


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, a[3][3], j;
cout<<"Enter a matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Left diagonal="<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
}
cout<<endl;
}
getche();
}
20.Enter a matrix and display right diagonal
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, a[3][3], j;
cout<<"Enter a matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Right diagonal="<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i+j==2)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
}
cout<<endl;
}
getche();
}

21.Enter a matrix and display lower triangle.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, a[3][3], j;
cout<<"Enter a matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Lower Triangle="<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
}
cout<<endl;
}
getche();
}

22. Enter a matrix and display upper triangle.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, a[3][3], j;
cout<<"Enter a matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"Upper Triangle="<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<=j)
cout<<a[i][j]<<"\t";
else
cout<<"\t";
}
cout<<endl;
}
getche();
}
23.Enter n number of elements and search them by using bubble sort
technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int list[50];
int i,j,n,temp;
cout<<"Enter size of list =\n";
cin>>n;
cout<<"Enter list \n";
for(i=0;i<n;i++)
{
cin>>list[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
cout<<"The sorted list is = \n";
for(i=0;i<n;i++)
{
cout<<list[i]<<endl;
}
getche(); }

24. Enter n number of elements and search them by using insertion sort
technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,list[20];
void insertion(int,int[]);
cout<<"Enter size of list = ";
cin>>n;
cout<<"Enter list\n";
for(i=0;i<n;i++)
cin>>list[i];
insertion(n,list);
getche();
}
void insertion(int n,int list[20])
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=list[i];
j=i-1;
while(temp<list[j] && (j>=0))
{
list[j+1]=list[j];
j=j-1;
}
list[j+1]=temp;
}
cout<<"The sorted list is\n";
for(i=0;i<n;i++)
{
cout<<list[i]<<endl;
}
}
25. Enter n number of elements and search them by using selection sort
technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,n,min,temp,a[20];
void selection(int,int[]);
cout<<"Enter size of list =\n";
cin>>n;
cout<<"Enter list \n";
for(i=0;i<n;i++)
cin>>a[i];
selection(n,a);
getche();
}
void selection(int n,int a[20])
{
int i,k,min,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(k=i+1;k<n;k++)
{
if(a[min]>a[k])
min=k;
}
if(i!=min)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
cout<<"The sorted list is \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}}

26.Enter n number of elements and search them by using linear search


technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a[20],x;
void linear(int,int[],int);
cout<<"Enter number of elements = ";
cin>>n;
cout<<"Enter values in an Array\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter value to be searched = ";
cin>>x;
linear(n,a,x);
getche();
}
void linear(int n,int a[20],int x)
{
int i,pos=-1;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
pos=i;
}
}
if(pos==-1)
cout<<"Element not in list";
else
cout<<"Element is present at "<<++pos<<" position";
}
27.Enter n number of elements and search them by using binary search
technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,x,a[20];
void binary(int,int[],int);
cout<<"Enter size of list = ";
cin>>n;
cout<<"Enter list\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter element to be searched = ";
cin>>x;
binary(n,a,x);
getche();
}
void binary(int n,int a[20],int x)
{
int f,l,m,pos;
f=0;
l=n-1;
pos=-1;
while(f<=l && pos==-1)
{
m=(f+l)/2;
if(a[m]==x)
pos=m;
else if(a[m]>x)
l=m-1;
else if(a[m]<x)
f=m+1;
}
if(pos==-1)
cout<<"Element is not in list.";
else
cout<<x<<" is present at "<<++pos<<" position.";
}

28.Insertion sort + Binary search


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,list[20],f,l,m,pos,temp,x,j;
cout<<"Enter size of list = ";
cin>>n;
cout<<"Enter list\n";
for(i=0;i<n;i++)
cin>>list[i];
for(i=0;i<n;i++)
{
temp=list[i];
j=i-1;
while(temp<list[j] && (j>=0))
{
list[j+1]=list[j];
j=j-1;
}
list[j+1]=temp;
}
cout<<"The sorted list is\n";
for(i=0;i<n;i++)
{
cout<<list[i]<<endl;
}
cout<<"Enter the element to be searched =\n";
cin>>x;
f=0;
l=n-1;
pos=-1;
while(f<=l && pos==-1)
{
m=(f+l)/2;
if(list[m]==x)
pos=m;
else if(list[m]>x)
l=m-1;
else if(list[m]<x)
f=m+1;
}
if(pos==-1)
cout<<"Element is not in list.";
else
cout<<x<<" is present at "<<++pos<<" position.";
getche();
}

29. Bubble sort + Binary search


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int list[50];
int i,j,n,temp,f,l,m,pos,x;
cout<<"Enter size of list =\n";
cin>>n;
cout<<"Enter list \n";
for(i=0;i<n;i++)
{
cin>>list[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
cout<<"The sorted list is = \n";
for(i=0;i<n;i++)
{
cout<<list[i]<<endl;
}
cout<<"Enter the element to be searched"<<endl;
cin>>x;
f=0;
l=n-1;
pos=-1;
while(f<=l && pos==-1)
{
m=(f+l)/2;
if(list[m]==x)
pos=m;
else if(list[m]>x)
l=m-1;
else if(list[m]<x)
f=m+1;
}
if(pos==-1)
cout<<"Element is not in list.";
else
cout<<x<<" is present at "<<++pos<<" position.";
getche();
}

30. Selection sort + Binary search


#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n,min,temp,a[20];
void selection(int,int[]);
cout<<"Enter size of list =\n";
cin>>n;
cout<<"Enter list \n";
for(i=0;i<n;i++)
cin>>a[i];
selection(n,a);
getche();
}
void selection(int n,int a[20])
{
int i,k,min,temp,f,l,m,pos,x;
for(i=0;i<n-1;i++)
{
min=i;
for(k=i+1;k<n;k++)
{
if(a[min]>a[k])
min=k;
}
if(i!=min)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
cout<<"The sorted list is \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
cout<<"Enter the element to be searched"<<endl;
cin>>x;
f=0;
l=n-1;
pos=-1;
while(f<=l && pos==-1)
{
m=(f+l)/2;
if(a[m]==x)
pos=m;
else if(a[m]>x)
l=m-1;
else if(a[m]<x)
f=m+1;
}
if(pos==-1)
cout<<"Element is not in list.";
else
cout<<x<<" is present at "<<++pos<<" position.";
getche();
}

31. Bubble sort + Linear search


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int list[50];
int i,j,n,temp,x,pos=-1;
cout<<"Enter size of list =\n";
cin>>n;
cout<<"Enter list \n";
for(i=0;i<n;i++)
{
cin>>list[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
cout<<"The sorted list is = \n";
for(i=0;i<n;i++)
{
cout<<list[i]<<endl;
}
cout<<"Enter value to be searched = ";
cin>>x;
for(i=0;i<n;i++)
{
if(list[i]==x)
{
pos=i;
}
}
if(pos==-1)
cout<<"Element not in list";
else
cout<<"Element is present at "<<++pos<<" position";
getche();
}

32.Write a function in C++ to count and display the number of lines not
starting with alphabet 'A' present in a binary text file "STORY.TXT".

ANS. void countlines()


{
ifstream afile ("STORY.TXT");
char str[80];
int i=0;
while(afile)
{
afile.getline(str,79);
if(str[0]!='A')
i++;
}
cout<<"Number of lines not starting with A are "<<i;
afile.close();
}

33.Write a definition for function Economic() in C++ to read each record of a


binary file ITEMS.DAT, find and display those items, which costs less than 2500.
Assume that the file ITEMS.DAT is created with the help of objects of class
ITEMS, which is defined below:

class ITEMS
{
int ID;
char PRODUCT [20];
float Cost;
public :
void GETDATA()
{
cin>ID;
gets(PRODUCT);
cin>> Cost;
}
void PUTDATA()
{
cout<<ID<<":"<<PRODUCT<<":"<<Cost<<endl;
}
float GetCost()
{
return(cost);
}
};

ANS. void economic()

{
ITEMS I;
ifstream afile("ITEMS.DAT",ios::binary);

while(afile.read((char*)&I,sizeof(I)))

if(I.Getcost()<2500)

I.PUTDATA();

afile.close();

34. Write a definition for function COSTLY() in C++ to read each record of a
binary file GIFTS.DAT, find and display those gifts, which are priced more that
200. Assume that the file GIFTS.DAT is created with the help of objects of class
GIFTS, which is defined below:

class GIFTS

int CODE;

char ITEM[20];

float PRICE;

public:

void Procure()

cin>>CODE;

gets(ITEM);

cin>>PRICE;

}
void View()

cout<<CODE<<":"<<ITEM<<":"<<PRICE<200;

G.View();

afile.close();

ANS. void COSTLY()

GIFTS G;

ifstream afile(“GIFTS.DAT”,ios::binary);

while (afile.read((char *)&G,sizeof(G)))

if(G.GetPrice()>2000)

G.View();

afile.close();

35. . Define a class RESORT in C++ with following description:


Private Members

 Rno //Data members to store Room No


 Name //Data members to store customer name
 Charges //Data members to store per day charges
 Days //Data members to store number of days of stay
 COMPUTE( ) //A function calculate and return Amount as Days *
Charges and if the value of Days * Charges is more than 11000 then as
1.02*Days*Charges
Public Members

 Getinfo( ) //A function to enter the content Rno, Name, Charges and
Days
 Dispinfo( ) //A function to display Rno, Name, Charges and Days and
amount
(Amount to be

//displayed by calling function COMPUTE( ))

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class RESORT
{
int roomno;
char customername[20];
float charges;
int days;
void COMPUTE
{
float=amount;
amount=days*charges;
if(days*charges>11000)
amount=1.02*days*charges;
return (amount);
}
public:
void getinfo();
void dispinfo();
};
void RESORT::getinfo()
{
cout<<"enter roomno";
cin>>Rno;
cout<<"enter name";
gets(name);
cout<<"enter charges";
cin>>charges;
cout<<"enter days";
cin>>days;
}
void RESORT::dispinfo()
{
cout<<Rno<<"\t"<<name<<"\t"<<charges<<"\t"<<days<<"\t"<<amount;
}
void main()
{
RESORT R;
R.getinfo();
R.dispinfo();
getche();
}

36. . Define class SUPPLY in C++ with following description:


Private Members

 Code of type int


 FoodName of type String
 Sticker of type Sting
 FoodType of String
 A member function GetType( ) to assign the following values for
FoodType as per the following sticker:
Sticker FoodType

GREEN Vegetarian

YELLOW Contains Egg

RED Non-Vegetarian

Public Members
 A function FoodIn( ) to allow user to enter values for Code,
FoodName, Sticker and call function GetType( ) to assign respective
FoodType.
A function FoodOut( ) to allow user to view the content of all the data
members

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class SUPPLY
{
int code;
char Foodname[20];
char sticker[20];
char FoodType[20];
void GetType()
{
if(strsmp(Sticker,"GREEN")==0)
strcpy(FoodType,"Vegetarian");
if(strsmp(Sticker,"YELLOW")==0)
strcpy(FoodType,"Contains egg");
if(strsmp(Sticker,"RED")==0)
strcpy(FoodType,"Non-Vegetarian");
}
public:
void FoodIn();
void FoodOut();
};
void SUPPLY::FoodIn()
{
cout<<"enter values for code ";
cin>>code;
cout<<"enter FoodName";
gets(FoodName);
cout<<"enter sticker";
gets(sticker);
GetType();
}
void SUPPLY::FoodOut()
{
cout<<Code <<"\t"<<FoodName<<"\t"<<Sticker<<"\t"<<FoodType;
}
void main()
{
SUPPLY S;
S.FoodIn();
S.FoodOut();
getche(); }
37. . Define a class in C++ with following description:
Private Members

 A data members Flight number of type integer


 A data members Destination of type string
 A data members Distance of type float
 A data members Fuel of type float
 A data members CALFUEL( ) to calculate the value of Fuel as per the
following criteria:
Distance Fuel

<=1000 500

More than 1000 and <=2000 1100

More than 2000 2200

Public Members

 A function FEEDINFO( ) to allow user to enter values for Flight Number,


Destination, Distance & call function CALFUEL( ) to calculate the quantity
of Fuel.
A function SHOWINFO( ) to allow user to view the content of all the data
members
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class FLIGHTFUEL
{
int flightnumber;
char destination[20];
float distance;
void CALFUEL()
{
if(distance<=1000)
fuel=500;
if(distance>1000&&distance<=2000)
fuel=1100;
if(distance>2000)
fuel=2200;
}
public:
void feedinfo();
void showinfo();
};
void FLIGHTFUEL::feedinfo()
{
cout<<"enter flight no";
cin>>flightnumber;
cout<<"enter destination";
gets(destination);
cout<<"enter distance";
cin>>distance;
CALFUEL();
}
void CALFUEL::showinfo()
{
cout<<flightnumber<<"\t"<<destination<<"\t"<<distance<<"\t"<<fuel;
}
void main()
{
FLIGHTFUEL F;
F.feedinfo();
F.showinfo();
getche();}
38. . Define a class Applicant in C++ with following description:
Private Members

 A data member ANo (Admission Number) of type long


 A data member Name of type string
 A data member Agg (Aggregate Marks) of type float
 A data member Grade of type char
 A member function GradeMe( ) to find the Grade as per the Aggregate
Marks obtained by a student. Equivalent Aggregate Marks range and the
respective Grades are shown as follows:
Aggregate Marks Grade

>=80 A

Less than 80and>=65 B

Less than 65and>=50 C

Less than 50 D

Public Members

 A function ENTER( ) to allow user to enter values for ANo, Name,


Agg & call function GradeMe( ) to find the Grade.
 A function RESULT( ) to allow user to view the content of all the data
members.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class APPLICANT
{
long Ano;
char name[20];
float Agg;
char grade;
void GradeMe()
{
if (Agg>=80)
grade='A';
if (Agg>=65&&Agg<80)
grade='B';
if (Agg>50&&Agg<65)
grade='C';
if(Agg<50)
grade='D';
}
public:
void ENTER();
void RESULT();
};
void Applicant::ENTER()
{
cout<<"enter Ano";
cin>>Ano;
cout<<"enter name";
gets(name);
cout<<"enter Agg";
cin>>Agg;
GradeMe();
}
void APPLICANT::RESULT()
{
cout<<Ano<<"\t"<<name<<"\t"<<Agg<<"\t"<<grade;
}
void main()
{
APPLICANT A;
A.ENTER();
A.RESULT();
getche();
}
39. Define a class CARRENTAL in C++ with following description:
 Car ID of type long int
 AboutCar of type string
 Cartype of type string
 Rent of type float
 A member function AssignRent( ) to assign the following values for Rent
as per the givenCartype:
Cartype Rent

Small 1000

Van 800

SUV 2500

Public Members

 A function GetCar( ) to allow user to enter values for carID, AboutCar,


Cartype and call function AssignRent( ) to assign Rent.
 A function ShowCar( ) to allow user to view the content of all the data
members
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class CARRENTAL
{
long int CarID;
char aboutcar[20];
float cartype[20];
float rent;
void assignrent()
{
if(stcmp(cartype,"small")==0)
Rent=1000;
if(strcmp(cartype,"van")==0)
Rent=800;
if(strcmp(cartype,"SUV")==0)
Rent=2500;
}
public:
void getcar ();
void showcar();
};
void CARRENTAL::getcar()
{
cout<<"enter carID";
cin>>carID;
cout<<"enter about car";
gets(aboutcar);
cout<<"enter car type";
gets(cartype);
AssignRent();
}
void CARRENTAL::showcar()
{
cout<<CarID<<"\t"<<AboutCar<<"\t"<<cartype<<"\t"<<Rent;
}
void main()
{
CARRENTAL C;
C.getcar();
C.showcar();
getche();
}
40. Define a class Candidate in C++ with following description:
Private Members

(i) A data member RNO (Registration Number) of type long


(ii) A data member Name of type string
(iii) A data member Score of type float
(iv) A data member Remarks of type string
(v) A member function AssignRem( ) to assign Remarks as per the score
obtained by a candidate. Score range and the respective Remarks are
shown as follows:
Score Remarks

>=50 Selected

Less than 50 Not Selected

Public Members

(i) A function ENTER( ) to allow user to enter values for RNO, Name,
Score & call function AssignRem( ) to assign the remarks.
(ii) A function DISPLAY( ) to allows user to view the content of all the
data members.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class CANDIDATE
{
long int Rno;
char name[20];
float score;
char Remarks[20];
void AssignRem()
{
if(Score>=50)
strcpy(Remarks,"selected");
else if(Score<50)
strcpy(Remarks,"not selected");
}
public:
void getinfo();
void dispinfo();
};
void CANDIDATE::getinfo()
{
cout<<"enter Rno";
cin>>Rno;
cout<<"enter name";
gets(name);
cout<<"enter score";
cin>>score;
AssignRem();
}
void CANDIDATE::dispinfo()
{
cout<<Rno<<"\t"<<name<<"\t"<<score<<"\t"<<Remarks;
}
void main()
{
CANDIDATE C;
C.getinfo();
C.dispinfo();
getche();
}
41. Enter ‘n’ no. of elements and delete particular element from top.
#include<iostream.h>

#include<conio.h>

Void main()

Inti , n , Ar[50] , pos=0;

Cout<<”enter no. of elements”;

Cin>>n;

For(i=0;i<n;i++)

Cout<<”enter value”;

Cin>>Ar[i];

For(i=pos;i<n-1;i++)

Ar[i]=Ar[i+1];

n--;

for(i=0;i<n;i++)

Cout<<Ar[i]<<endl;

}
Getche();

42.Enter 5 elements in the list and display number of digit in each element .
#include<iostream.h>

#include<conio.h>

Void main()

Inti , A[10] , k;

For(i=0;i<5;i++)

Cout<<”enter value in array”;

Cin>>A[i];
}

For(i=0;i<5;i++)

k=0;

while(A[i]>0)

A[i]=A[i]/10;

k++;

Cout<<”no. of digits”<<k<<endl;

Getche();

}
43.Enter ‘n’ no. of elements and insert particular element at desired position.
#include<iostream.h>

#include<conio.h>

Void main()

Inti ,Ar[30] , n , x , pos;

Cout<<”enter size of an array”;

Cin>>n;

For(i=0;i<5;i++)

Cout<<”enter array”;

Cin>>Ar[i];

Cout<<”enter position”;

Cin>>pos;

Cout<<”enter value”;

Cin>>x;

For(i=n;i>pos;i--)

Ar[i]=Ar[i-1];

Ar[pos]=x;
n++;

for(i=0;i<n;i++)

Cout<<Ar[i]<<endl;

Getche();

}
The End

You might also like