0% found this document useful (0 votes)
221 views54 pages

CS Prac

This document appears to be a student's practical file submission for their Computer Science course. It contains an acknowledgements section thanking various people for their help and support. It also includes a certificate confirming this is the student's bonafide practical work. The document then lists 20 questions in C++ and SQL for the student to answer as part of their practical assessment.

Uploaded by

Kanish Chugh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views54 pages

CS Prac

This document appears to be a student's practical file submission for their Computer Science course. It contains an acknowledgements section thanking various people for their help and support. It also includes a certificate confirming this is the student's bonafide practical work. The document then lists 20 questions in C++ and SQL for the student to answer as part of their practical assessment.

Uploaded by

Kanish Chugh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

DLF PUBLIC SCHOOL

SECTOR-2, RAJINDER NAGAR,


SAHIBABAD, GHAZIABAD,
UTTAR PRADESH, PIN: 201005

PRACTICAL FILE

COMPUTER SCIENCE

NAME: MAYANK GUPTA


CLASS: XII D
ADMISSION NO. : 3191
CBSE ROLL.NO. : __________________
SUBMITTED TO: MRS. ANSARA BANU
INDEX
ACKNOWLEDGEMENT

CERTIFICATE

QUESTIONS (C++)

QUESTIONS (SQL)

ANSWERS (C++)

ANSWERS (SQL)
ACKNOWLEDGEMENT:
DURING THE ACADEMIC YEAR, 2018-19, MANY PEOPLE
HELPED ME IN MY COMPUTER SCIENCE PRACTICALS. I
WOULD LIKE TO EXPRESS MY DEEP GRATITUDE TO THEM.

FIRSTLY, I WOULD LIKE TO THANK MY PARENTS, WHO


HELPED ME THROUGHOUT THIS YEAR, NOT ONLY IN
COMPUTER SCIENCE, BUT ALSO IN ALL THE OTHER
SUBJECTS AS WELL, BY PROVIDING ALL THE RESOURCES
REQUIRED, THE GUIDANCE NEEDED, AND THEIR
CONTINUOUS ENOURAGEMENT.

NEXT, I WOULD LIKE TO THANK THE AUTHOR MRS. SUMITA


ARORA, FOR WRITING THIS BRILLIANT BOOK, WHICH
HELPED ME IN UNDERSTANDING VARIOUS CONCEPTS
EASILY, AND VERY CLEARLY. THIS BOOK IS THE SOURCE
OF ALL MY KNOWLEDGE ABOUT C++.

I WOULD LIKE TO EXPRESS MY DEEP GRATITUDE TOWARDS


MY PRINCIPAL, MRS. SEEMA JERATH, WHO PROVIDED US
WITH THIS WONDERFUL OPPORTUNITY OF EXPLORING
THE LANGUAGE OF C++.

I WOULD ALSO LIKE TO THANK MY COMPUTER SCIENCE


TEACHER, MRS. ANSARA BANU, WHO ANSWERED THE
VARIOUS QUESTIONS I HAD, AND HELPED ME IN
UNDERSTANDING C++ DEEPLY AND CLEARLY.
CERTIFICATE

THIS IS TO CERTIFY THAT THIS FILE IS A


BONAFIDE PRACTICAL WORK DONE DURING
THE SEMESTER 2018-29.

NAME: MAYANK GUPTA


CLASS: XII D
ADMISSION NO. : 3191
CBSE ROLL.NO. : __________________
SUBMITTED TO: MRS. ANSARA BANU
QUESTIONS (C++)
1. Write a program (Telephone Directory simulation) that can sort a list of names and
telephone number alphabetically. Names to be treated as a unit, (Define them inside a
structure). Persons are to be sorted alphabetically by their last names. Persons with the
same last name are sorted by their first names.
2. Write a C++ program that uses a function to check whether a given number is divisible by
another number or not. However, if the second number is missing, the function checks
whether the first number is prime or not.
3. Write a Program Using a Class to Store a Price List of 50 Items and to print the Largest Price
along with The Sum of All Prices.
4. Write A Program To Hold Details Of 10 Students And Provide The Facility Of Viewing Details
Of The Topper As Well As Of Specific Student By Providing His / Her Roll Number.
5. Write A Program To Illustrate The Use Of Objects As Arguments To Member, And Non-
Member Functions Using Call By Value Mechanism.
6. Write A Program To Illustrate The Call By Reference Mechanism On Objects.
7. Write a C++ program using classes and objects to simulate result preparation system for 20
students. The data available for each student includes rollno, name and marks in 3
subjects. The percentage marks and grade are to be calculated from the above
information. The percentage marks are the average marks and the grade is calculated as
follows:
Percentage marks Grade
< 50 ‘F’
>=50 < 60 ‘D’
>=60 < 75 ‘C’
>=75 <90 ‘B’
>=90 <100 ‘A’

8. Write a program to illustrate order of constructor invocation.


9. Write a program to use an overloaded constructor.
10. Define a class Garments in C++ with the following descriptions:
Private Members:
 GCode of type string
 GType if type string
 GSize of type integer
 GFabric of type string
 GPrice of type float
 A function Assign() which calculates and assigns the value of GPrice as follows:
For the value of GFabric “COTTON”,

GType GPrice (Rs)

TROUSER 1300

SHIRT 1100

For GFabric other than “COTTON”, reduce GPrice mentioned above by 10%.

11. Define a class ‘Travel’ in C++ with the description given below:

Private Members:

o T_Code of type string


o No_of_Adults of type integer
o No_of_Children of type integer
o Distance of type integer
o TotalFare of type float

Public Members:

o A constructor to assign initial values as follows:


T_Code with the word “NULL”
No_of_Adults as 0
No_of_Children as 0
Distance as 0
TotalFare as 0
o A function AssignFare () which calculates and assigns the value of the data member
TotalFare as follows:

 For each Adult For distance


Fare (Rs)
(km)
500 >=1000
<1000 &
300
>=500
200 <500

 For each Child, the above Fare will be 50% of the Fare mentioned in the
above table.
o A function EnterTravel () to input the values of the data members T_Code,
No_of_Adults, No_of_Children and Distance; and invoke the AssignFare function.
o A function ShowTravel () which displays the content of all the data members for a
Travel.
12. A college maintains a list of its students graduating every year. At the end of the year, the
college produces a report that lists the following:
Year ______________________
Number of working graduates :
Number of non-Working graduates:
Details of the Top-most scorer:
Name :
Age :
Subject :
Average marks :
X % of the graduates this year are non-working and n % are first-divisioners.
Write a C++ program for it that uses the following inheritance path:
Person ----------------------- Student -------------------------- Graduate student
(Name Age) (rollno, average marks) (subject, employed)
The data members of these classes are shown in the parenthesis.
13. (a) Write a function in C++ to count the no. of “He” or “She” words present in a text file
“STORY.TXT”.
(b) Write a function in C++ to search for a camera from a binary file “CAMERA.DAT”
containing the objects of class CAMERA (as defined below). The user should enter the
model no. and the function should search and display the details of the camera.
Class CAMERA
{ long ModelNo;
float MegaPixel;
int Zoom;
char Details [120];

public:

void Enter () {cin>>ModelNo>>MegaPixel>>Zoom; gets (Details) ;}


void Display () {cout<<ModelNo<<MegaPixel<<Zoom<<Details<<endl ;}

long GetModelNo () {return ModelNo;}

};

14. Write a program to display the details of the salesperson who has made the maximum total
sales.
15. Define a string class having a member function upit () that converts the string to all upper
case. You can use the toupper () library function, which takes a single character as an
argument and returns a character that has been converted to upper case. To use this
function, CTYPE.H header file is included. Write some code in main () to test this function.
16. Write a C++ program to find the longest consecutive sequence of integers with no primes.
The program should accept a number N and print out the largest block of integers
between 2 and N with no primes.
17. Write a program that obtains the rotation factor and rotates the array as per it. For
example, if the given array is 1, 2, 3, 4, 5, 6, 7and the rotation factor is 2, then array’s
element will shift right by 2 elements and the last two elements will rotate and come to
positions 1 and 2, i.e. array will be 6, 7, 1, 2, 3, 4, 5.
18. Write a program that inputs a line of text and a substring to search for. Then it should display
the position of first and last occurrence of the word.
19. Write a complete program in C++ to implement a dynamically allocated Stack, which
contains names of countries.
20. Write a complete program in C++ to implement dynamically allocated Queue containing
names of cities.

QUESTIONS (SQL)
1. Given the following student relation :
No. Name Age Department Dateofadm Fee Sex
1. Pankaj 24 Computer 10/01/97 120 M
2. Shalini 21 History 24/03/98 200 F
3. Sanjay 22 Hindi 12/12/96 300 M
4. Sudha 25 History 01/07/99 400 F
5. Rakesh 22 Hindi 05/09/97 250 M
6. Shakeel 30 History 27/06/98 300 M
7. Surya 34 Computer 25/02/97 210 M
8. Shikha 23 Hindi 31/07/97 200 F
To display the
Write SQL commands:
a) To show all information about the students off History department.
b) To list the names of female students who are in Hindi department.
c) To list names of all students with their date of admission in ascending order.
d) To display student’s Name, Fee, Age for male students only.
e) To count the number of students with Age <23.
f) To insert a new row in the STUDENT table with the following data:

9, “Zaheer”, 36, “Computer”, {12/03/97}, 230, “M”

2. Given the following tables for a database LIBRARY:


Table: BOOKS

Book_Id Book_Name Author_Name Publishers Price Type Qty.


C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5
F0001 The Tears William Hopkins First Publ. 650 Fiction 20
T0001 My First C++ Brian & Brooke EPB 350 Text 10
T0002 C++ Brainworks A.W. Rossaine TDH 350 Text 15
F0002 Thunderbolts Anna Roberts First Publ. 750 Fiction 50
Table: ISSUED

Book_Id Quantity Issued


T0001 4
C0001 5
F0001 2

Write SQL queries:

a) To show Book name, Author name and Price of books of First Publ. publishers.
b) To list the names from books of Text type.
c) To display the names and price from books in ascending order of their price.
d) To increase the price of all books of EPB Publishers by 50.
e) To display the Book_Id, Book_Name and Quantity_Issued for all the books which
have been issued. ( The query will require contents from both the tables)

3. Consider the following DEPT and WORKER tables.

Table: DEPT
DCODE DEPARTMENT CITY
D01 MEDIA DELHI
D02 MARKETING DELHI
D03 INFRASTRUCTURE MUMBAI
D04 FINANCE KOLKATA
D05 HUMAN RESOURCE MUMBAI

Table: WORKER
WNO NAME DOJ DOB GENDER DCODE
1001 George K 2013-09-02 1991-09-01 MALE D01
1002 Ryma Sen 2012-12-11 1990-12-15 FEMALE D03
1003 Mohitesh 2013-02-03 1987-09-04 MALE D05
1007 Anil Jha 2014-01-17 1984-10-19 MALE D04
1004 Manila Sahai 2012-12-09 1986-11-14 FEMALE D01
1005 R Sahay 2013-11-18 1987-03-31 MALE D02
1006 Jaya Priya 2014-06-09 1985-06-23 FEMALE D05

Write SQL queries:


a) To display Wno, Name, Gender from the table WORKER in the descending order of
Wno
b) To display the Name of all the FEMALE workers from the table WORKER
c) To display the Wno and Name of those workers from the table WORKER who are
born between ‘1987-01-01’ and ‘1991-12-01’
d) To count and display MALE workers who have joined after ‘1986-01-01’
ANSWERS (C++)
1. #include<iostream.h>

#include<conio.h>

#include<string.h>

const int size=10;

struct Person

char fname[15];

char lname[25];

long tel;

}td[size],t;

swap ( Person &s1, Person &s2)

strcpy(t.fname,s1.fname);

strcpy(t.lname,s1.lname);

t.tel=s1.tel;

strcpy(s1.fname,s2.fname);

strcpy(s1.lname,s2.lname);

s1.tel=s2.tel;

strcpy(s2.fname,t.fname);

strcpy(s2.lname,t.lname);

s2.tel=t.tel;

int main()

clrscr();

int i, j;

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

Cout<<"Person"<<i+1<<endl;

cin>>td[i].fname>>td[i].lname>>td[i].tel;

for(i=0;i<size;i++)
{

for(j=0;j<size-1-i;j++)

{if(strcmp(td[j].lname,td[j+1].lname)>0)

swap(td[j],td[j+1]);

else if(strcmp(td[j].lname,td[j+1].lname)==0)

if(strcmp(td[j].fname,td[j+1].fname)>0)

swap(td[j],td[j+1]);

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

Cout<<"Person"<<i+1<<endl;

Cout<<td[i].lname<<" "<<td[i].fname<<", "<<td[i].tel<<endl;

return 0;

}
2. #include<iostream.h>
#include<stdlib.h>
int divide(int a, int b)
{
if (b == 0)
return -1;
if((a%b) == 0)
return 1;
else
return 0;
}
int divide(int a)
{
int j = a/2, flag = 1;
for(int i = 2; (i <= j) && (flag); i++)
{
if(a%i == 0)
flag = 0;
return flag;
}
int main()
{
system("cls");
int ch, res, a, b;
do
{
Cout << "1. Check for divisibility \n";
Cout << "2. Check for Prime \n";
Cout << "3. Exit \n"; Cout << "Enter your choice (1-3):";
cin >> ch;
Cout << "\n";
switch(ch)
case 1 : Cout << "Enter the Numerator and Denominator \n":
cin >> a >> b;
res = divide(a,b);
Cout << ((res) ? "It is" : "It is not") << "\n";
break;
case 2: Cout << "Enter the number \n";
cin >> a;
res = 0;
res = divide(a);
Cout << ((res) ? "It is" : "It is not") << "\n";
break;
case 3: break;
default : Cout << "Wrong choice !\n";
}; //end switch
} while(ch > 0 && ch < 3);
return 0;
}//end of main()
3. #include<iostream.h>
#include<stdlib.h>
class ITEM
{
int itemcode[50];
float it_price[50];
public:
initialize ();
float largest ();
float sum ();
display_items ();
};
//.........Member Function Definitions Follow........
ITEM :: initialize ()
{
for(int i = 0; i < 50 ; i++)
{
Cout << "\n" << "Item No:" << (i+1);
Cout << "\n" <<"Enter item code:”;
cin >> itemcode[i];
Cout<<"\n" <<"Enter item price:";
cin >> it_price[i];
Cout<<"\n";
}
float ITEM :: largest ()
{
float large = it_price[0]:
for(int i = 1:1<50; i++)
if(large <it_price[0))
large = it_price[i];
return large;
float ITEM :: sum () { float sum = 0; for(int i = 0;i<50; i++)
sum += it_price[i]; return sum
ITEM :: display items () { Cout <<"\nCode Price\n";
for(int i = 0; i < 50; i++) { Cout <<"\n" << itemcode[i];
Cout<<" " <<it_price[i];
Cout<<"\n";
int main() { ITEM order ;
order.initialize();
//Initialize arrays;
float total, biggest ;
int ch = 0; system("cls"); do { Cout << "\nMain Menu.\n";
Cout<<"\n1.Display largest price.";
Cout << "\n2.Display sum of prices.";
Cout << "\n3.Display item list." ;
Cout << "\nEnter your choice (1-3):" ;
cin >> ch;
switch(ch)
{ case 1: biggest = order.largest();
Cout << "The Largest price is " << biggest <<"n";
break;
case 2 : total = order.sum();
Cout << "The sum of prices is " << total <<"\n";
break;
case 3: order.display_items();
break;
default: Cout << "\nWrong choice!\n";
break;
} //end of switch
}
while(ch >= 1 && ch <= 3);
return 0;
4. #include <iostream.h>
#include<stdio.h> // for gets()
#include<stdlib.h>
class Student { private :
int rollno;
char name[25];
float marks ;
char grade;
public:
vold readStudent()
{
Cout <<"\n Enter rollno: ";
Cout<<"n Enter name:";
Cout<<"\n Enter marks: ";
cin >> rollno;
gets(name);
cin >> marks ;
}
dispStudent()
{
calcGrade(); // Invoke function to determine grade
Cout << "Roll no. : " <<rollno << endl;
Cout << "Name : " << name << endl;
Cout <<"Marks : " <<marks << endl;
Cout << "Grade : " << grade << endl;
}
Int getRollno()
{
return rollno;}
float getMarks
{
return marks;}
calcGrade
{ if(marks >= 75)
grade = '0';
else if ( marks >=60)
grade = 'A';
else if (marks >= 50)
grade = 'B';
else if (marks >= 40)
grade = 'C;
else
grade = 'F;
}
};
int main()
{
Student XIIa[10);
for(int i = 0;i<10; ++i)
{
Cout <<"\nEnter details of Student " <<i+1 <<":";
XIIa[i].readStudent ();
}
int choice, rno, pos = -1, highmarks = 0;
do
{
system("cls");
Cout <<"\n\n Main Menu\n";
Cout<<" 1. Specific student in";
Cout << " 2. Topper in " ;
Cout << " 3. Exit\n";
Cout <<"\n Enter your choice (1.3):" ;
cin >> choice;
switch(choice){
case 1: Cout << "\n Enter roll no of student whose details U want to see:";
cin >> rno;
for(i=0;i<10; ++i)
{
if (Xlla[i].getRollno() == rno) // NOTICE accessor used here
{
XIIa[i].dispStudent(); break;
}
}if (i == 10)
Cout <<"\nINVALID ROLLNO III\n"; break;
case 2: for(i = 0; i<10; ++i)
{
if (XIIa[i].getMarks() > highmarks) // NOTICE accessor used here
{pos = i;
highmarks = XIIa[i].getMarks();
}
}Xlla[pos].dispStudent();
break;
case 3: break;
default :Cout<<"\n Wrong Choice !!\n";
break;
} while (choice >=1 && choice <3);
return 0;
}
5. #include<iostream.h>
class Time {
int hrs, mins, secs;
public:
int tot_secs;
gettime(int n, int mints)
{
hrs=h;
mins = m;
secs = 5;
tot_secs = (hrs * 60) + (mins *60) + secs;
}
puttime()
{
cout << "Time is:"<<hrs <<":"<<mins <<":"<<secs <<”\n”;
}
convert(Time t, char ch);
sum(Time t1, Time t2);
int gethrs()
{ return hrs ; }
Int getmins()
{ return mins ;}
int getsecs()
{ return secs ; }
};
Time :: convert(Time t, char ch)
{ switch(ch)
{ case 'h':cout << " Time in hours is :"<<t.hrs <<":"<<t.mins <<":"<<t.secs<<" hrs \n";
break; case 'p' : cout << "Time in am/pm is :"
<<((t.hrs> 12) ? (t.hrs-12) : t.hrs) <<":"
<< t.mins <<":"<<t secs; cout << ((t.hrs > 12) ?" pm":"am") <<"\n";
break; default : cout << " Wrong choice in "; };
Time::sum(Time t1, Time t2) {
int h, m, s, sq, mq;
sq = (t1.secs + t2.secs)/60 ;
S = (t1.secs + t2.secs) %60 ;
mq = (sq + tl.mins + t2.mins)/60;
m=(sqt1.mins + t2.mins) %60 ;
h = mq ti.hrs + t2 hrs;
if (h = =24) h=0;
cout << "Total time is : " << h <<":"<<m<<":"<<s<<"\n";
}
prnvalues(Time t1)
{
/*
"cout <<" hrs : " <<t1.hrs <<endl;
// Invalid. hrs(private member), thus can't be accessed
cout << "mins : " <<t1.mins <<"\n";
//Invalid. same reason
cout << "secs : " <<t1.secs <<"\n";
//Invalid, same reason
*/
cout << "Total secs : "<<t1.tot_secs;
//Valid, totsecs can be accessed as it's public member
cout <<"n";
}
int main()
{ Time tm1, tm2;
//two objects of time created.
int tt;
tml gettime(5, 13, 27);
//get tm1
tm2.gettime(7, 48, 38);
//get tm2
cout<<"Enter h to convert in hours format, p for am/pm format: “;
char ch;
cin >>ch;
tm1.convert(tm2, ch);
//object passed to member function
tml.sum(tm1, tm2);
prnvalues(tm2);
//object passed to non-member function
return 0;
}
6. #include<iostream.h>
#include<string.h>
class Time{
int hrs, mins, secs;
char suf[4];
public:
Int tot secs;
gettime(int h, int m, ints)
{ hrs = h;
mins = m;
secs =S;
tot_secs = (hrs 60) + (mins *60) + secs; strcpy(suf, Hrs");
}
puttime()
{
cout << "Time is : " <<hrs <<":"<< mins <<":"<< secs <<’ ‘<< suf <<"\n";
}
char getsuf() { return suf ; }
convert(Time &t, char ch);
sum(Time &t1, Time &t2);
int gethrs() { return hrs ;}
int getmins() { return mins; }
int getsecs() { return secs ;}
};
Time::convert(Time & t, charch)
{ switch(ch)
{ case 'h' :if (strcmp(t.suf, "Hrs") != 0)
{ t.hrs= (strcmp (t.suf," am ") == 0) 7t.hrs : t.hrs + 12;
strcpy(t.suf,"Hrs");
cout << " Time in hours is : " <<t.hrs <<":"<<t.mins<<":"<<t.secs <<'<<t.suf <<"\n";
break;
case 'p': if (strcmp(t.suf,"Hrs") ==0)
{
(t.hrs > 12) ? strcpy(t.suf,"pm"): strcpy(t.suf,"am");
t.hrs = ((t.hrs > 12) ? (t.hrs-12) : t.hrs));
cout << "Time in am/pm is :"<<t.hrs <<":"<<t.mins<<":"<<t.secs <<"<<t.suf <<"\n";
Break;
defaul : cout << "Wrong choice \n";
vold Time::sum(Time &t1, Time 8t2)
{ int h, m, 5, sq, mq;
if (strcmp(t1.getsuf(),"pm") == 0)
{
Convert (t1,'h');
}
if (strcmp(t2.getsuf(),"p") == 0)
convert(t2,'h');
sq = (t1.secs + t2.secs)/60 ;
S = (t1.secs + t2.secs) %60 ;
mq = (sq + t1.mins + t2.mins)/60;
m = (sq + ti.mins + t2.mins)%60;
h = m + ti.hrs + t2.hrs;
if (h == 24)
h = 0;
cout << "Total time is : " << h <<":"<<m <<":<<s<<”Hrs”<<endl;
void prnvalues(Time &t1) //nonmember Function
{
/* cout << "hrs : " << t1.hrs << "\n";
//Invalid. hrs (private member)can't be accessed.
cout << "mins : " << t1.mins <<"\n";
//Invalid. same reason
cout << "secs : " <<t1.secs <<"\n";
//Invalid. same reason */
cout << "hrs:"<<t1.gethrs() <<"\n";
// Now hrs can be accessed thru accessor function.
cout << "mins :"<<t1.getmins() <<"\n";
//Valid now. same reason
cout << "secs :" <<t1.getsecs() <<"\n";
//Valid now same reason
cout << "Total secs :"<<t1.tot_secs << '\n';
//Valid. tot_secs is a public member.
int main()
{ Time tmi, tm2;
//two objects of time created.
tm1.gettime(15, 13, 27);
//get tm1
tm2. gettime(7, 48, 38);
//get tm2
cout <<"Enter h to convert in hours format, p for am/pm format: ";
char ch ;
cin >> ch;
cout << "Converted times are :\n";
cout <<"Time 1:";
tm1.convert(tmi, ch);
cout << "Time 2:";
tm2.convert(tm2, ch);
tmi.sum(tmi, tm2);
prnvalues(tm2);
return 0;
}
7. #include <iostream.h>
const int Obj = 20;
const int size = 3;
class Student{
Int rollno;
char name[21];
float marks[size] :
float perc;
char grade:
public:
void getval (void)
{
char ch;
cout <<"Enter Data :”<<"\n" ;
cout<<" Roll No :";
cin >> rollno;
cin.get(ch);
// to empty buffer for following getline)
cout << "\n" <<"Name:";
cin.getline(name, 21);
for(int i = 0; i < size ; i++)
{
cout << "\nMarks for subject " << (i+1) <<":";
cin >> marks[i];
}
cout << "\n";
}
void calculate (void);
void prnresult (vold);
void student :: calculate (void)
{
float total; total = marks[0] + marks[1] + marks[2];
perc = total/3;
if (perc < 50)
grade = 'F';
else if (perc < 60)
grade = 'D';
else if (perc <75)
grade = “C'';
else if (perc < 90)
grade = 'B';
else
grade = 'A';
}
void student :: prnresult (void)
{
cout <<"\n";
cout <<" Rollno: " << rollno << "\n";
cout << "Name:";
cout.write(name, 21);
cout<<"\n";
cout << “Marks in subject 1: “<<marks[0] << “\n”;
cout << "Marks in subject 2: " <<marks[1] << “\n”;
Cout << ”Marks in subject 3: ”<<marks[2]<<”\n”;
cout <<"Total Marks : " << (marks[0] + marks[1] + marks[2]) <<"\n”;
cout << "Percentage Marks" << perc <<"\n";
cout <<" Grade : " << grade <<"\n";
cout << "\n";
}
student std 10[Obj];
int main()
{
int i = 0; //Read Information for 20 students
for (i = 0;1<Obj ; i++)
{
cout << "Student " << (1+1) << "\n";
std 10[i].getval();
}
for (i = 0; i < Obj; i++) //calculate and print results of students
{
std 10[i].calculate();
cout << "Result of students " << (i+1) <<"\n";
std 10[i].prnresult();
return 0;
}
8. #include<iostream.h>
#include<stdlib.h>
class Subject {
int days;
int subjectno;
public :
Subject(int d = 123, int sn = 101); //prototype
void printsub(void)
{
cout << "Subject No : " << subjectno;
cout << "\n" <<"Days :" << days << "\n";
}
};
Subject :: Subject(int d, int sn)
{
cout << "Constructing Subject\n";
days = d;
subjectno = sn;
}
class Student {
int rollno;
float marks ;
public:
Student()
{
cout << "Constructing Student\n";
rollno = 0;
marks = 0.0 ;
}
void getval(void)
{
cout <<"Enter roll number and marks:" ;
cin >> rollno >> marks ;
cout <<"\n";
}
void print(void)
{
cout << "Roll no :"<<rollno;
cout<<"\n" << " Marks : " <<marks <<"\n";
};
class Admission {
Subject sub;
Student stud;
float fees;
public:
Admission() //constructor
{
cout << "Constructing Admissionin ";
fees = 0.0 ;
}
vold print(void)
{
stud.print();
sub.printsub();
cout << "Fees : " << fees <<"\n";
}
};
int main()
{
system("cls");
Admission adm;
cout << "\nBack in main()\n";
return 0;
}
9. #include<iostream.h>
#include<stdlib.h>
class Deposit {
long int principal ; // principal amount
int time; // time in years
float rate ; // rate of interest
float total amt ; // return amount
public:
Deposit();
Deposit(long p, int t float);
Deposit(long p, int t);
Deposit(long p, float r);
void calc_amt(void);
void display(void);
};
Deposit::Deposit()
{ principal = time = rate=0.0; }
Deposit::Deposit(long p, Int t, float r)
{ principal = p; time = t; rate=r; }
Deposit::Deposit(long p, int t)
{ principal = p; time = t; rate = 0.08; }
Deposit: Deposit(long p, float r)
{ principal = p; time = 2; rate= ;)
void Deposit::calc_amt(void) { total_amt = principal + (principal time*rate)/100;}
void Deposit::display(void)
{
cout <<"\nPrincipal Amount: Rs." << principal;
cout <<"\tPeriod of investment:" << time << "years;
cout <<"\nRate of interest: "<<rate;
cout << "\tTotal Amount: Rs. " << total amt << "\n";
}
int main()
system ("cls");
Deposit D1, D2(2000,2,0.07f), D3(4000,1), D4(3000,0.12);
//D1 invokes constructor #1, D2 invokes constructor #2
//D3 invokes constructor #3, D4 invokes constructor #4
D1.calc_amt();
D2.calc_amt();
D3.calc_amt();
D4.calc_amt();
cout << "Object 1\n"; D1.display();
cout << "Object 2\n"; D2.display();
cout << "Object 3\n"; D3.display();
cout << "Object 4\n"; D4.display();
return 0;
10. class Garments
{
char GCode[15];
char Type[15] ;
int GSize ;
char GFabric[15] ;
float GPrice ;
void Assign ()
{ if (strcmp(GFabric, "COTTON") == 0)
{ if(strcmp(GType," TROUSER") ==0)
GPrice = 1300;
else if (strcmp(GType," SHIRT ") == 0)
GPrice = 1100;
}
else {
if(strcmp(GType," TROUSER") == 0)
GPrice = 1300 - 0.10 - 1300;
else if(strcmp(GType," SHIRT ") == 0)
GPrice = 1100 - 0.10 * 1100;
}
}
Public:
Garments()
{ strcpy(GCode," NOT ALLOTED");
strcpy(GType," NOT ALLOTED");
strcpy(GFabric, " NOT ALLOTED");
GSize = 0;
GPrice = 0;
}
void Input()
{
cout << "Enter Garment Code";
cin >> GCode;
cout<<"\n Enter Garment Type (TROUSER/SHIRT)";
cin >> GType ;
cout<<"\n Enter Garment Size:";
gets(GSize);
cout << "\n Enter Garment Fabric : ";
gets(GFabric);
Assign();
}
Void Display()
{
cout<<” Garment code:” <<GCode<<endl;
cout<<” Garment type:” <<GType<<endl;
cout<<” Garment size:” <<GSize<<endl;
cout<<”Garment Fabric:” <<GFabric<<endl;
cout<<” Garment Price:” <<GPrice<<endl;
}
}

;
11. class Travel
{ char T_Code[5];
int No_of_Adults; int No_of_Children;
int Distance;
float TotalFare;
public:
Travel()
{
strcpy(T_Code," NULL");
No_of_Adults = 0;
No_of_Children = 0;
Distance = 0;
TotalFare = 0;
}
void AssignFare()
{
float fare = 0;
if(Distance >= 1000)
fare = No_of_Adults * 500+No_of_Children * 250;
else if(Distance >= 500)
fare = No_of_Adults - 300+No_of_Children * 150 ;
else
fare = No_of_Adults 200+No_of_Children * 100;
TotalFare = fare ;
}
void Enter Travel
{
cout <<"Enter value of travel code:";
cin>>T_Code;
cout <<"Enter No. of Adults: ";
cin >> No_of_Adults;
cout <<"Enter No. of Children: ";
cin >> No_of_Children ;
cout<<"Enter Distance";
cin >> Distance ;
AssignFare();
}
void ShowTravel()
{
cout << "Travel code : " <<T_Code << endl;
cout << " No. of Adults : " << No_of_Adults << endl;
cout << " No. of Children: " << No_of_Children << endl;
cout << " Distance:" << Distance << endl;
cout <<" Total Fare : " << TotalFare << endl;
}
}

;
12. #include<iostream.h>
#include<stdio.h>
// for gets
const int LEN = 25;
class Person {
char name[LEN];
int age;
public:
void readperson(void);
void displayperson(void)
{
cout <<"Name:";
cout.write(name, LEN); //To print white spaces
cout << "\tAge : " << age <<"\n";
}; void Person::readperson(void)
{ for (int i = 0; i < LEN;i++)
name[i] =’ ‘;
cout<<"Enter Name of the Person :" ;
gets(name);//gets reads white spaces also
cout <<"Enter Age:";
cin >> age;
}
class Student : public Person
{
int rollno;
float average ;
public:
void readstudent(void)
{
readperson :
cout <<"Enter Roll number :";
cin >> rollno;
cout <<"Enter Average Marks :";
cin >> average ;
void disp_rolino(void)
{
cout << " Roll Number :" << rollno <<"\n";
}
float getaverage(void) //accessor
{return average ; }
class GradStudent : public Student
{
char subject[LEN);
char working;
public:
void readit(void);
void displaysubject(void)
{
cout << "Subject : ";
cout.write(subject,LEN);
}
char workstatus(void)
{ return working ;}
};
void GradStudent::readit(void)
{
readstudent();
for(int i = 0; i < LEN;i++) subject[i]="";
cout<<"Enter Main Subject:" ;
gets(subject);
cout << " Working ?(Y/N): ";
cin >> working;
}
int main {
const int size = 5;
GradStudent grad[size];
//size stores no. of graduates
//list to store information of graduates
int year, num_working = 0, non_working = 0, div1 = 0, total = 0;
float topscore = 0, score, number,wperc, nwperc;
/* **********************************************************************************
num_working stores the number of working graduates
nonworking stores the number of non-working graduates
div1 stores the number of first divisioners
total stores the total number of graduates
topscore stores the highest average
score stores the average of the Graduate student
Number stores the ith number of the Graduate in the list
********************************************************************************** */
cout<<"Enter Year : ",
cin >> year;
// Loop for processing graduates information
for(int i = 0; i < size; I++) //to read information for ith graduate
{
cout<<"Enter Details for Graduate" << (1+1) <<"\n";
grad[i].readit();
Total++; //add to total
// Loop to count number of working and non-working students if((grad[i].workstatus() ==
‘y’)|| (grad[i].workstatus() == ‘Y’))
num_working++;
else non_working++;
//Determine the top scorer
score = grad[i].getaverage();
if(score> topscore)
{ topscore = score ; number = 1; }
//count the number of first divisioners
if(score>=60.0) divi++;
} //end for loop
// Now Print the Report
i = number;
cout << "\n" <<"\t\tReport for the Year " <<year <<"\n";
cout <<"\t\t --- cout << " Working Graduates :" <<num_working;
cout <<"\tNon-working Graduates :<<non_working <<"\n";
cout << " \tDetails of the Top Scorer \n";
grad[i].displayperson();
grad[i].displaysubject() ;
nwperc = ((float)non_working/(float) total)*100;
wperc = ((float)divi/(float) total)*100;
cout <<"\tAverage Marks : " <<grad[i].getaverage() << "\n";
cout <<\t <<nwperc <<"% of the graduates this year are non_working and \n”
<< wperc <<"% are first divisioners\n";
return 0; }
//end of main()
13. A.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
Int count();
// for exit()
ifstream ifile("story.txt");
if(lifile)
{
cout << "could not open story.txt file";
exit(-1);
}
else
{
char s[20];
int cnt = 0;
while(lifile.eof())
{
ifile >> S;
if((strcmp(s, "He") == 0) 1l (strcmp(s, "She") == 0))
cnt++;
}
Ifile.close();
cout << "Count of He/She in file is : " <<cnt ;
return cnt ;
}
B.
void search(long MNO)
{
Ifstream ifile ("CAMERA.DAT", los::inios::binary);
if(!ifile)
{
cout << "could not open CAMERA.DAT file";
exit(-1);
}
else
{
CAMERA C;
int found = 0;
while(ifile.read((char*)&c, sizeof(c)))
{
if(c.GetModel No() == MNO)
{
c.Display();// display details of model found
found = 1;
break;
}
}
}
if(found == 0)
cout << "Given ModelNo not found" ;
14. #include<iostream.h>
#include<string.h>
class Salesman {
char name[11];
float qi_sal, 42_sal, q3_sal, q4_sal, tot_sal;
public:
Salesman()
{
strcpy(name," ");
q1_sal = 42_sal = 43_sal = 44_sal = tot_sal = 0;
}
void getdata(char*s, float i, float j, float k, float 1)
{
strcpy(name, 5); qi_sal = i; 42_sal = ] ; q3_sal = k; 44_sal = 1;
}
void calc_tot()
{
tot_sal = q1_sal +q2_sal + q3_sal+q4_sal;
}
char *get_name()
{ return name ; }
float get_q1()
{return qi_sal; }
float get_q2()
{return q2_sal; }
float get_q3()
{ return q3_sal; }
float get_q4()
{return q4_sal; }
float get_tot()
{ return tot_sal; }
Salesman *max_sal(Salesman *S) //function that makes use of this
{ if (IS) S = this; //If Sis null, then S stores current object
else
{ float f1, f2;
f1 = S->get_tot(); //passed pointer's total sales
f2 = this->get_tot(); //current objects total sales
if (fl <f2) //if the passed object's sales is less than this
S = this; //object sales, 5 points to this object
}
return S;
}
};
//S pointer points to the object with Maximum Sales
//object pointer
Salesman *sp;
void printit(Salesman *sp)
{
cout << "\nSalesman with Maximum Sales\n";
cout<<"Name:";
char *ss = sp ->get_name();
cout.write(ss, 11).put("\n');
cout << "Total Sales : " <<sp ->get_tot( ) << "\n";
}
int main()
{
Salesman Raman, Sita, Vedant, Anubhav, Bina; //objects declared float qi, 42, 43,
44;
//Object pointer initialized
sp = & Raman;
cout <<"Enter sales in four quarters for Raman\n";
cin >>q1 >> q2 >> q3 >> q4;
Raman.getdata(" Raman", q1, 42, 43, 44); //Initialize object Raman.calc_tot ;
//Calculate total sales
sp = Raman.max_sal(sp);
//sp store the pointer returned
cout<<"Enter sales in four quarters for Sita\n";
cin >> q1 >> q2 >> q3 >> q4;
Sita.getdata("Sita", q1, q2, q3, q4);
Sita.calc_tot();
sp = Sita.max_sal(sp);
cout <<"Enter sales in four quarters for Vedant\n”;
cin >> q1 >> q2 >> q3 >> q4;
Vedant.getdata (“Vedant", 91, 92, 93, 94);
Vedant.calc_tot();
sp = Vedant.max_sal(sp);
cout <<"Enter sales in four quarters for Anubhav\n”;
cin >> q1 >> q2 >> q3 >> q4;
Anubhav.getdata(" Anubhav ", q1, 42, 43, 4);
Anubhav.calc_tot();
sp = Anubhav.max_sal(sp);
cout << "Enter sales in four quarters for Bina\n";
cin >> q1 >> q2 >> q3 >> q4;
Bina.getdata(" Bina", q1, 42, 43, 44);
Bina.calc_tot();
sp = Bina.max_sal(sp);
//print details of salesman with maximum sales
printit(sp);
return 0;
15. #include<iostream.h>
#include<string.h>
//for strcpy(), etc
#include<ctype.h>
//for to upper()
class string
{
private:
char *str;
public: string(char *s) //constructor
{ int len = strlen(s); //length of string argument
str = new char[len+1]; //dynamically allocate memory for str
string strcpy(str, s); //copy argument to str
//destructor //release memory occupied by str
~string()
{ delete str; }
void display
{
cout << str ;}
void upit();
};
void string :: upit() //uppercase each character
{
char *sp = str; //pointer to this string
while(*sp) //until null
{ *sp = toupper(*sp); //uppercase each character
sp++; }
}
int main()
{
string S1 = "Life is a bunch of flowers" ;
cout << "\nOriginal String\n";
S1.display();
S1.upit(); //uppercase string
cout << "\nUpper case String\n";
S1.display();
return 0;
16. #include<iostream.h>
#define MAX 100
int main()
{
int isPrime[MAX + 1];
int N;
cout << "Enter N( <= 100) :";
cin >> N ;
// initially assuming all integers are prime
for(int i = 2;i<= N;i++)
isPrime[i] = 1;
//mark non-primes <= MAX using Sieve of Eratosthenes
for(i = 2;1*1 <= N;i++)
{ //if i is prime then mark multiples of i as nonprime
if (isPrime[i] == 1)
{
for(int j = 1; 1*j<= N; j++)
isPrime[i * ]] = 0;
}
}
//find longest consecutive sequence of integers with no primes
int gap = 0;
int bestgap = 0;
int right = 0;
for (i = 2;i<= N;i++)
{
If (isPrime[i]) gap = 0;
else gap++;
if (gap > bestgap)
{ bestgap = gap:
right = i;
}
int left = right-bestgap + 1;
cout << "There are no primes between" << left << "and" << right <<endl; cout << "That
is" << bestgap << "consecutive integers." << endl;
return 0;
17. #include<iostream.h>
int main()
{
int Arr[15] = {22, 31, 12, 44,5, 17, 1, 69, 11, 23, 6, 9, 23, 67,90) ;
int r;
cout << "\nEnter rotation factor (i.e. rotate by ?):" ;
cin >>r;
cout << "Originally array is :" << endl;
for (int i = 0; i < 15; i++)
cout << Arr[i] <<",";
cout << ":::" << endl;
//now rotate the array
int temp[15] ;
for(i = 14; i >= 0; i--)
{
if ((i+r) >= 15)
templi +r-15] = Arr [0];
else
temp[i+r] = Arr[i];
}
cout << "Array after Rotation by factor" <<r<< "is:"<<endl;
for(i = 0; i < 15; i++)
{
Arr[i] = temp[i];
cout << Arr[i]<<",";
}
cout << ":::" << endl;
}
18. #include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char String[50] ;
cout << "Enter the string/line of text:" ;
gets(String);
int len = strlen(String);
int i, j, last = 0;
for(i = 0; i < len ;)
{
j=i;
while(String[i] != "&& String[j]!= '\0')
j++;
last =j;
if (j%2 == 0)
last =j;
else
last =j-1;
char chi;
for(int k = i; k< last ; k += 2)
{
chi = String[k];
String[k] = String[k+1];
String[k + 1] = chi;
}
i = 3 + 1;
}
cout << String << endl;
return 0;
}
19. #include <iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct node {
char country[20];
node *link;
};
class stack {
node top;
public: stack()
{
top = NULL;
}
void push();
void pop();
void display();
~stack();
};
void stack::push()
{ node *temp = new node;
gets(temp -> country);
temp -> link = top;
top = temp;
}
void stack::pop()
{
if(top == NULL) cout << "UNDERFLOW !!!";
else
{ node *ptr = top;
top = top -> link;
delete ptr ;
}
}
void stack::display
{ node * temp = top;
while(temp != NULL)
{ cout << temp -> country <<"->";
temp = temp -> link;
}
cout << "!!!\n";
}
stack::~stack()
{ node *temp;
while(top != NULL)
{ temp = top;
top = top -> link; delete temp;
void main()
{
stack st;
int ch;
while(1)
{
cout << "Menu: \n 1) push \n 2)pop \n 3) display \n 4) Exit\n";
cout << "Enter your choice:";
cin >> ch;
switch (ch)
{
case 1: st.push();
break;
case 2: st.pop();
break;
case 3: st.display();
break;
default: cout << "Wrong choice entered" ;
}
}
}
20.#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
char city[20];
node link;
};
class queue
{
node *rear, "front;
public:
queue
{
rear = NULL;
front = NULL;
}
void push();
void pop();
void display();
~queue();
}
void queue::push()
{
node *temp = new node;
gets(temp -> city);
temp -> link = NULL;
if (rear == NULL)
{
rear = front = temp;
}
else {
rear-> link = temp;
rear = temp;
}
vold queue::pop()
{
if(front == NULL)
cout << "UNDERFLOW III";
else
{ node *ptr = front;
cout << front-> city << "deleted/n";
front = front -> link;
delete ptr ;
If (front == NULL)
{
rear = NULL;
}
}
}
void queue::display()
{
node *temp = front;
while(temp != NULL)
{
cout << temp -> city <<"->";
temp = temp -> link;
cout << "III\n";
}
}
queue :: queue()
{
node *temp;
while(front != NULL)
{
temp = front;
front = front-> link;
delete temp;
}
}
void main()
{
queue qt;
Int ch;
while(1)
{
cout << "Menu: \n 1) push \n 2)pop \n 3) display in 4) Exit\n";
cout <<"Enter your choice:";
cin >> ch;
switch (ch)
{
case 1: qt.push();
break;
case 2: qt.pop();
break;
case 3: qt.display() :
break;
case 4:
exit(0);
default: cout << "Wrong choice entered" ;
break;
}
ANSWERS (SQL)
1)
a. SELECT*FROM Student
WHERE Department= “History”
b. SELECT Name FROM Student
WHERE sex=”F” and DEPARTMENT=”Hindi”;
c. SELECT Name FROM Student
ORDER BY Dateofadm;
d. SELECT Name, Fee, Age
FROM Student
WHERE sex=”M”;
e. SELECT COUNT(*) FRIM Student
WHERE Age<23
f. INSERT INTO Student
VALUES ( 9,”Zaheer”,”Computer”, “12/03/97”, 230, “M”);
2)
a. SELECT Book_Name, Author_Name, Price
FROM Books
WHERE Publishers=”First Publ.”;
b. SELECT Book_Name
FROM Books
WHERE Type=”Text”;
c. SELECT Book_Name, Price
FROM Books
ORDER BY Price;
d. UPDATE Books
SET Price=Price+50
WHERE Publishers-“EPB”;
e. SELECT Books.Book_Id, Book_Name, Quantity_Issued
FROM Books, Issued
WHERE Books.Book_Id=Issued.Book_Id;
3)
a. SELECT WNO, NAME, Gender,
FROM WORKER
ORDER BY WNO Desc;
b. SELECT NAME
FROM WORKER
WHERE GENDER=’FEMALE’;
c. SELECT WNO, NAME
FROM WORKER
WHERE DOB BETWEEN ‘1987-01-01’ AND ‘1991-12-01’;
d. SELECT count(*)
FROM WORKER
WHERE GENDER=’MALE’ AND DOJ > ‘1986-01-01’

You might also like