Sie sind auf Seite 1von 46

Program - 1: Write a program to input day number of a week and display the corresponding day name.

#include <iostream.h>
#include <conio.h>
void main()
{
int day;
cout << "Enter a number between 1 to 7: ";
cin >> day;
switch(day)
{
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Number entered is invalid" << endl;
}
getch();
}

OUTPUT:

2
Program - 2: Write a program to implement the concept of multiple inheritance.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person
{
char name[21];
int age;
public:
void indata()
{
cout<<"\nEnter the name of Student: " ;
gets(name);
cout<<"\nEnter the age : ";
cin>>age;
}
void outdata();
};
void person::outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\nName of the student is: "<<name;
cout<<"\nAge of the student is : "<<age;
}
class game
{
char game_name[20];
public:
void input()
{
cout<<"\nEnter the game name : ";
cin.get();
cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
class student: public person, public game
{
float Tmarks;
int rollno;
public:
char calgrade()
{
if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)

3
return 'D';
else
return 'E';
}
void enter()
{
indata();
cout<<"\n\nEnter the roll number: "; cin>>rollno;
input();
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{
clrscr();
student A;
A.enter();
cout<"The details of the student are : ";
A.display();
getch();
}

OUTPUT:

4
Program - 3: Write a program to implement the concept of multi-level inheritance.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person
{
char name[21];
int age;
public:
void indata()
{
cout<<"\nEnter the name of Student: " ;
gets(name);
cout<<"\nEnter the age : ";
cin>>age;
}
void outdata();
};
void person::outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
class game : public person
{
char game_name[20];
public:
void input()
{
cout<<"\nEnter the game name : ";
cin.get();cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
class student: public game
{
float Tmarks;
int rollno;
public:
char calgrade()
{
if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';

5
else
return 'E';
}
void enter()
{
indata();
cout<<"\nEnter the roll number: "; cin>>rollno;
input();
cout<<"\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{
clrscr();
student A;
A.enter();
cout<<"The details of the student are :";
A.display();
getch();
}

OUTPUT:

6
Program - 4: Write a program to illustrate working of function overloading.

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

float area(int p)
{
return p*p;
}
float area(float p)
{
return 3.14*p*p;
}
float area(float p,float q)
{
return p*q;
}
float area(int p, int q)
{
return (0.5*p*q);
}
float area(float p,float q, float r)
{
float s;
s=(p+q+r)/2;
return sqrt(s*(s-p)*(s-q)*(s-r));
}
void main()
{
clrscr();
int p,q,ch;
float a,b,r;
char choice;
do
{
cout<<"\n\nChoose from the following : ";
cout<<"\n\n1. Area of square ";
cout<<"\n\n2. Area of circle ";
cout<<"\n\n3. Area of rectangle ";
cout<<"\n\n4. Area of right triangle ";
cout<<"\n\n5. Area of triangle ";
cout<<"\n\nEnter your choice : "; cin>>ch;
switch(ch)
{
case 1: cout<<"\n\nEnter side of square : ";
cin>>p;
cout<<"\n\narea of square is : "<<area(p);
break;
case 2: cout<<"\n\nEnter radius of circle : ";
cin>>a;
cout<<"\n\narea of circle is : "<<area(a);
break;
case 3: cout<<"\n\nEnter length of rectangle : ";
cin>>a;
cout<<"\n\nEnter breadth of rectangle : ";
7
cin>>b;
cout<<"\n\narea of rectangle is : "<<area(a,b);
break;
case 4: cout<<"\n\nEnter base and altitude of right triangle : ";
cin>>p;
cin>>q;
cout<<"\n\narea of right triangle is : "<<area(q,p);
break;
case 5: cout<<"\n\nEnter sides of triangle : ";
cin>>a;
cin>>b;
cin>>r;
cout<<"\n\narea of triangle is : "<<area(a,b,r);
break;
}
cout<<"\n\nWant to choose again : ";
cin>>choice;
}
while(choice=='y'||choice=='Y');
getch();
}

OUTPUT:

8
Program - 5: Write a program to demonstrate the concept of array of objects.
#include<iostream.h>
#include<conio.h>
class rec
{
private:
int I;
int b;
public:
rec(int a,int c)
{
I=a;
b=c;
}
void put()
{
cout<<"Area is : "<<I*b <<endl;
}
};
void main()
{
clrscr();
rec obj[3]={rec(3,6),rec(2,5),rec(5,5)};
cout<<"Displaying Areas of Rectangles : \n";
for(int i=0;i<3;i++)
{
obj[i].put();
}
getch();
}

OUTPUT:

9
Program - 6: Write a program to demonstrate the concept of Constructor overloading.
#include <iostream.h>
#include <conio.h>
class Area
{
private:
int length;
int breadth;
public:
// Constructor
Area(): length(5), breadth(2){ }
void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}
int AreaCalculation()
{
return (length * breadth);
}
void DisplayArea(int temp)
{
cout << "Area: " << temp;
}
};
void main()
{
clrscr();
Area A1, A2;
int temp;
A1.GetLength();
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
cout << endl << "Default Area when value is not taken from user" << endl;
temp = A2.AreaCalculation();
A2.DisplayArea(temp);
getch();
}

OUTPUT:

10
Program - 7: Write a program to demonstrate the concept of Copy constructor.
#include <iostream.h>
#include <conio.h>
class copy
{
private:
int X;
int Y;
public:
copy (int a, int b);
copy (const copy &d);
void Display();
};
copy:: copy(int a, int b)
{
X = a;
Y = b;
}
copy:: copy(const copy &d)
{
X = d.X;
Y = d.Y;
}
void copy:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}
int main()
{
copy d1(10,20) ;
cout << endl <<"D1 Object: " << endl;
cout << "Value after initialization : " ;
d1.Display();
copy d2 = copy(d1);
cout << endl << "D2 Object: " << endl;
cout << "Value after initialization : ";
d2.Display();
getch();
}

OUTPUT:

11
Program - 8: Write a program to create a single file and display its contents.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
system("cls");
ofstream fout("student", ios::out);
char name[30], ch;
float marks =0.0;
for(int i=0;i<5;i++) //Loop to get 5 records
{
cout<<"Student "<<(i+1)<<":\tName: ";
cin.get(name,30);
cout<<"\t\tMarks: ";
cin>>marks;
cin.get(ch); //to empty input buffer write to the file
fout<<name<<"\n"<<marks<<"\n";
}
fout.close(); //disconnect student file from fout
ifstream fin("student", ios::in); //connect student file to input stream fin
fin.seekg(0); //To bring file pointer at the file beginning
cout<<"\n";
for(i=0;i<5;i++) //Display records
{
fin.get(name,30); //read name from file student
fin.get(ch);
fin>>marks; //read marks from file student
fin.get(ch);
cout<<"Student Name: "<<name;
cout<<"\tMarks: "<<marks<<"\n";
}
fin.close(); //disconnect student file from fin stream
getch();
}

OUTPUT:

12
Program - 9: Write a program to find the sum of even/odd numbers using recursion.
#include<iostream.h>
#include<conio.h>

int sum_even(int );
int sum_odd(int );
int sum=0;
int num=0;

void main()
{
clrscr();
int n;
int s_e, s_o;
cout<<"\n\nEnter the number upto which you want the sum of even/odd numbers : ";
cin>>n;
cout<<"\n\n The list of integers is : \n\n";
for(int l=1; l<=n; l++)
cout<<l<<"\n";
s_e=sum_even(n);
s_o=sum_odd(n);
cout<<"\n\nThe sum of even numbers upto "<<n<<" = "<<s_e;
cout<<"\n\nThe sum of odd numbers upto "<<n<<" = "<<s_o;
getch();
}
int sum_even(int n)
{
if(num%2==0)
sum=sum+num;

if(num!=n && num<n)


{
++num;
sum_even(n);
}
return sum;
}
int sum_2=0;
int num_2=0;
int sum_odd(int n)
{
if(num_2%2!=0)
sum_2=sum_2+num_2;
if(num_2!=n)
{
num_2++;
sum_odd(n);
}
return sum_2;
}

13
OUTPUT:

14
Program - 10: Write a program using pointers to find the smallest and the largest element in a
dynamically created array.
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int *array, smallest, largest, n;
cout<<"\n\nEnter the number of elements : ";
cin>>n;

array=new[n];

cout<<"\n\nEnter the elements : \n\n";


for(int i=0; i<n; i++)
cin>>array[i];
i=0;
cout<<"\n\nThe array formed is : \n\n";

while(i!=n)
{
cout<<array[i]<<" ";
i++;
}

smallest=array[0];

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


{
if(array[i]<=smallest)
smallest=array[i];
}

largest=array[0];

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


{
if(array[i]>=largest)
largest=array[i];
}

cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is :


"<<largest;

getch();

15
OUTPUT:

16
Program - 11: Write a program to swap two integers using pointers.
#include<iostream.h>
#include<conio.h>

void swap_using_pointers(int *, int *);

void main()
{
clrscr();

int a,b;

cout<<"\n\nEnter first integer : "; cin>>a;


cout<<"\n\nEnter second integer : "; cin>>b;

swap_using_pointers(&a,&b);

cout<<"\n\nNow value of first integer = "<<a;


cout<<"\n\nNow value of second integer = "<<b;

getch();

void swap_using_pointers(int *a,int *b)


{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

OUTPUT:

17
Program - 12: Write a program to implement stack as an array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class stack
{
int num[10],top,max;
public:
stack()
{
top=-1;
max=9;
}
void push(int n)
{
if(top==max)
cout<<"\nStack Overflow,Push not Possible";
else
{
top++;
num[top]=n;
}
}
void pop()
{
if(top==-1)
cout<<"\nStack Underflow,Stack does not exist";
else
{
int temp=num[top];
top--;
cout<<"\nDeleted Number is:"<<temp;
}
}
void traverse();
}st;
void stack::traverse()
{
if(top==-1)
cout<<"\nStack Underflow";
else
for(int i=top;i>=0;i--)
cout<<num[i]<<" ";
}
void main()
{
clrscr();
int ch,no;
menu:
cout<<"\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter number:";

18
cin>>no;
st.push(no);
break;
case 2:
st.pop();
break;
case 3:
st.traverse();
break;
case 4:
getch();
exit(0);
default:
cout<<"\nWrong Choice!Please Re-enter";
}
goto menu;
}
OUTPUT:

19
Program - 13: Write a program to implement stack as a Linked List.
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class stack
{
int rno;
char n[42];
float per;
public:
stack *next;
void getd()
{
cout<<"\nEnter Roll No:";
cin>>rno;
cout<<"Enter Name:";
gets(n);
cout<<"Enter Percentage:";
cin>>per;
next=NULL;
}
void showd()
{
cout<<"\nRoll No:"<<rno<<"\nName:"<<n<<"\nPercentage:"<<per;
}
};
stack *newptr,*top=NULL,*ptr;
void main()
{
clrscr();
int ch;
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
newptr=new stack;
if(!newptr)
{
cout<<"\n\t\aMemory Allocation Error!";
getch();
exit(0);
}
newptr->getd();
if(top==NULL)
top=newptr;
else
{
newptr->next=top;
top=newptr;
}
break;
case 2:

20
if(top==NULL)
cout<<"\n\aStack Underflow,no element";
else
{
ptr=top;
top=top->next;
cout<<"Deleted Element:";
ptr->showd();
delete ptr;
}
break;
case 3:
ptr=top;
if(top==NULL)
cout<<"\nStack Does Not Exist";
else
while(ptr!=NULL)
{
ptr->showd();
ptr=ptr->next;
}
break;
case 4:
getch();
exit(0);
default:
cout<<"\nWrong Choice Entered,Please Re-Enter";
}
goto menu;
}

OUTPUT:

21
Program - 14: Write a program to implement Queue as an array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{const int max=10;
int ch,num,front=-1,rear=-1;
int queue[max];
clrscr();
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the number:";
cin>>num;
if(rear==max-1)
cout<<"\n\aQueue overflow";
else
{
if(front==-1)
{
front++;
queue[front]=num;
rear=front;
}
else
{
rear++;
queue[rear]=num;
}
}
break;
case 2:
if(front==-1)
cout<<"\nQueue does not exist";
else if(rear==front)
{
num=queue[front];
front=-1;
rear=-1;
cout<<"\nDeleted element:"<<num;
}
else
{
num=queue[front];
front++;
cout<<"\nDeleted Element:"<<num;
}
break;
case 3:
if(front==-1)
cout<<"\nQueue does not exist";
else

22
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
break;
case 4:
getch();
exit(0);
default:
cout<<"Wrong Choice!Please Re-enter:";
}
goto menu;
}

OUTPUT:

23
Program - 15: Write a program to implement Queue as a Linked List.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class queue
{
int rno;
char n[15];
float per;
public:
queue *next;
void getd()
{
cout<<"\nEnter Roll No:";
cin>>rno;
cout<<"Enter Name:";
gets(n);
cout<<"Enter Percentage:";
cin>>per;
next=NULL;
}
void showd()
{
cout<<"\nRoll No:"<<rno<<"\nName:"<<n<<"\nPercentage:"<<per;
}
};
queue *newptr=NULL,*ptr=NULL,*front=NULL,*rear=NULL;
void main()
{
clrscr();
int ch;
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
newptr=new queue;
if(!newptr)
{
cout<<"\n\t\aMemory Allocation Error!";
getch();
exit(0);
}
newptr->getd();
if(front==NULL)
{
front=newptr;
rear=newptr;
}
else
{
rear->next=newptr;
rear=rear->next;

24
rear->next=front;
}
break;
case 2:
if(front==NULL)
cout<<"Queue underflow (does not exist)";
else
{
ptr=front;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
rear->next=front;
}
cout<<"\nDeleted Element:";
ptr->showd();
delete ptr;
}
break;
case 3:
if(front==NULL)
cout<<"Queue does not exist";
else
{
ptr=front;
while(ptr->next!=front)
{
ptr->showd();
ptr=ptr->next;
}
ptr->showd();
}
break;
case 4:
getch();
exit(0);
default:
cout<<"\nWrong Choice Entered,Please Re-enter";
}
goto menu;
}

25
OUTPUT:

26
Program - 16: Write a program to implement Queue as a circular array.
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int max=5;
void main()
{
clrscr();
int q[max],front=-1,rear=-1,ch,num;
menu:
cout<<"\n\nMenu\n1.Push\n2.Pop\n3.Traverse\n4.Exit\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
if(front==(rear+1)%max)
cout<<"Queue Overflow";
else if(front==-1&&rear==-1)
{
front=0;
rear=0;
cout<<"Enter the number:";
cin>>num;
q[front]=num;
}
else
{
rear=(rear+1)%max;
cout<<"\nEnter the number:";
cin>>num;
q[rear]=num;
}
break;
case 2:
if(front==-1&&rear==-1)
cout<<"\nQueue does not exist";
else if(front==rear)
{
num=q[front];
cout<<"\nThe deleted number:"<<num;
front=-1;
rear=-1;
}
else
{
num=q[front];
cout<<"\nThe deleted number is:"<<num;
front=(front+1)%max;
}
break;
case 3:
if(front==-1&&rear==-1)
cout<<"\nQueue does not exist";
else
{

27
for(int i=front;i!=rear;i=(i+1)%max)
cout<<q[i]<<" ";
cout<<q[rear]<<endl;
}
break;
case 4:
getch();
exit(0);
default:
cout<<"Wrong Choice,Please Re-Enter:";
}
goto menu;
}

OUTPUT:

28
Program - 17: Write a program to illustrate the concept of structure.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

struct bl{int no;


char name[20];
float unit,bill;
}a;

void main()
{
clrscr();
cout<<"Enter consumer number:";
cin>>a.no;
cout<<"Enter consumer name:";
gets(a.name);
cout<<"Enter no. of units consumed:";
cin>>a.unit;
if(a.unit<=100)
a.bill=a.unit*0.4;
if(a.unit>100 && a.unit<=200)
a.bill=(a.unit-100)*0.5+40;
if(a.unit>200 && a.unit<=300)
a.bill=(a.unit-200)*0.75+90;
if(a.unit>300)
a.bill=(a.unit-300)*1+165;
cout<<"\nBill";
cout<<"\nConsumer No:"<<a.no;
cout<<"\nName:"<<a.name;
cout<<"\nUnits consumed:"<<a.unit;
cout<<"\nNet amount=Rs"<<a.bill;
getch();
}

OUTPUT:

29
Program - 18: Write a program to add any two matrices using multi-dimensional array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
getch();
}
OUTPUT:

30
Program - 19: Write a program to find transpose of a matrix using multi-dimensional array.
#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c;
// Storing element of matrix entered by user in array a[][].
cout << endl << "Enter elements of matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter elements a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
// Displaying the matrix a[][]
cout << endl << "Entered Matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl;
}
// Finding transpose of matrix a[][] and storing it in array trans[][].
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
trans[j][i]=a[i][j];
}
// Displaying the transpose,i.e, Displaying array trans[][].
cout << endl << "Transpose of Matrix: " << endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
getch();
}
OUTPUT:

31
Program - 20: Write a program to multiply any two matrices using multi-dimensional array.
#include <iostream.h>
#include <conio.h>

void main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
// If column of first matrix in not equal to row of second matrix,
// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
// Storing elements of first matrix.
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix.
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Initializing elements of matrix mult to 0.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}
// Multiplying matrix a and b and storing in array mult.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}
// Displaying the multiplication of two matrix.
cout << endl << "Output Matrix: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{

32
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}
getch();
}

OUTPUT:

33
Program - 21: Write a program to show significance of tellp, tellg, seekp and seekg.
#include <iostream.h>
#include<conio.h>
#include <fstream.h>

int main()
{
clrscr();
fstream st; // Creating object of fstream class
st.open("Data.txt",ios::out); // Creating new file

if(!st) // Checking whether file exist


{
cout<<"File creation failed";
}
else
{
cout<<"New file created"<<endl;
st<<"Hello Friends"; //Writing to file

// Checking the file pointer position


cout<<"File Pointer Position is "<<st.tellp()<<endl;

st.seekp(-1, ios::cur); // Go one position back from current position

//Checking the file pointer position


cout<<"As per tellp File Pointer Position is "<<st.tellp()<<endl;

st.close(); // closing file


}

st.open("Data.txt",ios::in); // Opening file in read mode

if(!st) //Checking whether file exist


{
cout<<"No such file";
}
else
{
char ch;
st.seekg(5, ios::beg); // Go to position 5 from begning.
cout<<"As per tellg File Pointer Position is "<<st.tellg()<<endl;
//Checking file pointer position
cout<<endl;

st.seekg(1, ios::cur); //Go to position 1 from beginning.


cout<<"As per tellg File Pointer Position is "<<st.tellg()<<endl;
//Checking file pointer position
st.close(); //Closing file
}
getch();
return 0;
}

34
OUTPUT:

35
Program - 22: Write a program to demonstrate the searching operation in binary file.
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>

class student
{
int rollno;
char name[20];
char branch[3];
float marks;
char grade;

public:
void getdata()
{
cout<<"Rollno: ";
cin>>rollno;
cout<<"Name: ";
cin>>name;
cout<<"Subject: ";
cin>>branch;
cout<<"Marks: ";
cin>>marks;

if(marks>=75)
{
grade = 'A';
}
else if(marks>=60)
{
grade = 'B';
}
else if(marks>=50)
{
grade = 'C';
}
else if(marks>=40)
{
grade = 'D';
}
else
{
grade = 'F';
}
}

void putdata()
{
cout<<"Rollno: "<<rollno<<"\tName: "<<name<<"\n";
cout<<"Marks: "<<marks<<"\tGrade: "<<grade<<"\n";
}

int getrno()
{

36
return rollno;
}
}stud1;

void main()
{
clrscr();

fstream fio("marks.dat", ios::in | ios::out);


char ans='y';
while(ans=='y' || ans=='Y')
{
stud1.getdata();
fio.write((char *)&stud1, sizeof(stud1));
cout<<"Record added to the file\n";
cout<<"\nWant to enter more ? (y/n)..";
cin>>ans;
}

clrscr();
int rno;
long pos;
char found='f';

cout<<"Enter rollno of student to be search for: ";


cin>>rno;

fio.seekg(0);
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char *)&stud1, sizeof(stud1));
if(stud1.getrno() == rno)
{
stud1.putdata();
fio.seekg(pos);
found='t';
break;
}
}
if(found=='f')
{
cout<<"\nRecord not found in the file..!!\n";
cout<<"Press any key to exit...\n";
getch();
exit(2);
}

fio.close();
getch();
}

37
OUTPUT:

38
Program - 23: Write a menu driven program for selection sort, bubble sort, insertion sort.
#include<iostream.h>
void bubblesort(int arr[],int m)
{ int temp,ctr=0;
for(int i=0;i<m;++i)
{ for(int j=0;j<((m-1)-i);++j)
{
if (arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void selectionsort(int arr[],int m)
{
int pos=0,small,temp;
for(int i=0;i<m;++i)
{
small=arr[i];
pos=i;
for(int j=i+1;j<m;++j)
{
if(small>arr[j])
{
small=arr[j];
pos=j;
} }
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp; } }
void insertionsort(int arr[],int m)
{
int temp,j;
for(int i=1;i<m;++i)
{
temp=arr[i];
j=i-1;
while(temp<arr[j] && j>=0)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
int main()
{
clrscr();
int arr[30],m,x;
char ch='y';
do
{

39
cout<<"Enter Size of the array::";
cin>>m;
cout<<"Enter elements of Array::";
for(int i=0;i<m;++i)
{
cin>>arr[i];
}
cout<<"Select the sorting Method:";
cout<<"\n1)Bubble Sort. \n2)Selection Sort. \n3)Insertion Sort.";
cout<<"\n Enter ur choice::";
cin>>x;
switch(x)
{
case 1:
{ cout<<"Sorting using Bubble sort";
bubblesort(arr,m);
break;
}
case 2:
{ cout<<"Sorting Using Selection Sort";
selectionsort(arr,m);
break;
}
case 3:
{ cout<<"Sorting Using insertion Sort";
insertionsort(arr,m);
break;
}
default:
cout<<"Wrong Selection";
}
cout<<"\nSorted array is as follows::\n";
for(x=0;x<m;++x)
cout<<"\t"<<arr[x];
cout<<"\n Do u want to continue::";
cin>>ch;
}while(ch=='y' || ch=='Y');
}

40
OUTPUT:

41
Program - 24: Write an SQL command for the queries for the questions relation SHOP shown below:

No. Shop_name Sale Area Cust% Rating City


1 S.M.Sons 250000 West 68.6 C Delhi
2 Dharohar 500000 South 81.8 A Mumbai
3 Kriti Art 300000 North 79.8 B Kolkata
4 A1 Store 380000 North 88.0 B Mumbai
5 Crystal 456000 East 92.0 A Delhi
6 Best Shop 290000 South 66.7 A Kolkata

(i) Show the names of all shops which are in the South area and cust-percent < 75.
(ii) To display number of shops in each city.
(iii) To display list of all the shops with sale > 300000 in ascending order of Shop_Name.
(iv) To display Shop_name,Area and Rating for only thos shops whose sale is between 350000 and
400000 (including both 350000 and 400000).
(v) To count the no. shops whose rating is A.

OUTPUT:

(i) SELECT Shop_name


FROM SHOP
WHERE Area='South' AND Cust%<75;
(ii) SELECT City,Count(*)
FROM SHOP
GROUP BY City;

(iii) SELECT Shop_name


FROM SHOP
WHERE Sale>300000
ORDER BY Shop_name;

(iv) SELECT Shop_name,Area,Rating


FROM SHOP
WHERE Sale BETWEEN(350000 AND 400001);

(v) SELECT Count(*)


FROM SHOP
WHERE Rating='A';

42
Program - 25: Write an SQL command for the queries for the questions relation EMPDETS and LEAVE
shown below:
TABLE: EMPDETS

Emp_no. Emp_name Department Salary Experience


E01 Manas Accts 15000 3
E02 Shiv System 35000 5
E03 Saksham System 20000 5
E04 Swastik Accts 15000 7
E05 Abijeet Accts 12000 3
E06 Nilank Purchase 18000 10
TABLE: LEAVE
Emp_no. Leave
E01 3
E04 5
E05 5
E06 1
(i) To display all employee names in descending order of salary.
(ii) Display the no. of employee whose salary > 15000 and experience > 5 years.
(iii) Display the average salary of each department.
(iv) To display Emp_no, Emp_name, leave for all employees who have taken leave.
(v) Increase the salary of all employees of Accts Department by 1000.
(vi) Delete the employee details whose experience is less than 3 yr.
(vii) Insert a new row in leave with details E02,3.

OUTPUT:
(i) SELECT Emp_name
FROM EMPDETS
ORDER BY Salary desc;
(ii) SELECT count(*)
FROM EMPDETS
WHERE Salary>15000 AND Experience>5;

(iii) SELECT Avg(Salary)


FROM EMPDETS
GROUP BY Department;

(iv) SELECT LEAVE.Emp_no, Emp_name, Leave


FROM EMPDETS,LEAVE
WHERE EMPDETS.Emp_no=LEAVE.Emp_no

(v) UPDATE EMPDETS


SET Salary=Salary+1000
WHERE Department=’Accts’;

(vi) DELETE FROM EMPDETS


WHERE Experience<3;

(vii) INSERT INTO LEAVE


VALUES(‘E02’,3);
43
Program - 26: Write an SQL command for the queries given from based a relation CLUB shown below:

Mcode Mname Gender Age Fees Type


1 Yuvraj Male 35 7000 Monthly
2 Bhavya Female 25 8000 Monthly
3 Mansha Female 42 24000 Yearly
4 Anushka Female 27 12000 Quarterly
5 Anumit Male 54 6000 Monthly
6 Raghav Male 43 4500 Monthly
7 Kanishtha Female 22 500 Guest
8 Harshit Male 51 24000 Yearly
9 Shiv Male 44 100000 Life
10 Nilank Male 33 12000 Quarterly

(i) To display all employee names in descending order of salary.


(ii) To display Mcode, Mname, Age of all female members of the CLUB with Age in descending
order.
(iii) To count the number of members of the CLUB of each type.
(iv) To display Mname, Fees of all those members of the CLUB whose Age < 40 and are Monthly
type members of the CLUB.
(v) To find out the max and min fees of each type.

OUTPUT:

(i) SELECT Mname,Age,Fees


FROM CLUB
WHERE Fees BETWEEN 6000 AND 10000;

(ii) SELECT Mcode,Mname,Age


FROM CLUB
WHERE Sex='Female'
ORDER BY Age DESC;

(iii) SELECT Type,Count(*)


FROM CLUB
GROUP BY Type;

(iv) SELECT Mname,Fees


FROM CLUB
WHERE Age<40 AND Type='Monthly';

(v) SELECT Type,Max(Fees),Min(Fees)


FROM CLUB
GROUP BY Type;

44
Program - 27: Write an SQL command for the queries for the questions given based on a relation TRAIN
shown below:

Train No. Name Class Seat No. Age Fare


1 Swastik I 11 16 700
2 Saksham AC-CHAIR 34 43 1300
3 Manas AC-I 78 18 9000
4 Shiv AC-II 23 17 600
5 Yuvraj I 3 25 1700
6 Nilank II 6 20 500
7 Abijeet AC-II 2 11 1200

(i) To display all employee names in descending order of salary.


(ii) To display Train No. and Seat No. where the Fare per Ticket is between 900 and 1300.
(iii) To display list of passengers in ascending order of Train No.
(iv) To display the highest amount paid as Fare per Ticket.
(v) To display all the passengers in each class.
OUTPUT:

(i) SELECT Mname,Age,Fees


FROM CLUB
WHERE Fees BETWEEN 6000 AND 10000;

(ii) SELECT Train_No,Seat_No.


FROM TRAIN
WHERE FareBETWEEN 900 AND 1300;

(iii) SELECT *
FROM TRAIN
ORDER BY Train_No.;

(iv) SELECT Max(Fare)


FROM TRAIN;

(v) SELECT *
FROM TRAIN
GROUP BY Class

45
Program - 28: Write an SQL command for the queries for the questions relation ITEM and CUSTOMER
shown below:

TABLE: ITEM

I_ID ItemName Manufacturer Price


PC01 Personal Computer ABC 35000
LC05 Laptop ABC 55000
PC03 Personal Computer XYZ 32000
PC06 Personal Computer COMP 37000
LC03 Laptop PQR 57000

TABLE: CUSTOMER

C_ID CustomerName City I_ID


01 N Roy Delhi LC03
06 H Singh Mumbai PC03
12 R Pandey Delhi PC06
15 C Sharma Delhi LC03
16 K Agarwal Banglore PC01

(i) To display the details of those Customer whose City is Delhi.


(ii) To display the details of Item whose Price is in the range of 3500 to 55000 (Both values
included).
(iii) To display the customerName, City from table Customer, and ItemName and Price from table
Item, with their corresponding matching I_ID.
(iv) To increase the Price of all Items by 1000 in the table Item.
OUTPUT:

(i) SELECT *
FROM CUSTOMER
WHERE CITY = 'DELHI';

(ii) SELECT *
FROM ITEM
WHERE PRICE BETWEEN 35000 AND 55000;

(iii) SELECT CUSTOMERNAME,CITY,ITEMNAME,PRICE


FROM CUSTOMER A INNER JOIN ITEM B
WHERE A.I_ID=B.I_ID;

(iv) UPDATE ITEM


SET PRICE=PRICE+1000;

46
Program - 29: Write an SQL command for the queries for the questions relation SENDER and RECIPIENT
shown below:
TABLE: SENDER

SenderID SenderName SenderAddress SenderCity


ND01 R Jain 2, ABC Appts New Delhi
MU02 H Sinha 12, Newtown Mumbai
MU15 S Jha 27/A, Park Street Mumbai
ND50 T Prasad 122-K, SDA New Delhi

TABLE: RECIPIENT

RecID SenderID RecName RecAddress RecCity


KO05 ND01 R Bajpayee 5, Central Avenue Kolkata
ND08 MU02 S Mohan 116, Anand Vihar New Delhi
MU19 ND01 H Singh 26, Andheri East Mumbai
MU32 MU15 P K Swamy B5, C S Terminus Mumbai
ND48 ND50 S Tirupathi 13, B1-D, Mayur Vihar New Delhi

(i) To display the names of all Senders from Mumbai.


(ii) To display the RecID, SenderName, SenderAddress, RecName, RecAddess for every Recipient.
(iii) To display Recipient detail in ascending order of RecName.
(iv) To display number of Recipients from each city
OUTPUT:

(i) SELECT SENDERNAME


FROM SENDER
WHERE SENDERCITY='MUMBAI';

(ii) SELECT RECID, SENDERNAME,SENDERADDRESS,RECNAME,RECADDRESS


FROM RECIPIENT A INNER JOIN SENDER B
ON A.SENDERID=B.SENDERID;

(iii) SELECT *
FROM RECIPIENT
ORDER BY RECNAME;

(iv) SELECT RECCITY,COUNT(RECNAME)


FROM RECIPIENT
GROUP BY RECCITY;

47

Das könnte Ihnen auch gefallen