You are on page 1of 63

# C++ RECORD II YEAR

2008-2009
SUJITH KUMAR

## KELTRON IT EDUCATION CENTRE

SPENCER
TRIVANDRUM
UNIVERSITY OF KERALA
Program 1:MATRIX MULTIPLICATION

## Aim: Write a C++ program to multiply two matrices of order m x n.

Program:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

void main()

clrscr();

cin>>m>>n;

cin>>p>>q;

if(n!=p)

getch();

exit(0);

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

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

cin>>A[i][j];

## cout<<"Enter Second Matrix"<<endl;

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

2
for(j=0;j<q;j++)

cin>>B[i][j];

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

for(j=0;j<q;j++)

sum=0;

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

sum=sum+A[i][k]*B[k][j];

C[i][j]=sum;

cout<<"Resultant Matrix"<<endl;

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

for(j=0;j<q;j++)

cout<<" "<<C[i][j];

cout<<endl;

getch();

Out put 1:

## Enter the order of first matrix

3 4

3
Enter the order of second matrix

4 3

## Enter First Matrix

12 13 14 10 25 21 14 13 12 47 10 12

## Enter Second Matrix

24 20 23 21 22 20 35 36 14 12 14 14

Resultant Matrix

Out put 2:

3 2

4 3

## Program 2:COUNT – CHARACTERS,WORDS

Aim: Write a C++ program to to count characters, words and number of lines in a text.

Program:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

#include <string.h>

class textc

protected:

char text[100];

int nw;

int nc;

int nl;

public:

4
textc()

strcpy(text,"");

nw=0;

nc=0;

nl=0;

void gettext()

## cout<<"Enter Text : ";

cin.getline(text,100);

void count()

int i,x;

x=strlen(text);

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

if(text[i]==' ')

nw++;

if(text[i+1]==' '||text[i+1]=='.'||text[i+1]==NULL)

nw--;

if(text[i]=='.'||text[i+1]==NULL)

nl++;

if(text[i+1]!=' ')

nw++;

5
nc=i;

void display()

};

void main()

textc T;

clrscr();

T.gettext();

T.count();

T.display();

getch();

Out put:

Characters -> 47

words -> 9

Lines ->2

## Aim: Write a C++ program to calculate the electricity charge

Program:

#include <iostream.h>

#include <fstream.h>

#include <string.h>

6
#include <stdlib.h>

#include <conio.h>

class electricity

char Cnam[20];

int Cno;

int Pre_R;

int Cur_R;

float Rate;

public:

void getdata()

cout<<"Con.No : ";

cin>>Cno;

cin>>Cnam;

cin>>Pre_R;

cin>>Cur_R;

void display()

gotoxy(15,15);

gotoxy(45,15);

gotoxy(15,17);

gotoxy(15,19);

7

gotoxy(15,21);

## cout<<"Consumed Unit: "<<Cunit;

gotoxy(15,23);

cout<<"Amount : "<<Rate;

void calcu()

## int Cunit=Cur_R - Pre_R;

if(Cunit<101)

Rate=Cunit*.8;

else if(Cunit<201)

Rate=80+(Cunit-100)*1;

else if(Cunit<301)

Rate=180+(Cunit-200)*2;

else if(Cunit<501)

Rate=380+(Cunit-300)*5;

else

Rate=1380+(Cunit-500)*9;

};

void main()

electricity E;

clrscr();

## cout<<"Enter Billig Details"<<endl;

E.getdata();

E.calcu();

clrscr();

gotoxy(25,10);

8
cout<<"Electricity Billig";

gotoxy(24,12);

cout<<"********************";

E.display();

getch();

Out put:

Con.No :S2456

## Con Name : Sreekantan

Electricity Billig

********************

## Consumer No: S2456 Consumer Name: Sreekantan

Consumed Unit:102

Amount :82

## Program 4:ITEM DETAILS USING CLASS

Aim: Write a C++ program to read a list containing item name, code, and cost and produce a three column output.
Program:

#include <iostream.h>

#include <iomanip.h>

#include <conio.h>

class List

char It_nam[20];

9
int It_code;

float It_price;

public:

void getitem()

cin>>It_nam;

## cout<<"EnterItem Code : ";

cin>>It_code;

cout<<"EnterPrice/unit : ";

cin>>It_price;

void display()

cout<<endl<<setw(10)<<It_code<<setw(10)<<It_nam<<setw(10)<<It_price<<endl;

};

void main()

List L[20];

char ch='y';

int i=0,j=0;

clrscr();

while(ch=='y')

L[i].getitem();

++i;

cin>>ch;

10
clrscr();

gotoxy(15,5);

## cout<<"Item Details ";

gotoxy(14,6);

cout<<"**************" <<endl;

cout<<setw(10)<<"Code"<<setw(10)<<"Item"<<setw(10)<<"Price"<<endl;

while(j<i)

L[j].display();

++j;

getch();

Out put:

## EnterItem Code :0123

EnterPrice/unit : 18.50

Enter Another....?(y/n) :y

## EnterItem Code : 1242

EnterPrice/unit :25.80

Enter Another....?(y/n) :n

Item Details

**************

## Program 5:SHOPPING LIST USING CLASS

11
Aim: Write a C++ program to prepare a shopping list using class with arrays as data members.

Program:

#include <iostream.h>

#include <iomanip.h>

#include <stdlib.h>

#include <conio.h>

class shopping

char item[20][10];

int it_no[20],it_qty[20];

float it_rate[20],total[20],sum;

int count;

public:

shopping()

count=0;

sum=0;

void getdata()

## cout<<"Enter the item: ";

cin>>item[count];

## cout<<"Enter Item Price : ";

cin>>it_rate[count];

cout<<"Enter quantity:";

cin>>it_qty[count];

total[count]=it_rate[count]*it_qty[count];

it_no[count]=count+1;

count++;

void display()

12
{

int y=6;

cout<<"Slno:"<<"\t"<<"Item"<<setw(25)<<"Price"<<setw(5)<<"qty"<<setw(10)<<"Total"<<endl;

cout<<"-----------------------------------------------------"<<endl;

for(int i=0;i<count;i++)

cout<<it_no[i]<<"\t"<<item[i];

gotoxy(23,y);

cout<<setw(14)<<it_rate[i]<<setw(5)<<it_qty[i]<<setw(10)<<total[i]<<endl;

sum=sum+total[i];

y++;

void disp()

};

void main()

shopping S;

char ch='Y';

int x;

do

clrscr();

S.getdata();

S.display();

## cout<<"Do u want to continue?(Y/N)";

13
cin>>ch;

}while(ch=='y'||ch=='Y');

S.disp();

getch();

Out put:

Enter quantity:3

## Grand Total :37.5

Do u want to continue?(Y/N) y

Enter quantity:1

## Grand Total :162.5

Do u want to continue?(Y/N) n

## Program 6:CONSTRUCTORS IN A CLASS

Aim: Write a C++ program to find the interest of a FD using dynamic initialization of constructors.

Program:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

class FD

{
14
float principle;

float rate;

float days;

float intr;

public:

FD()

principle=0.0;

rate=0.0;

days=0;

intr=0.0;

## FD(float pr, float r, int d)

principle=pr;

rate=r;

days=d;

intr=principle*(days/365)*rate/100;

void display()

cout<<endl<<"Principle : "<<principle<<endl;

cout<<"Rate : "<<rate<<"%"<<endl;

cout<<"Interest : "<<setprecision(3)<<intr<<endl;

};

void main()

FD F1;

float pr,rt;

15
char ch='y';

int dt;

clrscr();

do

cout<<setw(25)<<"FD Details"<<endl;

cout<<"Principle : ";

cin>>pr;

## cout<<"Rate [%]: ";

cin>>rt;

cout<<"Period[days] : ";

cin>>dt;

F1=FD(pr,rt,dt);

F1.display();

## cout<<endl<<"Do You want to Continue?(Y/N)";

cin>>ch;

}while (ch=='Y'||ch=='y');

Out put:

FD Details

Principle : 25000

## Rate [%]: 4.5

Period[days] : 450

Principle : 25000

Rate : 4.5%

Interest :1386.986

## Do You want to Continue?(Y/N)y

FD Details

Principle :50000

Rate [%]: 3

16
Period[days] : 365

Principle : 50000

Rate : 3%

Interest :1500

## Program 7:RANK LIST USING TWO CLASSES

Aim: Write a C++ program to create a rank list of students using two different classes.
Program:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

#include <string.h>

class student

protected:

char nam[20];

int M1,M2,M3,tot;

public:

student()

strcpy(nam,"");

M1=0;

M2=0;

M3=0;

tot=0;

void getstud()

cout<<setw(25)<<"Mark Details"<<endl;

## cout<<"Student Name : ";

cin>>nam;

17
cout<<"Mark1 : ";

cin>>M1;

cout<<"Mark2 : ";

cin>>M2;

cout<<"Mark3 : ";

cin>>M3;

tot=M1+M2+M3;

};

int rnk;

public:

void display()

## static int rnk;

cout<<setw(10)<<++rnk;

cout<<setw(10)<<nam;

cout<<setw(10)<<tot<<endl;

};

## void great( rank & x,rank & y)

rank T;

if(x.tot<y.tot)

T=x;

x=y;

y=T;

18
}

void main()

rank R[10],T;

char check='y';

int x=0;

clrscr();

for(int i=0;check=='y';i++)

R[i].getstud();

## cout<<"Do u want to enter another [Y/N]: ";

cin>>check;

cout<<"Rank list"<<endl;

for(int j=0;j<i;j++)

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

great(R[k],R[k+1]);

clrscr();

cout<<setw(25)<<"Rank List"<<endl;

cout<<setw(10)<<"Rank"<<setw(10)<<"Name"<<setw(10)<<"Total"<<endl;

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

R[j].display();

19
getch();

Out put:

Mark Details

Mark1 :48

Mark2 : 49

Mark3 :47

Mark Details

Mark1 :46

Mark2 : 45

Mark3 :45

Mark Details

Mark1 :42

Mark2 : 43

Mark3 :46

1 ramesh 144

2 suresh 136

3 victor 131

## Program 8:DIFFERENT OPERATIONS ON STRING

Aim: Write a C++ program for different string manipulations - concatenation, copy, compare

20
Program:

#include <iostream.h>

#include <conio.h>

#include <string.h>

#include <iomanip.h>

void main()

char str1[20],str2[20];

clrscr();

## cout<<" Enter String 1: ";

cin>>str1;

strcpy(str2,str1);

## cout<<endl<<" String 1 is COPIED to string 2"<<endl;

cout<<endl<<"String 1 is :"<<str1<<endl;

## cout<<endl<<" Enter Another String : ";

cin>>str2;

int x=strcmp(str1,str2);

if (x<0 || x>0)

else

## cout<<endl<<" Both the Strings you entered are equal "<<endl;

strcat(str1,str2);

## cout<<endl<<" CONCATINATED String : "<<str1<<endl;

getch();

Out put:

21
Enter String 1: keltron

## String 1 is COPIED to string 2

String 1 is : keltron

String 2 is : keltron

## Program 9:SALARY LIST USING FRIEND FUNCTION

Aim: Write a C++ program to calculate the salary of different grade of employers in a company using friend
function.
Program:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>
#define DA 0.45
class employee
{
char enam[20];
float ebasic;
public:

void getdata()
{
cout<<setw(30)<<" Employee Details."<<endl;
cout<<"Enter emp Name : ";
cin>>enam;
cout<<"Enter basic : ";
cin>>ebasic;
}
friend void calcu(employee E);

};
void calcu(employee emp)
{
float da,pf,lic,hra,gsal,nsal;
da=DA * emp.ebasic;
lic=.05*emp.ebasic;
{
pf=.15*emp.ebasic;
hra=.1*emp.ebasic;

22
}
{
pf=.13*emp.ebasic;
hra=.08*emp.ebasic;
}
{
pf=.11*emp.ebasic;
hra=.07*emp.ebasic;
}

{
pf=.10*emp.ebasic;
hra=.06*emp.ebasic;
}
gsal=emp.ebasic+da+hra;
nsal=gsal-pf-lic;
cout<<"Employee Name:"<<setw(5)<<emp.enam<<endl;
cout<<"Basic "<<setw(5)<<emp.ebasic<<endl;
cout<<"DA "<<setw(5)<<da<<endl;
cout<<"HRA "<<setw(5)<<hra<<endl;
cout<<"PF "<<setw(5)<<pf<<endl;
cout<<"LIC "<<setw(5)<<lic<<endl;
cout<<"Gross Salary "<<setw(5)<<gsal<<endl;
cout<<"Net Salary "<<setw(5)<<nsal<<endl;
}
void main()
{
employee E;
char x;
do
{

clrscr();
E.getdata();
calcu(E);
cout<<"Do u calculate another(y/n) -> ";
cin>>x;

}while(x=='y');
}

Out put:

Employee Details.

## Enter basic :5000

Employee Name:gowri

Basic 5000

23
DA 2250

HRA 400

PF 650

LIC 250

## Program 10:MULTIPLE INHERITANCE

Aim: Write a C++ program to print a student report using multiple inheritances.
Program:
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

#include<iomanip.h>

class marks

protected:

int M1,M2,M3;

public:

marks()

M1=M2=M3=0;

void getmark();

};

class sports

protected:

int grace;

public:

24
sports()

grace=0;

void getgrace();

};

## class student:public marks, public sports

int Rollno;

char name[20];

public:

student()

Rollno=0;

strcpy(name,"");

void getstudent();

void display();

};

void marks::getmark()

cout<<"Enter Marks"<<endl;

cin>>M1>>M2>>M3;

void sports::getgrace()

## cout<<"Enter grace mark(sports) : ";

cin>>grace;

void student::getstudent()

25
{

cin>>Rollno;

## cout<<"Enter Name ";

cin>>name;

void student::display()

int s=M1+M2+M3;

int Tot=s+grace;

cout<<endl<<Rollno<<"\t\t"<<name<<"\t\t\t"<<Tot<<endl;

void main()

student s[10];

int j,i=0;

char ch='y';

while(ch=='y')

clrscr();

s[i].getstudent();

s[i].getmark();

s[i].getgrace();

i++;

## cout<<"Do u want to enter Another Record [y/n]";

cin>>ch;

cout<<endl<<"Students Records"<<endl;

cout<<endl<<"Roll No:\tName\t\t\tTotalmarks"<<endl;

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

26
{

s[j].display();

getch();

Out put:

Students Details

Enter Marks

45 46 43

Students Details

Enter Marks

42 43 41

Students Records

## Aim: Write a C++ program to solve the problem of towers of Hanoi.

Program:

#include <iostream.h>

#include <conio.h>

27
#include <string.h>

void main()

clrscr();

int n;

void transfer(int,char,char,char);

## cout<<"Enter no. of Discs: ";

cin>>n;

transfer(n,'A','C','B');

getch();

## void transfer(int n, char from,char to,char tmp)

if(n>0)

transfer(n-1,from,tmp,to);

cout<<"Move disk"<<n<<"From"<<from<<"to"<<to<<endl;

transfer(n-1,tmp,to,from);

Out put:

## Move 4 disk FromAtoC

28
Move 1 disk FromBtoC

## Move 3disk FromBtoC

Move1disk FromAtoB

## Move 1disk FromBtoC

Aim: Write a C++ program to perform arithmetic operations on complex numbers using operator overloading.
Program:
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class complex

float real;

float img;

public:

complex()

real=img=0;

void getdata()

## cout<<"Real Part : ";

cin>>real;

29
cout<<"Imaginary Part : ";

cin>>img;

void display()

if(img>=0)

cout<<real<<"+"<<"i"<<img<<endl;

else

cout<<real<<"-"<<"i"<<(-1*img)<<endl;

};

complex complex::operator+(complex c)

complex tmp;

tmp.real=real+c.real;

tmp.img=img+c.img;

return(tmp);

complex complex::operator-(complex c)

complex tmp;

tmp.real=real-c.real;

tmp.img=img-c.img;

return(tmp);

complex complex::operator*(complex c)

complex tmp;

tmp.real=(real*c.real)-(img*c.img);

tmp.img=(img*c.real)+(real*c.img);

return(tmp);

30
}

complex complex::operator/(complex c)

complex tmp;

float ft;

ft=c.real*c.real+c.img*c.img;

tmp.real=(real*c.real+img*c.img)/ft;

tmp.img=(img*c.real-real*c.img)/ft;

return(tmp);

void main()

complex c1,c2,c3;

clrscr();

c1.getdata();

c2.getdata();

## cout<<endl<<" Results "<<endl;

c3=c1+c2;

c3.display();

cout<<"Subtraction : ";

c3=c1-c2;

c3.display();

cout<<"Multiplication : ";

c3=c1*c2;

c3.display();

cout<<"Division : ";

c3=c1/c2;

31
c3.display();

getch();

Out put:

## Enter Complex no.1:

Real Part : 16

Imaginary Part : 8

## Enter Complex no.2:

Real Part : 12

Imaginary Part : 5

Results

Subtraction : 4 + i3

## Division : 1.3727 + i0.0946

Aim: Write a C++ program to concatenate two strings using operator overloading.
Program:
#include <iostream.h>

#include <conio.h>

#include <string.h>

class string

char str[20];

public:

string()

strcpy(str,"");

string(char *ch)

32
strcpy(str,ch);

void display()

cout<<str<<endl;

};

## string string::operator+(string ch)

string tmp;

strcpy(tmp.str,str);

strcat(tmp.str,ch.str);

return(tmp);

void main()

clrscr();

string ch3;

string ch1="GOOD";

ch3=ch1+ch2;

ch3.display();

getch();

Out put:

## Concatenated string is : GOOD MORNING

33
Program 14:VIRTUAL FUNCTION

Aim: Write a C++ program to display the details of books and CDs in a shop using virtual functions.
Program:
#include <iostream.h>

#include <conio.h>

#include <string.h>

#include <iomanip.h>

class media

protected:

char title[20];

public:

media()

strcpy(title,"");

void getmedia()

cout<<"Title : ";

cin>>title;

{}

};

## class book:public media

char author[20];

float price;

public:

34
book()

strcpy(author,"");

price=0;

void getbook()

getmedia();

cout<<"Author : ";

cin>>author;

cout<<"Price : ";

cin>>price;

void display()

cout<<"Title : "<<title<<endl;

cout<<"Author : "<<author<<endl;

cout<<"Price : "<<price<<endl<<endl;

};

## class CD:public media

char director[20];

float price;

public:

CD()

strcpy(director,"");

price=0;

35
}

void getcd()

getmedia();

cout<<"Director : ";

cin>>director;

cout<<"Price : ";

cin>>price;

void display()

cout<<"Name : "<<title<<endl;

cout<<"Director : "<<director<<endl;

cout<<"Price : "<<price<<endl<<endl;

};

void main()

book B;

CD C;

clrscr();

## cout<<"Enter Book Details"<<endl;

B.getbook();

cout<<"Enter CD Details"<<endl;

C.getcd();

## media *M2=new media;

M1 = &B;

M2 = &C;

cout<<setw(20)<<"MEDIA DETAILS"<<endl;

cout<<setw(15)<<"Book"<<endl;

36
M1->display();

cout<<setw(15)<<"CD Details"<<endl;

M2->display();

getch();

Out put:

## Enter Book Details

Title :othello

Author : shakespere

Price :250

Enter CD Details

Title :IIhariharnagar

Director :lal

Price :100

MEDIA DETAILS

Book

Title :othello

Author : shakespere

Price :250

CD

Name : IIhariharnagar

Author : lal

Price:100

## Program 15:IMPLEMENTATION OF STACK

Aim: Write a C++ program to perform push and pop operations on a stack.

Program:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 5

37
int stack[20];

class stck

int top;

public:

stck()

top=-1;

char push(int);

int pop();

int empty();

int overflow();

};

char ch;

if(overflow())

return 0;

stack[++top]=st;

## cout<<"Do u want to push another(y/n)-";

cin>>ch;

return(ch);

int stck::pop()

if(empty())

38
{

cout<<"Stack Empty"<<endl;

return 0;

## cout<<"Poped item : ";

cout<<stack[top--];

return 0;

int stck::overflow()

if(top==MAX-1)

return 1;

return 0;

int stck::empty()

if(top==-1)

return 1;

return 0;

void main()

stck s;

int n,x;

char ch='y';

clrscr();

while(1)

cout<<"1. PUSH"<<endl;

cout<<"2. POP"<<endl;

39
cout<<"3. Exit"<<endl;

cin>>x;

switch(x)

case 1:

do

## cout<<"Enter the number to push: ";

cin>>n;

ch=s.push(n);

}while(ch=='y');

break;

case 2:

s.pop();

break;

case 3:

exit(0);

break;

default:

cout<<"Invalied";

break;

Out put:

1. PUSH

2. POP

3. Exit

40
Enter ur choice : 1

## Do u want to push another(y/n)-n

1. PUSH

2. POP

3. Exit

Enter ur choice : 2

## Poped item :50

1. PUSH

2. POP

3. Exit

Enter ur choice : 2

## Poped item :24

1. PUSH

2. POP

3. Exit

Enter ur choice :2

Stack Empty

1. PUSH

2. POP

3. Exit

Enter ur choice : 3

Aim: Write a C++ program to create a linked list and add and delete from it.
Program:

41
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

struct node

int data;

node * next;

void main()

void create();

void insert();

void display();

void del();

clrscr();

while(1)

int choice;

cin>>choice;

switch(choice)

case 1:

create();

break;

case 2:

insert();

break;

case 3:

42
del();

break;

case 4:

exit(0);

break;

void display()

## cout<<endl<<" Data in List"<<endl;

while(p!=NULL)

cout<<p->data<<"-";

p=p->next;

cout<<endl;

void create()

node *p;

char ch;

do

temp=new node;

temp->next=NULL;

cin>>temp->data;

43

else

p->next=temp;

p=temp;

cin>>ch;

}while(ch=='y');

display();

void insert()

char ch='n';

do

cin>>choice;

temp=new node;

temp->next=NULL;

## cout<<"Enter Data to be insert : ";

cin>>temp->data;

switch(choice)

case 1:

break;

44
case 2:

while(p->next!=NULL)

p=p->next;

p->next=temp;

break;

case 3:

cout<<"Current list"<<endl;

display();

## cout<<"Before which Node : ";

cin>>data1;

while(p!=NULL)

if(p->data==data1)

q->next=temp;

temp->next=p;

break;

else

q=p;

p=p->next;

break;

case 4:

cout<<"Current list"<<endl;;

45
display();

## cout<<"After which Node : ";

cin>>data1;

while(p!=NULL)

if(p->data==data1)

temp->next=p->next;

p->next=temp;

break;

else

p=p->next;

break;

cout<<"Changed Data"<<endl;

display();

cin>>ch;

}while(ch=='y');

void del()

## int choice, data1;

char ch='n';

46
cout<<" Delete from Where : "<<endl;

do

## cout<<"1. First Node"<<endl<<"2. Last Node"<<endl<<"3. Particular Node"<<endl<<"Enter your choice :

";;

cin>>choice;

switch(choice)

case 1:

break;

case 2:

while(p->next!=NULL)

q=p;

p=p->next;

q->next =NULL;

delete p;

break;

case 3:

cout<<"Current list";

display();

## cout<<"Node to be deleted : ";

cin>>data1;

while(p!=NULL)

if(p->data==data1)

47
{

q->next=p->next;

delete p;

break;

else

q=p;

p=p->next;

break;

cout<<"Changed Data"<<endl;

display();

cin>>ch;

}while(ch=='y');

Out put:

1. Create

2. Insert

3. Delete

4. Exit

## Enter Data to be insert : 10

Do u insert another....?(y/n)y

## Enter Data to be insert : 12

48
Do u insert another....?(y/n)n

Data in List

10 – 12 –

1. Create

2. Insert

3. Delete

4. Exit

Where to insert

1. First Node

2. Last Node

3. Insert Before

4. Insert After

Current list

Data in List

10 – 12 –

## After which Node : 10

Changed Data

Data in List

10 – 9 -12 –

Do u insert another....?(y/n) n

1. Create

2. Insert

3. Delete

4. Exit

## Delete from Where :

49
1. First Node

2. Last Node

3. Particular Node

## Enter your choice : 1

Changed Data

Data in List

9 -12 –

Do u Delete another....?(y/n) n

1. Create

2. Insert

3. Delete

4. Exit

## Aim: Write a C++ program to sort an array using bubble sort.

Program:
#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

#include <string.h>

void main()

int n,i,*A;

clrscr();

cin>>n;

A=new int[n];

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

cin>>A[i];

50
bsort(n,A);

## cout<<"Elements in Sorted order is"<<endl;

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

cout<<setw(10)<<A[i]<<endl;

getch();

## void bsort(int n,int *A)

for(int j=0;j<n-1;j++)

for(int k=0;k<n-j-1;k++)

if(A[k]>A[k+1])

int tp=A[k];

A[k]=A[k+1];

A[k+1]=tp;

Out put:

## Enter no:of Element-> 10

Enter Elements : 12 20 8 56

4 23 11 10

6 7

51
8

10

11

12

20

23

56

## Program 18:SEARCH IN A FILE

Aim: Write a C++ program to search the record in the file using name or phrases.
Program:

#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <string.h>

class student

char nam[20];

int age;

public:

student()

strcpy(nam,"");

age=0;

//void getstud();

## int search(char *ch)

return(strcmp(ch,nam));

void display()

52
cout<<"Student Name : "<<nam<<endl;

cout<<"Age : "<<age<<endl;

void getstud()

## cout<<"Student Name : ";

cin>>nam;

cout<<"Age : ";

cin>>age;

};

void main()

student S;

char ch[20],check='y';

int x=0;

fstream f;

f.open("student",ios::in|ios::out);

clrscr();

cout<<"Enter Data"<<endl;

while(check=='y')

S.getstud();

f.write((char*)&S, sizeof(S));

cin>>check;

f.seekg(0);

## cout<<"Enter name to search"<<endl;

cin>>ch;

53
cout<<"Result"<<endl;

while(f.eof()==0)

x=S.search(ch);

if(x==0)

S.display();

break;

if(x!=0)

f.close();

getch();

Out put:

Enter Data

Age : 24

Enter Data

Age : 23

Enter Data

Age : 26

54
Result

## Student Name :prajitha

Age : 23

Aim: Write a C++ program to add, update, and delete from a file.
Program:
#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <string.h>

#include <stdlib.h>

#include <iomanip.h>

#include <stdio.h>

class student

public:

char nam[20],place[20];

int age;

public:

void display()

cout<<setw(13)<<nam<<setw(10)<<age<<setw(10)<<place<<endl;

void getstud()

## cout<<"Student Name : ";

cin>>nam;

cout<<"Age : ";

cin>>age;

cout<<"Place :";

cin>>place;

55
}

};

void main()

student S;

char ch[20],c;

int i,check,n,flg=0;

fstream f,f1;

int sz=sizeof(S);

clrscr();

while(1)

## cout<<"1. Add"<<endl<<"2. Update"<<endl<<"3. Delete"<<endl<<"4. Display"<<endl<<"5.

Exit"<<endl<<"Enter ur Choice -> ";

cin>>check;

switch(check)

case 1:

f.open("d:\student1.txt",ios::in|ios::out);

cout<<"Enter Data"<<endl;

f.seekp(0,ios::end);

S.getstud();

cin.get(c);

f.write((char*)&S, sizeof(S));

f.close();

break;

case 2:

int loc;

flg=0;

f1.open("e:\student1.txt",ios::in|ios::out|ios::ate);

56
f1.seekg(0,ios::beg);

f1.seekp(0,ios::beg);

## cout<<"Enter which one modified(enter name)"<<endl;

cin>>ch;

if(strcmp(S.nam,ch)==0)

## cout<<"Enter New Data"<<endl;

S.getstud();

cin.get(c);

n=f1.tellg();

loc=n/sz;

f1.seekp((loc-1)*sz);

f1.write((char*)&S, sizeof(S));

f1.close();

flg=1;

break;

if(flg==0)

cout<<"Can't Found"<<endl;

f1.close();

break;

case 3:

f1.open("d:\student1.txt",ios::in|ios::out);

f.open("d:\student.txt",ios::in|ios::out);

f1.seekg(0,ios::beg);

f.seekg(0,ios::beg);

57
flg=0;

## cout<<"Enter which one delete(enter name)"<<endl;

cin>>ch;

if(strcmp(S.nam,ch)!=0)

f.write((char*)&S, sizeof(S));

if(strcmp(S.nam,ch)==0)

flg=1;

cin.get(c);

if(flg==1)

cout<<"Removed Successfully"<<endl;

else

f.close();

f1.close();

remove("d:\student1.txt");

if(rename("d:\student.txt","d:\student1.txt")==0)

remove("d:\student.txt");

break;

case 4:

f.open("d:\student1.txt",ios::in);

i=0;

f.seekg(0);

cin.get(c);

cout<<setw(24)<<"Student Details"<<endl<<setw(15)<<"Student
Name"<<setw(10)<<"Age"<<setw(10)<<"Place"<<endl;

S.display();

58
i++;

if(i==0)

cout<<"No Data....!!!!!"<<endl;

f.close();

break;

case 5:

exit(0);

break;

f.close();

Out put:

2. Update

3. Delete

4. Display

5. Exit

Enter Data

## Student Name : ramesh

Age : 25

Place : pallichal

2. Update

59
3. Delete

4. Display

5. Exit

Enter Data

## Student Name : sreejith

Age : 26

Place : ooruttambalam

2. Update

3. Delete

4. Display

5. Exit

ramesh

## Student Name : ramesh

Age : 26

Place : trivandrum

2. Update

3. Delete

4. Display

5. Exit

Student Details

## Student Name Age Place

Ramesh 26 trivandrum

60
Sreejith 26 ooruttambalam

Place : trivandrum

2. Update

3. Delete

4. Display

5. Exit

## Enter which one delete(enter name)

suresh

Removed Successfully

2. Update

3. Delete

4. Display

5. Exit

## Program 20:COMMAND LINE ARGUMENTS

Aim: Write a C++ program to create copy command (command line arguments).
Program:
#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <string.h>

class student

char nam[20];

int age;

public:

student()

61
{

strcpy(nam,"");

age=0;

void getstud()

cin>>nam;

cout<<"Age : ";

cin>>age;

void display()

## cout<<"Student Name : "<<nam<<endl;

cout<<"Age : "<<age<<endl;

};

## void main(int argc, char* argv[])

student S;

char ch[20],check='y';

fstream f1,f2;

f1.open(argv[1],ios::in|ios::out);

clrscr();

cout<<"Enter Data"<<endl;

while(check=='y')

S.getstud();

f1.write((char*)&S, sizeof(S));

## cout<<"Do u want to enter another : ";

cin>>check;

62
}

f2.open(argv[2],ios::in|ios::out);

f1.seekg(0,ios::beg);

f2.seekp(0,ios::beg);

f2.write((char*)&S, sizeof(S));

## cout<<"Content of file 2"<<endl;

f2.seekg(0,ios::beg);

S.display();

f1.close();

f2.close();

getch();

Thank GOD.

Thank Parents.

Thank you.

63