Вы находитесь на странице: 1из 74

NAMECLASS- 12th B.

SUBJECT- COMPUTER SCIENCE C++.


SUBJECT TEACHER NAMEROLL NO. ALLOTED BY CBSESASSION-2014-2015.
SCHOOL NAME- D.S.R. MODERN
SCHOOL.

Q.1. What a menu driven program to perform following operators on


strings without using built in functions: find the length of the strings.
compare two strings.
concatenate two strings.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void findlength()
{
char str[30];
int l=0;
cout<<"\n Enter the string (size<=30) ";
gets(str);
while(str[l]!='\0')
{
l++;
}
cout<<"\n Length Of the given String is: "<<l<<endl;
}
void compare()
{

char str1[30], str2[30];


int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size<=30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
if(l2!=l1)
{
cout<<"\n Strings are not Equal ";
}
else
{
for(i=0;i<l1;i++)
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"\n Strings are not Equal ";
}
else
{
cout<<"\n Strings are Equal ";
}
}

}
void concat()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) ";
gets(str1);

while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size<=30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
for(i=0;i<l2;i++)
{
str1[l1+i]=str2[i];
}
str1[l1+l2]='\0';
cout<<"\n The concatenated String is: ";
puts(str1);
}

void main()
{
clrscr();
cout<<"Enter your choice \n \t1.Find length of string\n\t"
"2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n";
char ch;
cin>>ch;
do
{
if(ch=='1')
findlength();
if(ch=='2')
compare();
if(ch=='3')
concat();
cout<<"Enter your choice \n \t1.Find length of string\n\t"
"2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n";
cin>>ch;
}while(ch!='4');
getch();
}

Q.2. Create an array containing country and capital now write a menu
driven program to: display capital of the given country.
print the tabular report of the country and corresponding capital.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct world
{
char country[30];
char capital[30];
};
void main()
{
clrscr();
world w[10];

int i=0,n;
cout<<"Enter no. of records (<=10): ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the country: ";
gets(w[i].country);
cout<<"Enter the capital: ";
gets(w[i].capital);
}
char ch,*cont;
int flag;
cout<<"\n\t1.Search for capital\n\t2.List all Records\n\t3.Exit";
cin>>ch;
do
{
if(ch=='1')
{
cout<<"\nEnter the country : ";
gets(cont);
flag=0;

for(i=0;i<n;i++)
{
if(strcmp(cont,w[i].country)==0)
{
cout<<"\n capital is: "<<w[i].capital;
flag=1;
break;
}
}
if(flag==0)
cout<<"\ncountry not found";
}
if(ch=='2')
{
cout<<"\nCOUNTRY\t\tCAPITAL";
cout<<"\n-------\t\t-------";
for(i=0;i<n;i++)
cout<<"\n"<<(w[i].country)<<"\t\t"<<(w[i].capital);
}

cout<<"\n\t1.Search for capital\n\t2.List all Records\n\t3.Exit";


cin>>ch;
}while(ch!='3');
getch();
}

Q.3. Write a function in C++ which accepts a 2-D array of integers and its
size as arguments and displays elements which are exactly two-digit
number.
#include<iostream.h>
#include<conio.h>
void twodigit(int a[10][10],int m,int n)
{
int i,j,flag=0;
cout<<"The Two digit Numbers are: ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(a[i][j]>=10&&a[i][j]<=99)
{
cout<<a[i][j]<<ends;
flag=1;
}
}
if(flag==0)
cout<<"None";
}
void main()
{
clrscr();
int a[10][10],i,j,m,n;
cout<<"\n EnterNo. of rows: ";
cin>>m;

cout<<"\nEnter the no. of columns: ";


cin>>n;
cout<<"\nEnter the Elements of the array: ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
twodigit(a,m,n);
getch();
}

Q.4. Write a program to declare arrays for:


Name, roll no.m1, m2, m3, total for N students and print the tabular report and report card
of an individual student according to the users choice.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char *name[50],*nm;
int rno[50],m1[50],m2[50],m3[50],tot[50],i,n,trn;
cout<<"Enter no. of records: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name of Student "<<i<<": ";
gets(name[i]);
cout<<"Enter Roll No. of Student "<<i<<": ";
cin>>rno[i];
cout<<"Enter Mark1 of Student "<<i<<": ";
cin>>(m1[i]);
cout<<"Enter Mark2 of Student "<<i<<": ";
cin>>(m2[i]);
cout<<"Enter Mark3 of Student "<<i<<": ";
cin>>(m3[i]);
tot[i]=m1[i]+m2[i]+m3[i];
}
char ch,ch1;
int flag=0;
cout<<"\n\t1.Report card for particular student"
"\n\t2.List all records\n\t3.Exit";

cin>>ch;
do
{
switch(ch)
{
case '1':
cout<<"\nEnter the Roll no.";
cin>>trn;
flag=0;
for(i=0;i<n;i++)
if(trn==rno[i])
{
cout<<"\n Name: "<<name[i]<<"\t";
cout<<"Roll No.: "<<rno[i];
cout<<"\nMark1: "<<m1[i];
cout<<"\nMark2: "<<m2[i];
cout<<"\nMark3: "<<m3[i];
cout<<"\n----------";
cout<<"\nTotal: "<<tot[i];
flag=1;
}
if(flag==0)
cout<<"Record not found";
break;
case '2':
cout<<"\nRollno\tName \tMark1\tMark2\tMark3";
cout<<"\tTotal";
cout<<"\n------\t----------\t----\t----\t----";
cout<<"\t-----";
for(i=0;i<n;i++)
{
cout<<"\n"<<rno[i]<<"\t"<<name[i]<<"\t"
<<m1[i]<<"\t"<<m2[i]<<"\t"<<m3[i]<<"\t"
<<tot[i];
}
break;
}
cout<<"\n\t1.Report card for particular student"
"\n\t2.List all records\n\t3.Exit";
cin>>ch;

}while(ch!='3');
getch();
}

Q.5. Write a program to display the report card of any student and also
print the tabular report of the whole class in ascending order of total
marks.
(use structure: Roll no., Name, mark 1, mark 2, mark 3, Total).
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{
char name[30];
int roll;
int mark1;
int mark2;
int mark3;
int total;
}st[100];
main()
{
int n,ch,i,j;
char choice;
do
{
clrscr();
cout << "1. For enter "<<endl;
cout << "2. For tabular report"<<endl;
cout << "3. For Report card"<<endl;
cout << "4. For exit";
cin >> ch;
switch(ch)
{
case 1: cout << "Enter how many students ";
cin >>n;
for(i=0;i<n;i++)
{
cout << "Enter name ";
gets(st[i].name);

cout << "Enter Roll Number ";


cin >>st[i].roll;
cout << "Enter Mark1 ";
cin >>st[i].mark1;
cout << "Enter Mark2 ";
cin >> st[i].mark2;
cout << "Enter Mark3 ";
cin >> st[i].mark3;
st[i].total = st[i].mark1+st[i].mark2+st[i].mark3;
}
break;
case 2:
student temp;
for (i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if (st[j].total>st[j+1].total)
{
temp = st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
gotoxy(6,6);
cout <<"Name ";
gotoxy(16,6);
cout <<"Roll";
gotoxy(26,6);
cout <<"Mark1";
gotoxy(36,6);
cout <<"Mark2";
gotoxy(46,6);
cout <<"Mark3";
gotoxy(56,6);
cout <<"Total";
int r = 8;
for(i=0;i<n;i++)
{
gotoxy(6,r);
cout <<st[i].name;
gotoxy(16,r);
cout <<st[i].roll;
gotoxy(26,r);
cout <<st[i].mark1;
gotoxy(36,r);

cout <<st[i].mark2;
gotoxy(46,r);
cout <<st[i].mark3;
gotoxy(56,r);
cout <<st[i].total;
r++;
}
break;
case 3: int troll;
cout << "\nEnter the roll number to be searched ";
cin >> troll;
for(i=0;i<n;i++)
{
if (st[i].roll == troll)
{
cout << " \n Name "<<st[i].name;
cout << "\n Roll "<< st[i].roll;
cout << "\n Mark1 "<<st[i].mark1;
cout << "\n Mark2 "<<st[i].mark2;
cout << "\n Mark3 "<<st[i].mark3;
cout << "\n total "<<st[i].total;
}
}
break;
case 4: exit(0);
}
cout << "\n Do U want to continue";
cin>>choice;
}while(choice == 'Y' ||choice =='y');
}

Q.6. Write a menu driven program: to read an integer array.


to sort it using bubble sort.
to search an element using the binary seary.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],n,i,j,temp;
char ch;
do
{
cout<<"\n\t1.Enter Array\n\t2.Sort Array\n\t3.Search\n\t4.Display\n\t5.Exit";
cin>>ch;
switch(ch)
{
case '1':
cout<<"\nEnter no. of Elements (<=20): ";
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
break;
case '2':
cout<<"\nThe Array is Now Sorted";
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{

temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
break;
case '3':
cout<<"\nThe element to be searched";
int el;
cin>>el;
int first=0,last=n-1,mid=0,flag=0;
while(first<=last&&flag==0)
{
mid=(first+last)/2;
if(a[mid]==el)
{
flag=mid;
}
else if(a[mid]<el)
{
first=mid+1;
}
else
{
last=mid-1;
}
}
if(flag>0)
cout<<"\nThe Element is Found at: "<<++flag<<" in the sorted
array";
else
cout<<"\n No such Element";
break;
case '4':
cout<<"\n";
for(i=0;i<n;i++)
cout<<a[i]<<ends;

}
}while(ch!='5');
}

Q.7. A class student has three data members and few member functions:1.
2.
3.
4.
5.

Name.
Roll no.
Marks.
Input()
Display()

Write a menu driven program:i.


ii.

To create a file.
To print the stream according to the total marks of the student:96 or more
Computer Science.
91-95
Electronics.
86-90
Mechanical.
81-85
Electrical.
76-80
Chemical.
71-75
Civil.

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
class student
{
char name[30];
int rollno;
int marks;
public:
void input()
{
cout<<"\nEnter Name: ";
gets(name);
cout<<"Enter Rollno.: ";
cin>>rollno;
cout<<"enter marks";
cin>>marks;

}
void display()
{
cout<<"\n"<<name<<"\t"<<rollno<<"\t"<<marks<<"\t";
if(marks>=96)
cout<<"computer sc.";
else if(marks>=91&&marks<=95)
cout<<"Electronics";
else if(marks>=86&&marks<=90)
cout<<"Mechanical";
else if(marks>=81&&marks<=85)
cout<<"Electrical";
else if(marks>=76&&marks<=80)
cout<<"Chemical";
else if(marks>=71&&marks<=75)
cout<<"Civil";
else
cout<<"none";
}
};
void main()
{
clrscr();
student s;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Show Records\n\t3.Exit\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("st.dat",ios::app|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(student));
}
ofile.close();

break;
cout<<"\nName\tRollno\tMarks\tStream";
afile.open("st.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(student));
if (!afile)
break;
s.display();
}
afile.close();
break;
case '3' : exit(0);
case '2' :

}
cout<<"\n\t DO U want to continue <Y/N>: ";
cin>>ch1;
}while(tolower(ch1)!='n');
getch();
}

Q.8. Declare a class containing:* E no.


* Name.
* Salary.
* And required functions.
Write a menu-driven program: To create an array of 10 employees.
To display the record in ascending order of salary.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<iomanip.h>
class employee
{
int eno;
char name[30];
float salary;
public :
void input()
{
cout << "Enter Employee Number ";
cin >>eno;
cout << "Enter name ";
gets(name);

cout << "Enter salary ";


cin >>salary;
}
void show()
{
cout << eno << setw(20)<<name<<setw(20)<<salary<<endl;
}
float rt_sal()
{
return salary;
}
}emp[10];
main()
{
int n,ch,i,j;
char choice;
do
{
clrscr();
cout << "1. For enter "<<endl;
cout << "2. For tabular report"<<endl;
cout << "3. For exit";
cin >> ch;
switch(ch)
{
case 1: cout << "Enter how many employees ";
cin >>n;
for(i=0;i<n;i++)
{
emp[i].input();
}
break;
case 2:
employee temp;
for (i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if (emp[j].rt_sal()>emp[j+1].rt_sal())
{
temp = emp[j];
emp[j]=emp[j+1];
emp[j+1]=temp;
}
}
}
gotoxy(6,6);

cout <<"Employee Number ";


gotoxy(26,6);
cout <<"Name";
gotoxy(46,6);
cout <<"Salary"<<endl;
int r = 8;
for(i=0;i<n;i++)
{
emp[i].show();
r++;
}
break;
case 3: exit(0);
}
cout << "\n Do U want to continue";
cin>>choice;
}while(choice == 'Y' ||choice =='y');
}

Q.9. Write a menu-driven program:* to create a text file.


* to create another file using the first one which will store all the words starting with vowel.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;
do
{
cout<<"\n\t1.Enter Text\n\t2.Show Text\n\t3.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\nEnter The Text ";
gets(str);
ofile<<str;
ofile.close();
char tmp[20];
afile.open("smp.txt",ios::in);
ofile.open("vwl.txt",ios::out);
while(!afile.eof())
{
afile.getline(tmp,20,' ');
if(tmp[0]=='a'||tmp[0]=='e'||tmp[0]=='i'||tmp[0]=='o'||tmp[0]=='u')
{
ofile<<tmp;
ofile<<' ';
}
}
afile.close();
ofile.close();

case '2' :

break;
cout<<"\nFormatted text:\t";
afile.open("vwl.txt",ios::in);
while(afile)

{
afile.get(ch);
cout<<ch;
}
afile.close();
break;
case '3' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

Q.10. Write a menu-driven program:* to create a text file.


* to count member of vowels, digits & words.
* to create another file using the original which will contains the text after replacing all the blank spaces
With #.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;
do
{
cout<<"\n\t1.Create Text\n\t2.Count vowels/words/digits\n\t3.Show Text\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\n Enter The Text ";
gets(str);
ofile<<str;
ofile.close();
break;
case '2' :
char tmp1;
int v=0,d=0,w=0;
afile.open("smp.txt",ios::in);
while(!afile.eof())
{
afile.get(tmp1);
if(tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u')
v++;
if(isdigit(tmp1))
d++;
if(tmp1==' '||tmp1=='.')
w++;

}
afile.close();
cout<<"\n No of Vowels: "<<v;
cout<<"\n No of digits: "<<d+1;
cout<<"\n No of words: "<<w;
break;
case '3' :
char tmp2;
afile.open("smp.txt",ios::in);
ofile.open("spl.txt",ios::out);
while(!afile.eof())
{
afile.get(tmp2);
if(tmp2==' ')
{
ofile<<'#';
}
else
{
ofile<<tmp2;
}
}
afile.close();
ofile.close();
cout<<"\nFormatted text:\t";
afile.open("spl.txt",ios::in);
while(afile)
{
afile.get(ch);
cout<<ch;
}
afile.close();
break;
case '4' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

Q.11. Write a menu driven program:

To create a text file.


To read the file and display.
- Uppercase vowels.
- Lowercase vowels.
- Uppercase consonants.
- Lowercase consonants.
To create another file using the first file which will take only vowels from the first file.

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;
do
{
cout<<"\n\t1.Create Text\n\t2.Read from File\n\t3.create another file";
cout << "\n 4.Exit ";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\n Enter The Text ";
gets(str);
ofile<<str;
ofile.close();
break;
case '2' :
char tmp1;
afile.open("smp.txt",ios::in);
while(!afile.eof())
{
afile.get(tmp1);
if(isalpha(tmp1))
{
if (islower(tmp1))
{
if (tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u')
cout << "\n Lower case vowel "<<tmp1;

else
cout<<"\n Lower case consonants "<<tmp1;
}
if (isupper(tmp1))
{
if
(tmp1=='A'||tmp1=='E'||tmp1=='I'||tmp1=='O'||tmp1=='U')
cout << "\n Upper case vowel "<<tmp1;
else
cout<<"\n Lower case consonants "<<tmp1;
}
}

case '3' :

}
afile.close();
break;
ofile.open("smp.txt",ios::in);
afile.open("smp1.txt",ios::out);
char c;
while(ofile)
{
ofile.get(c);
c = tolower(c);
if (c=='a'||c=='i'||c=='e'||c=='o'||c=='u')
afile.put(c);
}
ofile.close();
afile.close();

case '4' : exit(0);


}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

Q.12. Declare a class containing: Name.


Address.
Telephone number.

Write a menu driven program to: Append record in a file.


Display the name & address for a given telephone no.
# include <fstream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
class telephone
{
char name[30];
char address[50];
double tno;
public :
void input()
{
cout<<"\n Enter the name ";
gets(name);
cout << "\n Enter address ";
gets(address);
cout<<"\n Enter the telephone number ";
cin>>tno;
}
void show()
{
cout << "\n Name "<<name;
cout << "\n Address "<<address;
}
double rt_tno()
{
return tno;
}
}tele;
// Function to append the records in file
void append()
{
ofstream tfile;
telephone tele;
tfile.open("tele.dat", ios :: app);
int n,i;

cout<< "Enter how many customers ";


cin>>n;
for (i =0; i<n ;i++)
{
tele.input();
tfile.write((char *)& tele,sizeof(tele));
}
tfile.close();
}
// Function to search a record in the file
void display()
{
ifstream tfile;
tfile.open("tele.dat",ios :: binary);
int no,flag;
flag = 0;
cout<< "\n Enter telephone number to be searched ";
cin>>no;
while(tfile)
{
tfile.read((char *)&tele , sizeof(tele));
if(!tfile)
break;
if (tele.rt_tno() == no)
{
tele.show();
flag = 1;
}
}
if (flag == 0)
cout<< "\n Record does not exist ";
}
void main()
{
clrscr();
int ch;
char ch1;
do
{
cout << "1. For append record ";
cout <<"\n2. For search ";
cout << "\n3. For exit";
cout<<"Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1: append();
break;

case 2: display();
break;
case 3 : exit(0);
}
cout<<"\n\t DO U want to continu<Y/N>: ";
cin>>ch1;
}while(tolower(ch1)!='n');}

Q.13. A blood bank maintains the record of doner:Name.


Address.
Blood group.

Write a menu driven program: To create file of donors.


To print the name of all the donors having given blood group.
To print the tabular list of donors.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
class donor
{
char name[30];
char address[30];
char bgroup[5];
public:
void input()
{
cout<<"\nEnter Donor Name: ";
gets(name);
cout<<"Enter Address: ";
gets(address);
cout<<"Enter Blood Group: ";
gets(bgroup);
}
void display()
{
cout<<"\nDonor Name: "<<name<<"\tAddress: "<<address<<"\tBlood Group:
"<<bgroup<<"\t";
}
char *getbgroup()
{
return bgroup;
}
};
void main()
{
clrscr();

donor d;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.List Records\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("dnr.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
d.input();
ofile.write((char*)&d,sizeof(donor));
}
ofile.close();
break;
case '2' :
cout<<"\nEnter Blood Group to be searched: ";
char bg[5],flag=0;
gets(bg);
afile.open("dnr.dat",ios::in);
while(afile)
{
afile.read((char *)&d,sizeof(donor));
if(!afile)
break;
if (strcmp(bg,d.getbgroup())==0)
{
d.display();
flag=1;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;
case '3' :
afile.open("dnr.dat",ios::in);
while(afile)
{
afile.read((char *)&d,sizeof(donor));

if(!afile)
break;
d.display();
}
afile.close();
break;

case '4' : exit(0);


}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

Q.14. Declare a class containing:

B no.
B name.
Price.
And required function.

Write a menu driven program: To add record in a file.


To modify the price of the given B no.
For material other than COTTON the above mentioned price gets reduced by 25% public
members.
A constructor to assign initial value of code, type and material with the word NOT
ASSIGNED and size & price with 0.
A function Enter() to input the values of the data members code, type, size and material
and invoke the calc price() function.
A function Show() which displays the content of all the data members for a clothing.
Write a program to read & write the object of the above class in a file.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class book
{
char bname[30];
int bno;
float price;
public:
void input()
{
cout<<"\nEnter Book Name: ";
gets(bname);
cout<<"Enter BOOK No.: ";
cin>>bno;
cout<<"Enter Price";
cin>>price;
}
void setprice()
{
cout<<"\nEnter Price";

cin>>price;
}
void display()
{
cout<<"\nBook Name: "<<bname<<"\tBook No.: "<<bno<<"\tPrice: "<<price<<"\t";

}
int getbno()
{
return bno;
}
};
void main()
{
clrscr();
book b;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Modify Records\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("bk.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
b.input();
ofile.write((char*)&b,sizeof(book));
}
ofile.close();
break;
case '2' :
cout<<"\nEnter Book No. to be searched: ";
int bn,flag=0;
cin>>bn;
afile.open("bk.dat",ios::in);
while(afile)
{
afile.read((char *)&b,sizeof(book));
if(!afile)

break;
if (bn==b.getbno())
{
b.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;
case '3' :
cout<<"\nEnter Book No. to be modified ";
int bn1,flag1=0,r=0;
cin>>bn1;
afile.open("bk.dat",ios::in|ios::out|ios::binary);
while(afile)
{
afile.read((char *)&b,sizeof(book));
if(!afile)
break;
if (bn1==b.getbno())
{
b.setprice();
afile.seekp(r*sizeof(b),ios::beg);
afile.write((char *)&b,sizeof(book));
flag1=1;
break;
}
r++;
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
break;

case '4' : exit(0);


}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

Q.15. Define a class clothing in C++ with the following description:Private members
Code
of
Type
of
Size
of
Material of
Price
of

type
type
type
type
type

string.
string.
string.
string.
string.

A function Calc_price() which calculates & assigns the value of


price as follows:For the value of material as COTTON;

Type

Price (Rs.)

TROUSER

1500

SHIRT

1200

#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
class Clothing
{
char Code[15];
char Type[15];
int Size;
char Material[20];
float Price;
public:
// Constructor to assign initial values
Clothing()
{
strcpy(Code, "NOT ASSIGNED");
strcpy(Type, "NOT ASSIGNED");
strcpy(Material, "NOT ASSIGNED");
Size = 0;
Price = 0;

}
// Function to calculate value
void CalPrice()
{
if (strcmp(Material, "COTTON") == 0)
{
Price = 1500;
}
else
if (strcmp(Material, "SHIRT") == 0)
{
Price = 1200 - (1200 * (25/100));
}
}
// Function to input the values
void Enter()
{
cout << "Enter code : ";
gets(Code);
cout << "Enter type : ";
gets(Type);
cout << "Enter size : ";
cin >> Size;
cout << "Enter Material : ";
gets(Material);
CalPrice();
}
// Function to display values
void Show()
{
cout << "Code : ";
puts(Code);
cout << "Type : ";
puts(Type);
cout << "Size : " << Size << endl;
cout << "Material : ";
puts(Material);
cout << "Price : " << Price;
}
};
void main()
{
clrscr();
Clothing C;
C.Enter();
C.Show();
}

Q.16. Declare a class sports having:o


o
o
o

S no.
S name
Fees.
Required functions.

Write a menu driven program: To append record in a file.


To delete the record of given S no.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class Sports{
char sname[30];
int sno;
float fees;
public:
void input()
{
cout<<"\nEnter sports Name: ";
gets(sname);
cout<<"Enter sports No.: ";
cin>>sno;
cout<<"Enter Fees: ";
cin>>fees;
}
void display()
{
cout<<"\nSports Name: "<<sname<<"\tSports No.: "<<sno<<"\tFees: "<<fees<<"\t";

}
int getsno()
{
return sno;
}
};
void main()
{
clrscr();
Sports s;
int n,i,j;
fstream ofile,afile;
char ch,ch1;

do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Delete Records\n\t4.Exit\n";
cout << "Enter your choice... ";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("Sport.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(Sports));
}
ofile.close();
break;
case '2' :
cout<<"\nEnter Sports No. to be searched: ";
int sn,flag=0;
cin>>sn;
afile.open("Sport.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
if(!afile)
break;
cout << s.getsno();
if (sn==s.getsno())
{
s.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;
case '3' :
cout<<"\nEnter Sports No. to be Deleted ";
int sn1,flag1=0;
cin>>sn1;
afile.open("Sport.dat",ios::in|ios::binary);
ofile.open("TSport.dat",ios::out|ios::binary);
while(afile)
{

afile.read((char *)&s,sizeof(Sports));
if(!afile)
break;
if (sn1==s.getsno())
{
flag1=1;
}
else
{
ofile.write((char *)&s,sizeof(Sports));
}
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
ofile.close();
afile.open("TSport.dat",ios::in|ios::binary);
ofile.open("Sport.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
ofile.write((char *)&s,sizeof(Sports));
}
afile.close();
ofile.close();
break;

case '4' : exit(0);


}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

Q.17. Each node of QUEUE contains:E no, Salarey, Pointer field.

Front is the first node of QUEUE & REAR is the last node.
Write a menu driven program: To add element in the queue.
To delete element from the queue.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
// Declares a queue structure
struct node
{
int Eno;
float Salary;
node *link;
};
// Functions prototype to add queue, delete queue, and show queue
node *add_Q(node *rear, int val,float val1);
// Add queue
node *del_Q(node *front, int &val, float &val1);// Delete queue
void show_Q(node *front);
// Show queue
// Main programming logic
void main()
{
node *front, *rear;
int val;
float val1;
int choice;
char opt = 'Y'; // To continue the do loop in case
front = rear = NULL;
// Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do

{
cout << "Enter the value to be added in the
queue ";
cin >> val;
cin >> val1;
rear = add_Q(rear, val,val1);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element
<Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y';
// Initialize for the second loop
do
{
front = del_Q(front, val, val1);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is "
<< val;
cout << "\nDo you want to delete more element
<Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Q(front);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
// Function body to add queue elements
node *add_Q(node *rear, int val, float val1)
{
node *temp;
temp = new node;
temp->Eno = val;
temp->Salary = val1;
temp->link = NULL;
rear->link = temp;
rear = temp;
return (rear);

}
// Function body to delete queue elements
node *del_Q(node *front, int &val, float &val1)
{
node *temp;
clrscr();
if (front == NULL)
{
cout << "Queue Empty ";
val = -1;
}
else
{
temp = front;
front = front->link;
val = temp->Eno;
val1 = temp->Salary;
temp->link = NULL;
delete temp;
}
return (front);
}
// Function body to show queue elements
void show_Q(node *front)
{
node *temp;
temp = front;
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\nENO : "<< temp->Eno;
cout <<"\nSalary : "<<temp->Salary;
temp = temp->link;
}
}

Q.18. Each node of STACK contains: Roll no.


Age.
Pointer field.

Top is the first node of STACK.


Write a menu driven program:To push.
To pop.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
// Declares a stack structure
struct node
{
int roll;
int age;
node *link;
};
// Function prototype declaration for add stack, delete stack, and show stack
node *push(node *top, int val, int tage); // Add stack
node *pop(node *top); // Delete stack
void show_Stack(node *top); // Show stack
// Main programming logic
void main()
{
node *top;
int troll, tage, choice;
char opt = 'Y';
// To continue the do loop in case
top = NULL;
// Initialization of Stack
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do

{
cout << "Enter the roll no. : ";
cin >> troll;
cout << "Enter age : ";
cin >> tage;
top = push(top, troll, tage);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
top = pop(top);
if (troll != -1)
cout << "Value deleted from Stack is " << troll;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
// Function body for adds stack elements
node *push(node *top, int val, int tage)
{
node *temp;
temp = new node;
temp->roll = val;
temp->age = tage;
temp->link = NULL;
if(top ==NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
return(top);
}
// Function body for delete stack elements

node *pop(node *top)


{
node *temp;
int tage, troll;
clrscr();
if (top == NULL )
{
cout << "Stack Empty ";
troll = -1;
}
else
{
temp = top;
top = top->link;
troll = temp->roll;
tage = temp->age;
temp->link = NULL;
cout << "\n\tPopped Roll Number is : " << temp->roll;
cout << "\n\tPopped Age is : " << temp->age;
delete temp;
}
return (top);
}
// Function body for show stack elements
void show_Stack(node *top)
{
node *temp;
temp = top;
clrscr();
cout << "The values are \n";
while (temp != NULL)
{
cout << "\n" << temp->roll << "\t" << temp->age;
temp = temp->link;
}
}

Q.19.Declare a linear stack containing integer.


Write a menu driven program to push and pop elements.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
// Shows maximum array length
int stack[MAX];
// Declares array global variable
int top;
// Declares integer top
// Function prototypes of add stack, delete stack, and
// show stack in array implementation
void push(int stack[], int val, int &top); // Add stack
int pop(int stack[], int &top); // Delete stack
void show_Stack(int stack[], int top); // Show stack
void main()
{
int choice, val;
char opt = 'Y';
// To continue the do loop in case
top = -1;
// Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above -> ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the stack ";
cin >> val;
push(stack, val, top);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{

val = pop(stack, top);


if (val != -1)
cout << "Value deleted from statck is " << val;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(stack, top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
// Function body for add stack with array
void push(int stack[], int val, int &top)
{
if (top == MAX - 1)
{
cout << "Stack Full ";
}
else
{
top = top + 1;
stack[top] = val;
}
}
// Function body for delete stack with array
int pop(int stack[], int &top)
{
int value;
if (top < 0)
{
cout << "Stack Empty ";
value = -1;
}
else
{
value = stack[top];
top = top - 1;
}
return (value);
}
// Function body for show stack with array
void show_Stack(int stack[], int top)
{

int i;
if (top < 0)
{
cout << "Stack Empty";
return;
}
i = top;
clrscr();
cout << "The values are ";
do
{
cout << "\n" << stack[i];
i = i - 1;
}while(i >= 0);
}

Q.20. Declare a linear circular queues of intger.


Write a menu driven program to add & delete elements from the queue.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 20
// Show maximum array length
char queue[MAX];
// Declares array global variable
int front, rear;
// Declares integer front and read
// Function prototypes to add queue, delete queue and show queue in array
implementation
void add_Q(char queue[], int front, char val, int &rear); // Add queue
char del_Q(char queue[], int &front, int rear); // Delete queue
void show_Q(char queue[], int front, int rear); // Show queue
void main()
{
int choice;
char val;
char opt = 'Y';
// To continue the do loop in case
rear = -1;
// Initialization of Queue
front = -1;
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the
queue ";
cin >> val;
add_Q(queue, front, val, rear);
cout << "Do you want to add more element
<Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;

case 2:
opt = 'Y';
// Initialize for the second loop
do
{
val = del_Q(queue, front, rear);
if (val != -1)
cout << "Value deleted from Queue is "
<< val;
cout << "\nDo you want to delete more element
<Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Q(queue, front, rear);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
// Function body to add circular queue with array of character
void add_Q(char queue[], int front, char val, int &rear)
{
if ((rear + 1) % MAX == front)
{
cout << "Queue Full ";
}
else
{
rear = (rear + 1) % MAX;
queue[rear] = val;
}
}
// Function body to delete circular queue with array of character
char del_Q(char queue[], int &front, int rear)
{
char value;
if (front == rear)
{
cout << "Queue Empty ";
value = -1;
}
else
{
front = (front + 1) % MAX;
value = queue[front];

}
return (value);
}
// Function body to show circular queue with array
void show_Q(char queue[], int front, int rear)
{
clrscr();
cout << "The values are ";
do
{
front = (front + 1) % MAX;
cout << "\n" << queue[front];
}while(front != rear);
}

SQL

Q-1 Write a query to display employee name, salary and department


whose address is Delhi.

Q-2 List all department id in table employee.

Q-3 List all unique department id in table employee.

Q-4 List the details of all employees whose hire date is 23 May 1990.
rd

Q-5

List the details of the employees who have four lettered name.

Q-6 List the details of all employees whose annual salary in between
900 and 10000.

Q-7 List the details of all employees who earn more than 4000.

Q-8 Write a query to display the name, salary of employees whose


department id is 50.

Q-9 Write a query to display the name of employees whose name contains
t as the last alphabet.

Q-10 Write a query to display the name of employees who is having b as


any alphabet of the address.

Q-11 Write a query to display name, salary and hire date of employees
who are hire between may 23, 1990 and December 31 1990. Order the query
in ascending order of hire date.

Q-12 write a query to display the employees name, salary and current
date.

Q-13 For each employees display the employees name and total numbers
of years lapsed between hire date and today.

Q-14 Write a query which displays the employees name with other letter
in lower case and length of these name string.

Q-15 Modify the salary and increase by 1000, for all who get salary
more than 1500.

Q-16 Delete the employee record having name as smith.

Q-1 7 Add one column email to the table employee.

Q-18 Write a query to join two Tables Employee and department on the
basis of field dept id.

Q-19 Display a details like sum, average, highest used and lowest
salary of the employee.

Q-20 Write a query to display the difference of Highest and Lowest


Salary of each Department having Maximum Salary greater than 900.

INDEX
S.NO

PROGRAMS

REMARK

Вам также может понравиться