• No results found

c++ programs for class 12

N/A
N/A
Protected

Academic year: 2021

Share "c++ programs for class 12"

Copied!
59
0
0

Loading.... (view fulltext now)

Full text

(1)

C

C

O

O

M

M

P

P

U

U

T

T

E

E

R

R

S

S

C

C

I

I

E

E

N

N

C

C

E

E

P

P

R

R

O

O

G

G

R

R

A

A

M

M

S

S

I

I

N

N

C

C

+

+

+

+

2012-2013

NAME: ALOK KUMAR

ROLL NO: 1603879

(2)
(3)

INDEX

S.No.

Contents

1.

Classes and objects

2.

Polymorphism

3.

Constructor destructor

4.

Inheritance

5.

Stacks and queue

6.

Data file management

7.

Array

(4)

CLASSES AND OBJECTS

1. Program to Define a Class Student and accessing

member function using its object.

Define a class student with the following specification: Private members:

Admno integer Sname 20 character Eng,Math,Science, float

total float

ctotal() a function to calculate eng + math + science with float return type.

Public member:

Takedata() Function to accept values for admnosnameeng science and invoke ctotal() to calculate total. Showdata() Function to display all the data members onthe

screen. #include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: intAdmno; charSname[20];

(5)

floatEngMathScience; float total; floatctotal() { returnEng+Math+Science; } public: voidTakedata() {

cout<<"Enter admission number "; cin>>Admno;

cout<<"Enter student name " ; gets(Sname);

cout<<"Enter marks in englishmathscience "; cin>>Eng>>Math>>Science; total=ctotal(); } voidShowdata() { cout<<"Admission number"<<Admno<<"\n Student name"<<Sname<<"\nEnglish"<<Eng <<"\nMath"<<Math<<"\nScience "<<Science <<"\nTotal "<<total; } }; voidmain () { clrscr(); studentobj ; obj.Takedata(); obj.Showdata(); getch(); }

(6)

2. Program to Define a Class Batsman and accessing

member function using its object.

Define a class batsman with the following specifications: Private members:

bcode 4 digits code number bname 20 characters

inningsnotoutruns integer type

batavg it is calculated according to the formula – batavg =runs/(innings-notout)

calcavg() Function to compute batavg Public members:

readdata() Function to accept value forbcodebname

inningsnotout and invoke the function calcavg() displaydata() Function to display the data members on the

screen. #include<iostream.h> #include<conio.h> #include<stdio.h> class batsman { intbcode; charbname[20]; int inningsnotoutruns; intbatavg;

(7)

voidcalcavg() { batavg=runs/(innings-notout); } public : voidreaddata (); voiddisplaydata(); }; void batsman::readdata () {

cout<<"Enter batsman code "; cin>>bcode;

cout<<"Enter batsman name "; gets(bname);

cout<<"enter inningsnotout and runs "; cin>>innings>>notout>>runs;

calcavg(); }

void batsman::displaydata() {

cout<<"Batsman code "<<bcode<<"\nBatsman name " <<bname<<"\nInnings "<<innings<<"\nNot out "<<notout <<"\nRuns "<<runs<<"\nBatting Average "<<batavg; } void main() { batsmanobj; obj.readdata(); obj.displaydata(); getch(); }

(8)

3. Program to Define a Class Test and accessing member

function using its object.

Define a class TEST in C++ with following description: Private Members:

TestCode integer Description string NoCandidate integer

CenterReqd integer(number of centers required)

CALCNTR() function to calculate and return the numberof centers as (NoCandidates/100+1)

Public Members:

SCHEDULE()- Function to accept value from TestCode,

Description, NoCandidate and invoke the function CALCNTR()

DISPTEST()- Function to display the data members on the screen. #include<iostream.h> #include<conio.h> #include<stdio.h> class TEST { intTestCode; char Description[30]; intNoCandidate; intCenterReqd; int CALCNTR() { returnNoCandidate/100+1; }

(9)

public: void SCHDULE(); void DISPTEST(); }; void TEST::SCHDULE() {

cout<<"Enter Test code "; cin>>TestCode; cout<<"Enter description "; gets(Description); cout<<"Enter no of candidates "; cin>>NoCandidate; CenterReqd=CALCNTR(); }

void TEST :: DISPTEST() {

cout<<"Test code "<<TestCode<<"\nDescripton " <<Description<<"\nNo of candidate"<<NoCandidate <<"\nCenter required "<<CenterReqd;

} void main () { TEST obj; obj.SCHDULE(); obj.DISPTEST(); getch(); }

(10)

4. Program to Define a Class BOOK and accessing

member function using its object.

Define a class BOOK with the following specifications : Private members:

BOOK NO integer type BOOKTITLE string

PRICE float

TOTAL_COST() A function to calculate the total cost for Nnumber of copies where N is passed to thefunction as argument.

Public members:

INPUT() function to read BOOK_NO. BOOKTITLE, PRICE

PURCHASE() function to ask the user to input the numberof copies to be purchased. It invokes

TOTAL_COST() and prints the total cost tobe paid by the user.

#include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int BOOKNO; char BOOKTITLE[20]; float PRICE; void TOTAL_COST(int N)

(11)

{ floattcost; tcost=PRICE*N; cout<<tcost; } public: void INPUT() {

cout<<"Enter Book Number "; cin>>BOOKNO;

cout<<"Enter Book Title "; gets(BOOKTITLE);

cout<<"Enter price per copy "; cin>>PRICE;

}

void PURCHASE() {

int n;

cout<<"Enter number of copies to purchase ";

cin>>n; cout<<"Total cost is "; TOTAL_COST(n); } }; void main() { BOOK obj; obj.INPUT(); obj.PURCHASE(); getch(); }

(12)

5

.

Define a class Travel in C++ with the description

given below

Private Members:

T_Code string

No_ of_ Adults integer

No _of _Children integer

Distance integer

TotalFare float

Public Members:

constructor to assign initial values as follows:

TCodeas “NULL” No _of_ Adults as 0 No_ of_Children as 0 Distance as 0

TotalFare as 0

AssignFare() function which calculates and assigns the value of t

he data member Totalfare as follows For each Adult Fare (Rs) For Kilometers 500 >=1000 300 <1000 &>=500 200 <500

For each Child the above Fare will be 50% of the Fare mentioned in the above table

(13)

EnterTour() function to input the values of the data members T_Code, No_of_Adults, No_of_Children and Distance ; and invoke the AssignFare() function.

ShowTravel() function which displays the content of all the data

members for a Travel.

#include<conio.h> #include<stdio.h> #include<string.h> #include<iostream.h> class Travel { charT_Code[21]; intNo_of_Adults,No_of_Children,Distance; float TotalFare; public: Travel( ) { strcpy(T_Code,"NULL"); No_of_Adults=No_of_Children=Distance= TotalFare=0; } voidAssignFare( ) { if(Distance>=1000) TotalFare=No_of_Adults*500+No_of_Children* 250; else if(Distance>=500) TotalFare=No_of_Adults*300+No_of_Children* 150; else TotalFare=No_of_Adults*200+No_of_Children* 100; }

(14)

voidEnterTravel( ) {

cout<<"\nEnter the Travel Code: "; gets(T_Code);

cout<<"\nEnter the Number of Adults: "; cin>>No_of_Adults;

cout<<"\nEnter the Number of Children: "; cin>>No_of_Children;

cout<<"\nEnter the Distance in Kilometres: "; cin>>Distance;

AssignFare( ); }

void ShowTravel( ) {

cout<<"\nThe Travel Code: "<<T_Code;

cout<<"\nThe Number of Adults: "<<No_of_Adults; cout<<"\nThe Number of Children:"

<<No_of_Children;

cout<<"\nThe Distance in Kilometres: "<<Distance; cout<<"\n\nThe Total Fare: "<<TotalFare;

} }; void main( ) { clrscr(); Travel T; T.EnterTravel( ); T.ShowTravel( ); getch(); }

(15)

POLYMORPHISM

#include<iostream.h> #include<conio.h>

float area(float a, float b) { return a*b; } float area(float a) { return a*a; } void main() { clrscr(); int choice; float a,b, ar;

cout<<"Enter 1 for area of rectangle."; cout<<"\nEnter 2 for area of square."; cout<<"\nEnter your choice:";

cin>>choice; switch(choice)

{

case 1: cout<<"Enter length and breadth of rectangle:"; cin>>a>>b;

ar=area(a,b);

cout<<"\nArea of rectangle:"<<ar; break;

case 2: cout<<"Enter length of square:"; cin>>a;

ar=area(a);

cout<<"\nArea of square:"<<ar; break;

default: cout<<"wrong choice."; }

(16)

CONSTRUCTOR DISTRUCTURE

1. Program to calculate factorial of a given number

using copy constructor.

#include<iostream.h> #include<conio.h> class copy { intvar,fact; public: copy(int temp) { var = temp; } double calculate() { fact=1; for(int i=1;i<=var;i++) { fact = fact * i; } return fact; } }; void main() { clrscr(); int n;

cout<<"\n\tEnter the Number : "; cin>>n;

copyobj(n); copycpy=obj;

cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate(); cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();

getch(); }

(17)

2. Program to Calculate Prime Number Using Constructor

#include<iostream.h> #include<conio.h> class prime { inta,k,i; public: prime(int x) { a=x; } void calculate() { k=1; { for(i=2;i<=a/2;i++) if(a%i==0) { k=0; break; } else { k=1; } } } void show() { if(k==1)

(18)

else

cout<<"\n\tA is Not prime."; } }; void main() { clrscr(); int a;

cout<<"\n\tEnter the Number:"; cin>>a; primeobj(a); obj.calculate(); obj.show(); getch(); }

(19)

3. Program to print student details using constructor and

destructor

#include<iostream.h> #include<conio.h> classstu { private: char name[20],add[20]; introll,zip; public: stu ( );//Constructor ~stu( );//Destructor void read( ); voiddisp( ); }; stu :: stu( ) {

cout<<“This is Student Details”<<endl; }

voidstu :: read( ) {

cout<<“Enter the student Name”; cin>>name;

cout<<“Enter the student roll no “; cin>>roll;

cout<<“Enter the student address”; cin>>add;

cout<<“Enter the Zipcode”; cin>>zip;

(20)

voidstu :: disp( ) {

cout<<“Student Name :”<<name<<endl; cout<<“Roll nois:”<<roll<<endl; cout<<“Address is:”<<add<<endl; cout<<“Zipcode is:”<<zip; } stu : : ~stu( ) {

cout<<“Student Detail is Closed”; } void main( ) { stu s; clrscr( ); s.read ( ); s.disp ( ); getch( ); }

Output:

Enter the student Name James

Enter the student roll no 01

Enter the student address Newyork

Enter the Zipcode 919108

Student Name : James Roll no is : 01

Address is : Newyork Zipcode is :919108

(21)

4. Program to show constructor overloading

#include<iostream.h> #include<conio.h> class temp { int a,b; public:

temp(int x, int y); temp(int x); temp(); void put(); }; temp::temp(int x, int y) { a=x; b=y; } temp::temp(int x) { a=x; b=0; } temp::temp() { a=0; b=0; } void temp::put() { cout<<"a="<<a<<"\tb="<<b<<endl; } void main() { clrscr(); temp t1(10,20); temp t2(11); temp t3; t1.put(); t2.put(); t3.put(); }

(22)

INHERITANCE

1. Program to find out the student details using multiple

inheritance.

#include<iostream.h> #include<conio.h> class student { protected: int rno,m1,m2; public: void get() {

cout<<"Enter the Roll no :"; cin>>rno;

cout<<"Enter the two marks:"; cin>>m1>>m2; } }; class sports { protected:

intsm;// sm = Sports mark public:

voidgetsm() {

cout<<"\nEnter the sports mark :"; cin>>sm;

} };

(23)

classstatement:publicstudent,public sports { inttot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3;

cout<<"\n\n\tRoll No: "<<rno<<"\n\tTotal:" <<tot; cout<<"\n\tAverage: "<<avg; } }; void main() { clrscr(); statementobj; obj.get(); obj.getsm(); obj.display(); getch(); }

Output:

Enter the Roll no: 100 Enter two marks 90

80

Enter the Sports Mark: 90 Roll No: 100

Total: 260 Average: 86.66

(24)

2. Program to find the mean value of a given number

using friend function.

#include<iostream.h> #include<conio.h> classbase { int val1,val2; public: void get() {

cout<<"Enter two values:"; cin>>val1>>val2;

}

friend float mean(base ob); };

float mean(base ob) { return float(ob.val1+ob.val2)/2; } void main() { clrscr(); baseobj; obj.get();

cout<<"\n Mean value is : "<<mean(obj); getch();

}

Output:

Enter two values: 10, 20 Mean Value is: 15

(25)

STACKS AND QUEUE

1.Program: Static queue

#include<iostream.h> #include<conio.h> #include<process.h> class queue { int data[10]; int front,rear; public: queue() { front=rear=-1; } void add(); void remove(); void display(); };

void queue :: add() { if((front==0&&rear==9)||(rear==(front-1))) { cout<<"overflow\n"; return; } int x; cout<<"value to insert\n"; cin>>x; if(rear==-1) front=rear=0; else if(rear==9) rear=0;

(26)

else rear++; data[rear]=x; } void queue::remove() { if(front==-1) { cout<<"underflow\n"; return; } int x; x=data[front]; if(front==rear) front=rear=-1; else if(front==9) front=0; else front++; cout<<x<<"deleted"; } void queue::display() { if(front==-1) cout<<"there is no element\n"; else if(front<=rear) { for(int i=front;i<=rear;++i) cout<<data[i]<<endl; } else { for(int i=front;i<=9;++i) cout<<data[i]<<endl; for(i=0;i<=rear;++i)

(27)

cout<<data[i]<<endl; } } void main() { clrscr(); queue q1; int ch; while(1) { cout<<"\n 1.add()"; cout<<"\n 2.remove()"; cout<<"\n 3.display()"; cout<<"\n 4.exit()";

cout<<"enter your choice\n"; cin>>ch; switch(ch) { case 1: q1.add(); break; case 2: q1.remove(); break; case 3: q1.display(); break; default: exit(0); } } }

(28)

2. Program: Queue with class

#include<iostream.h> #include<conio.h> #include<process.h> #define NULL 0 struct queue { int info; queue * next; }; class que { private: queue * front; queue * rear; public: queue(); void insert(); void del(); void display(); }; que:: que() {

front= rear= NULL; }

void que:: insert() {

queue * temp; temp= new queue; cout<<"enter info\n"; cin>>temp->info; temp->next=NULL;

(29)

if(rear== NULL) front= rear =temp; else { rear->next=temp; rear=temp; } }

void que :: del() { queue *temp; if(front == NULL) cout<<"underflow\n"; else if(front==rear) { temp=front; front=rear= NULL; delete temp; } else { temp=front; front= front->next; delete temp; } }

void que :: display() {

cout<<"entered data is\n";

for(queue*t = front; t; t=t->next) cout<<t->info<<"\n";

} void main()

(30)

clrscr(); que q1; int ch; while (1) sert()"; cout<<"\n 2. delete()"; cout<<"\n 3. display()"; cout<<"\n 4. exit()";

cout<<"enter your choice\n"; cin>>ch; switch (ch) { case 1: q1.insert(); break; case 2: q1.del(); break; case 3: q1.display(); break; default: exit(0); } } }

(31)

3. Program: stack-lifo

#include<iostream.h> #Include<conio.h> #include<process.h> Structemp { Char name[20]; Intsal; Emp *next; }; Emp * top=0; Void push() { emp *temp; temp= new emp;

cout<<“enter name salary”; cin>>temp->name>>temp->sal; temp->next=0; if(top= =0) top=temp; else { temp ->next=top; top=temp; } } Void pop() { emp *temp; if(top= =0) cout<<“under flow”;

(32)

else { temp=top; top=top->next; delete temp; } } Void print() { if(top!=0) for(em *t=top;t;t->next) { cout<<t->name<<t->sal; } } Void main() { Intch; While(1) { cout<<“1.pop”<<“2.push”<<“3.print”<<4.exit”<< “choice”; cin>>ch; switch (ch) { case 1:pop(); break; case 2:push(); break; case 3:print(); break; default:exit(0); } } }

(33)

4. Program: Based on QUEUE [FIFO]

#include<conio.h> #include<iostream.h> #define NULL 0 #include<process.h> structque { int info; que *next; }; que * front=NULL,*rear=NULL; void insert() { que *temp; temp=new que; cout<<"info"; cin>>temp->info; temp->next=NULL; if(rear=NULL) rear=front=temp; else { rear->next=temp; rear=temp; } } void del() { que * temp; if(front==NULL) cout<<"underflow"; else if(front++rear) { temp=front; front=rear=NULL;

(34)

delete temp; } else { temp=front; front=front->next; delete temp; } } void print() { if(front==NULL) cout<<"nothing"; else for(que * t=front;t;t=t->next) cout<<"info"<<t->info; } void main() { intch; while(1) { cout<<"1.insert"<<"2.delete"<<"3.print"<<"4.exit "<<"choice"; cin>>ch; switch(ch) { case 1: insert(); break; case 2: delete(); break; case 3: print(); break; default : exit(0); } } }

(35)

5. Program: Stack with class

#include<iostream.h> #include<conio.h> #include<process.h> # define NULL 0 struct stack { int info; stack * next; }; classst { private: stack * top; public: st(); void pop(); void push(); void display(); }; st:: st() { top= NULL; } voidst:: push() { stack * temp; temp= new stack; cout<<"enter info\n"; cin>>temp->info;

(36)

temp->next = NULL; if(top== NULL) top= temp; else { temp->next= top; top=temp; } } voidst:: pop() { stack* temp; if(top== NULL) cout<<"underflow\n"; else { temp= top; top= top->next; delete temp; } } voidst:: display() {

cout<<"entered data is\n"; for(stack*t = top; t; t=t->next) cout<<t->info<<"\n"; } void main() { clrscr(); st s1; int ch; while (1) {

(37)

cout<<"\n 1. pop()"; cout<<"\n 2. push()"; cout<<"\n 3. display()"; cout<<"\n 4. exit()";

cout<<"enter your choice\n"; cin>>ch; switch (ch) { case 1: s1.pop(); break; case 2: s1.push(); break; case 3: s1.display(); break; default: exit(0); } } }

(38)

DATA FILE MANAGEMENT

1. Program to count and display the number of

alphabets present in a text file

#include<iostream.h> #include<fstream.h> #include<ctype.h> #include<conio.h> void display() { ifstream afile; afile.open("STORY.TXT"); char ch; int c=0; while(afile) { afile.get(ch); if (isalpha(ch)) c++; }

cout<<"The number of alphabets are "<<c; } void main() { clrscr(); display(); }

(39)

2. Program to count and display the number of blank

spaces present in a text file

#include<fstream.h> #include<iostream.h> #include<ctype.h> #include<conio.h> void display() { ifstream afile; afile.open("NOTES.TXT"); char ch; int c = 0; while(afile) { afile.get(ch); if (ch == ' ' ) c++; }

cout<<"The number of blank spaces : "<<c; } void main() { clrscr(); display(); }

(40)

3. Program to calculate the average word size in a text

file.

#include<fstream.h> #include<conio.h> void calculate() { fstream tfile; clrscr(); tfile.open("Report.txt",ios::in); char arr[20]; char ch; int i=0,sum=0,n=0; while(tfile) { tfile.get(ch); arr[i] = ch; i++; if (( ch == ' ') || (ch == '.')) { I - -; sum = sum + i; i = 0; n++; } }

cout<<" Average word size is "<<(sum/n); }

void main() {

calculate(); }

(41)

4. Program to create the file of employees

# include<fstream.h> # include<stdlib.h> # include<conio.h> class EMPLOYEE {

int Empno, hra, da, net_sal; char Ename[20]; char add[20]; float basic; public : void GETIT() {

cout<<"Enter the emp number "; cin>>Empno;

cout<<"Enter the name "; cin>>Ename;

cout<<"Enter the address "; cin>>add;

cout<<"Enter the basic salary "; cin>>basic; } void CALC() { hra = (basic * 20) /100; da = (basic * 10) / 100; net_sal = basic + da + hra; }

void SHOWIT() {

(42)

CALC();

cout<<"Emp Number: "<<Empno<<"\n Name:" <<Ename<<"\n Net Salary : "<<net_sal<<endl; }

};

void create(EMPLOYEE emp) {

ofstream afile;

afile.open("Emp.dat", ios::app | ios :: binary); if (!afile)

{

cout<<"\n Unable to open the file "; exit(1); } afile.write((char *)&emp,sizeof(emp)); afile.close(); } void read_file() { ifstream afile;

afile.open("Emp.dat", ios::in | ios :: binary); if (!afile)

{

cout<<"\n File does not exist "; exit(1);

}

EMPLOYEE emp; while(afile)

{

afile.read((char *) &emp, sizeof(emp)); emp.SHOWIT();

}

afile.close(); }

(43)

void main() {

clrscr(); int n;

cout<<"Enter how many employee "; cin>>n;

EMPLOYEE emp; for (int i = 0;i<n;i++)

{ emp.GETIT(); create(emp); } read_file(); }

(44)

5. Program to print the frequency of the alphabets and the

numeric digits after reading from the text file.

# include<fstream.h> # include<stdio.h> # include<conio.h> void main()

{ char ch, fname[20]; int num, fre[26], i;

cout<<"\n enter the name of the file "; gets(fname); for (i=0;i<26;i++) fre[i] = 0; num = 0; ifstream afile(fname); if (!afile)

cout<<"\n File does not exist "<<endl; else { while (!afile.eof()) { afile.get(ch); if (ch>= 'a' && ch<= 'z') fre[ch - 'a']++; if (ch>= 'A' && ch<= 'Z') fre[ch - 'A']++; if (ch>= '0' && ch<= '9') num++; } afile.close(); for (i=0;i<26;i++)

{ cout<<"\n Frequency of "<<char(i+'A') <<" is "<<fre[i]<<endl;

}

cout<<"\n Frequency of the numeric digits" <<num<<endl;

} }

(45)

ARRAY

1. Write a program in C++ to find the maximum sum in a row

& a column and the sum of elements in a row & a column.

#include<iostream.h> #include<conio.h> void main()

{

clrscr();

void read( int [][10], int, int); int maxr( int [][10], int, int);

int maxc( int [][10], int, int); void rowsm( int [][10], int, int); void colsm( int [][10], int, int);

void disp( int, int); int a[10][10], b, d, r, c;

cout<<"enter no. of rows and columns\n"; cin>>r>>c; read( a, r, c); b = maxr(a ,r ,c); d = maxc(a, r ,c); rowsm (a ,r, c); colsm (a, r, c); disp (b, d); getch(); }

void read(int x[][10], int r, int c) {

cout<<"enter elements\n"; for(int i=0 ;i<r; ++i)

for(int j=0 ;j<c; ++j) cin>>x[i][j];

(46)

int maxr(int x[][10] ,int r, int c) {

int max=0, sum, rn; for(int i=0; i<r; ++i)

{ sum=0; for(int j=0 ;j<c; ++j) sum+=x[i][j]; if(sum>max) { max= sum; rn= i+1; } return rn; } }

int maxc(int x[][10],int r,int c) {

int max=0, sum, rn; for(int i=0; i<c; ++i)

{ sum=0; for(int j=0; j<r; ++j) sum+=x[i][j]; if(sum>max) { max= sum; rn= i+1; } return rn; } }

void rowsm(int x[][10], int r ,int c) {

(47)

for(int i=0; i<r; ++i) {

sum=0;

for(int j=0; j<c; ++j) sum+=x[i][j];

cout<<"sum of"<<i+1<<"row is"<<sum<<endl; }

}

void colsm(int x[][10] ,int r, int c) {

int sum;

for(int i=0; i<c; ++i) {

sum=0;

for(int j=0 ;j<r; ++j) sum+=x[j][i];

cout<<"sum of"<<i+1<<"column is"<<sum<< endl;

} }

void disp( int x, int y) {

cout<<"row no. having max. sum is"<<x<<endl; cout<<"column no. having max. sum is"<<y<<endl; }

(48)

2. Program related to binary search in simple notation

assuming the array to be ascending order

#include<conio.h> #include<iostream.h> void main() { int a[10]; int n,val;

void readsz(int &); void read(int *,int); void sort(int *,int); void val(int &);

int bsearch(int * , int, int); readsz( n ); read( a, n ); sort( a, n ); val( val ); cout<<bsearch(a,n,val); }

void readsz( int &x ) {

cout<<”Enter limit”; cin>>x ;

}

void read( int *, int n ) {

cout<<”enter elements”; for(int i=0; i<n; ++i) cin>>*(x+i);

(49)

void sort( int *, int n ); {

for(int i=0; i<n-1; ++i) for(int j=i+1; j<n; ++j) If(*(x+i)>*(x+j) ) { int t=*(x+J); *(x+j)=*(x+i); *(x+i)=t; } }

int bsearch(int *x, int n, int val) {

int u=0, l=n-1; int mid= (l+U)/2;

while ( ( *(x+mid)!=val && (u<=l ) ) { if(*(x+mid)>val) l=mid-1; else u=mid+1; mid=(l+u)/2; } if(*(x+mid) == val) return mid; else return -1; }

(50)

3. Write a C++ program to find the difference of the sum

of primary upper & lower diagonal.

#include<iostream.h> #include<conio.h> void main()

{

clrscr();

int a[10][10], ud=0, ld=0, r, i, j; cout<<"enter rows and columns\n"; cin>>r;

cout<<"enter values\n"; for(i=0; i<r; ++i)

for(j=0; j<r; ++j) cin>>a[i][j]; for(i=0; i<r; ++i) for(j=0; j<r; ++j) if( (i+j)>= r) { ld +=a[i][j]; } else { ud+=a[i][j];

cout<<"difference of ud and ld is"<<ud-ld; }

(51)

4. To merge two arrays, one being in ascending, second in

descending order, and resultant array should be In

ascending

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10], b[10], c[20]; int m, n;

cout<<"enter size for the first array\n"; cin>>m;

cout<<"enter elements\n"; for(int i=0; i<m; ++i) cin>>a[i];

cout<<"enter size for the second array\n"; cin>>n;

cout<<"elements for second array\n"; for(i=0; i<n; ++i)

cin>>b[i];

void sort_asc( int[], int); void sort_dsc( int[], int);

void merge(int[], int[], int[], int, int); sort_asc(a,m);

sort_dsc(b,n);

merge(a ,b ,c, m, n);

cout<<"resultant array is :\n"; for(i=0 ; i<(m+n); ++i)

cout<<c[i]<<"\t"; cout<<endl;

(52)

void sort_asc( int a[], int m) {

for(int i=0; i<(m-1); ++i) for(int j=(i+1); j<m; ++j) if(a[i]>a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; }

cout<<"array after sorting in ascending order\n"; for( i=0; i<m; ++i)

cout<<a[i]<<"\t"; cout<<endl;

}

void sort_dsc(int a[], int n) {

for(int i=0; i<(n-1); ++i) for(int j=(i+1); j<n; ++j) if(a[i]<a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; }

cout<<"array after sorting in descending order\n"; for(i=0; i<n; ++i)

cout<<a[i]<<"\t"; cout<<endl;

(53)

void merge(int a[], int b[], int c[], int m, int n) { int i=0, j=n-1, k=0; while(i<m && j>=0) { if(a[i]<b[j]) c[k++ ]= a[i++]; else c[k++] = b[j--]; } if(i==m) for(int l=j; l>=0; --l) c[k++]= b[l]; else

for(int l=i; i<m ; ++l) c[k++]=a[l];

(54)

SQL QUERIES

1. TABLE:- Student

No. Name

Age Department Dateofadm Fee Gender

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/97

300 M

7.

Surya

34

Computer

25/02/97

210 M

8.

Shikha 23

Hindi

31/07/97

200 F

1. Select * FROM Student WHERE Department = ”History”;

Output :-

2.

Shalini

21

History

24/03/98

200

F

4.

Sudha

25

History

01/07/99

400

F

6.

Shakeel

30

History

27/06/97

300

M

2. Select Name FROM Student WHERE Gender = “F”;

Output:-

2.

Shalini 21

History 24/03/98 200

F

4.

Sudha 25

History 01/07/99 400

F

8.

Shikha 23

Hindi

31/07/97 200

F

(55)

3. Select name FROM Student ORDER BY Dateofadm;

Output:-

4. Select COUNT (*) FROM Student WHERE age > 23;

Output :- 4

5. Select MAX (Age) FROM Student WHERE Gender = “F”;

Output :- 25 Sanjay Pankaj Surya Shakeel Shikha Rakesh Shalini Sudha

(56)

2. Table:- Hospital

No. Name Age Department Dateofadm Charg es Gender 1. Sandeep 65 Surgery 23/02/98 300 M 2. Ravina 24 Orthopedic 20/01/98 200 F 3. Karan 45 Orthopedic 19/02/98 200 M 4. Tarun 12 Surgery 01/01/98 300 M 5. Zubin 36 ENT 12/01/98 250 M 6. Ketaki 16 ENT 24/02/98 300 F 7. Ankita 29 Cardiology 20/02/98 800 F 8. Zareen 45 Gynecology 22/02/98 300 F 9. Kush 19 Cardiology 13/01/98 800 M 10. Shailya 31 Nuclear Medicine 19/02/98 400 F

1. Select * FROM Hospital WHERE Department = “Cardiology”;

Output :-

7. Ankita 29 Cardiology 20/02/98 800 F 9. Kush 19 Cardiology 13/01/98 800 M

2. Select Name, Charges, Age From Hospital WHERE Gender =

“M”; Output :-

Name Charges Age Sandeep 300 65 Karan 200 45 Tarun 300 12 Zubin 250 36

(57)

Kush 800 19

3. Select COUNT (*) FROM Hospital WHERE Age <30;

Output :- 5

4. Select Name FROM Hospital ORDER BY Dateofadm;

Output :- Name Tarun Zubin Kush Ravina Karan Shailya Ankita Zareen Sandeep Ketaki

5. Select SUM (Charges) FROM Hospital WHERE Dateofadm <

“12/08/98”; Output :- 3850

(58)

3. Table :- Books

Book_Id Book_Name Author_Name Publishers Price Type Qty C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5 F0001 The Teasers 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

1. Select * FROM Books WHERE Qty BETWEEN 10 and 50;

Output :-

Book_Id Book_Name Author_Name Publishers Price Type Qty T0002 C++

Brainworks

A. W. Rossaine

TDH 350 Text 15 F0001 The Teasers William

Hopkins

First Publ. 650 Fiction 20 F0002 Thunderbolts Anna Roberts First Publ. 750 Fiction 50

(59)

2. Select * FROM Books WHERE (Publishers = “EPB” OR

Publishers = “TDH”; Output:-

Book_Id Book_Name Author_Name Publishers Price Type Qty C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5 T0001 My First C++ Brian & Brooke EPB 350 Text 10 T0002 C++

Brainworks

A. W. Rossaine TDH 350 Text 15

3. UPDATE Books SET Price = Price + 50 WHERE Publishers =

“EPB”;

Select * FROM Books WHERE Publishers = “EPB”; Output :-

Book_Id Book_Name Author_Name Publishers Price Type Qty C0001 Fast Cook Lata Kapoor EPB 405 Cookery 5 T0001 My First

C++

Brian & Brooke

EPB 400 Text 10

4. Select SUM (Quantity Issued) FROM Issued;

Output :- 11

5. Select COUNT (DISTINCT Publishers) FROM Books;

References

Related documents