You are on page 1of 39

JM INTERNATIONAL

SCHOOL

C++ PRACTICAL FILE

SUBMTTD. BY -
ravi
CLASS-
12 A
ROLL NO.-
INDEX
PROGRAM 1. TO REVERSE EACH WORD OF A STRING.
PROGRAM 2. TO ILLUSTRATE MULTIPLE INHERITANCE.
PROGRAM 3. TO CHECK IF MATRICES ARE EQUAL OR NOT.
PROGRAM 4. TO FIND THE SUM AND MEMORY ADDRESS OF
ARRAY SUM.
PROGRAM 5. TO FIND THE SUM AND MEMORY ADDRESS OF
ARRAY SUM.
PROGRAM 6. TO CHANGE THE STRING.

PROGRAM 7. TO ILLUSTRATE CLASSES AND OBJECTS.


PROGRAM 8. MULTIPLICATION OF MATRIX.
PROGRAM 9. CLASS TO STORE 10 ITEMS AND TO PRINT
LARGEST PRICE AS WELL AS THE SUM OF ALL
THE PRICES.
PROGRAM 10. TO CONVERT STRING TO UPPERCASE.

PROGRAM 11. TO STORE THE MARKS OF A STUDENT IN A


MARKS.DAT FILE.
PROGRAM 12. TO DELETE ELEMENTS FROM ARRAY.
PROGRAM 13. TO SHOW THE NUMBERS IN DESCENDING ORDER.
PROGRAM 14. TO CHECK THE GIVEN STRING ISPALINDROME OR
NOT.
PROGRAM 15. TELEPHONE DIARY.

PROGRAM 16. BUBBLE SELECTION SORT.


PROGRAM 17. CIRCULAR QUEUE.
PROGRAM 18. TO STORE STUDENT RECORD.

PROGRAM 19. TO GET THE SUM OF BOTH DIAGONALS.


PROGRAM 20. BINARY SEARCH.
PROGRAM 1.TO REVERSE EACH WORD OF A
STRING:-
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
clrscr();
intch,i,j,k=0,p=0,l;
charst[50],sr[50];
cout<<"Enter Your Choice : "<<endl<<"1.Each Word Reversing"<<endl<<"2.Whole
Sentence Reversing"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the String"<<endl;
gets(st);
l=strlen(st);
for(i=0;i<=l;i++)
{
if((st[i]==' ')||(st[i]=='\0'))
{
for(j=i-1;j>=p;j--)
{
sr[k]=st[j];
k++;
}
sr[k]=' ';
k++;
p=i+1;
}
}
for(i=0;i<p;i++)
cout<<sr[i];
break;

case 2:
cout<<"Enter the String"<<endl;
gets(st);
l=strlen(st);
p=l-1;
for(i=l-1;i>=-1;i--)
{
if((st[i]==' ')||(i == -1))
{
for(j=i+1;j<=p;j++)
{
sr[k]=st[j];
k++;
}
sr[k]=' ';
k++;
p=i-1;
}
}
for(i=0;i<=l;i++)
cout<<sr[i];
break;

default:
cout<<"Wrong Choice"<<endl;
break;

}
getch();
}

OUTPUT
PROGRAM 2. TO ILLUSTRATE MULTIPLE
INHERITANCE:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class admin
{
intadmno;
char name[30];
public:
voidgetadmin();
voidputadmin();
};
void admin::getadmin()
{
cout<<"\nEnter admission no: ";
cin>>admno;
cout<<"\nEnter name: ";
gets(name);
}
void admin::putadmin()
{
cout<<"\nThe admission no is; ";
cout<<admno;
cout<<"\nThe name is: ";
cout<<name;
}
classacadm
{
intrno;
float marks;
public:
voidgetacadm();
voidputacadm();
};
voidacadm::getacadm()
{
cout<<"\nEnterrollno: ";
cin>>rno;
cout<<"\nEnter marks: ";
cin>>marks;
}
voidacadm::putacadm()
{
cout<<"\nTherollno is: ";
cout<<rno;
cout<<"\nThe total marks: ";
cout<<marks;
}
classschool:publicadmin,publicacadm //Derived class
{
char house[15],sport[20];
public:
voidget_school();
voidput_school();
};
void school::get_school()
{
getadmin();
getacadm();
cout<<"\nEnter house: ";
gets(house);
cout<<"\nEnter sport: ";
gets(sport);

}
void school::put_school()
{
putadmin();
putacadm();
cout<<"\nYour house is: ";
cout<<house;
cout<<"\nYour sports is: ";
cout<<sport;
}
void main()
{
clrscr();
schoolobj[10];
intn,i,j;
cout<<"Enter the no of students:";
cin>>n;
cout<<"\nEnter student details:";
for(i=0;i<n;i++)
{
obj[i].get_school(); //Input Function
}
cout<<"\nThe student details are:";
for(i=0;i<n;i++)
{
obj[i].put_school(); //Output Function
}
getch();}

OUTPUT
PROGRAM 3. TO CHECK IF MATRICES ARE EQUAL
OR NOT:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],r,c;
cout<<"enter first matrix row wise\n";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{cin>>a[r][c];}
}
cout<<"enter second matrix row wise\n";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{cin>>b[r][c];}
}
//loop to check equality
int flag=0;
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{if(a[r][c]!=b[r][c])
{
flag=1;break;
}
}
if(flag==1)break;
}
if(flag==0)
cout<<"\nmatrices are equal\n";
else
cout<<"\nmatrices are unequal\n";
getch();
}

OUTPUT
PROGRAM 4. TO FIND THE SUM AND MEMORY
ADDRESS OF ARRAY SUM:-
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],i,j,sum=0;
int *p,*q,*r;
clrscr();
cout<<"\n Enter Five Numbers";
for(i=0;i<=4;i++)
{
cin>>a[i];
sum=sum+a[i];
}
p=&a[0];
q=&a[4];
r=&sum;
cout<<"------------------";
cout<<"\n Starting address is:"<<p<<endl;
cout<<"\n ending address is:"<<q<<endl;
cout<<"\n Size of the array is:"<<5*2<<endl;
cout<<"\n Sum is:"<<sum<<endl;
cout<<"\n Address of sum is:"<<r<<endl;
cout<<"----------------------------";
getch();
}

OUTPUT
PROGRAM 5. FUNCTION OVERLOADING TO
CALCULATE THE AREA OF A CIRCLE,
RECTANGLE AND TRIANGLE :-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void area(float r)
{
float area;
cout<<"\nArea:"<<(3.14*r*r);
}
void area(float l,float b)
{
float area ;
cout<<"\nArea:"<<l*b;
}
void area(float x,floaty,float z)
{
float s;
s=(x+y+z)/2;
cout<<"\nArea:"<<sqrt(s*s-x*s-y*s-z);
}
void main()
{
clrscr();
intopt,x,y,z,l,b,r;
charch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\nMENU\n\n1.Area of circle\n2.Area of Rectangle\n3.Area of Triangle";
cout<<"\nEnter your Option:";
cin>>opt;
switch(opt)
{
case 1:cout<<"\nEnter radius of the circle:";
cin>>r;
area(r);
break;
case 2:cout<<"\nEnter the length and breadth of the rectangle:";
cin>>l>>b;
area(l,b);
break;
case 3:cout<<"\nEnter side 1:";
cin>>x;
cout<<"\nEnter side 2:";
cin>>y;
cout<<"\nEnter side 3:";
cin>>z;
area(x,y,z);
break;
default:cout<<"Wrong Option!!";
}
cout<<"\nDo you wnat to continue?(Y/N):";
cin>>ch;
}
getch();
}

OUTPUT
PROGRAM 6. TO CHANGE THE STRING:-
#include<iostream.h>
#include<string.h>
void main()
{clrscr();
char string[80];
cout<<"Enter string(max79 character)\n";
cin.getline(string,80);
int x1=strlen(string);
for(int i=0;string[i]!='0';i++)
if(string[i]==' ')
string[i]='*';
cout<<"The changed string is\n";
cout.write(string,x1);
getch();
}

OUTPUT
PROGRAM 7. TO ILLUSTRATE CLASSES AND
OBJECTS:-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
classemp //Employee class
{
intempcode;
charempname[20];
chardesig[20];
floatbsal,hra,da,netsal;
void calculate();
public:emp()
{ //Assigning all values as 0
empcode=0;
bsal=0.0;
hra=0.0;
da=0.0;
netsal=0.0;
strcpy(empname,"Not Initialised");
strcpy(desig,"Not Initialised");
}
void input(); //Calling input and output functions
void output();
}ob[10];
voidemp::calculate() //calculate function
{
netsal=bsal+hra+da;
}
voidemp::input() //accept values from user
{
cout<<"\nEnter the employee code:";
cin>>empcode;
cout<<"\nEnter the employee name:";
gets(empname);
cout<<"\nEnter designation:";
gets(desig);
cout<<"\nEnter the basic salary:";
cin>>bsal;
cout<<"\nEnter HRA:";
cin>>hra;
cout<<"\nEnter DA:";
cin>>da;
calculate();
}
voidemp::output() //Output values
{
cout<<empcode;
cout<<empname;
cout<<desig;
cout<<netsal;
}
void main()
{
clrscr();
inti,n;
cout<<"\nEnter the number of employees:";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].input();
}
cout<<"\n\nCode\tName\tSalary\n";
for(i=0;i<n;i++)
{
ob[i].output();
cout<<endl;
}
getch();
}

OUTPUT
PROGRAM 8. MULTIPLICATION OF MATRIX:-
#include<iostream.h>
#include<conio.h>

class matrix
{
int x[10][10];
intm,n;
public:
void input();
void output();
void multiply(matrix,matrix);};
void matrix::input()
{cout<<"Enter Row"<<endl;
cin>>m;
cout<<"Enter Column"<<endl;
cin>>n;
cout<<"Matrix"<<endl;
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{
cin>>x[i][j];}}}
void matrix :: output()
{
for(int i=0;i<m;i++)
{
cout<<endl;
for(int j=0;j<n;j++)
{
cout<<x[i][j]<<" ";}}}
void matrix :: multiply(matrix m1, matrix m2)
{
for(int i=0;i<m1.m;i++)
{
for(int j=0;j<m2.n;j++)
{
x[i][j]=0;
for(int k=0;k<m1.n;k++)
{
x[i][j]=x[i][j] +( m1.x[i][k] * m2.x[k][j]);
m=m1.m;
n=m2.n;}}}}
void main()
{
clrscr();
matrix m1,m2,m3;
m1.input();
m2.input();
m3.multiply(m1,m2);
m3.output();
getch();
}
OUTPUT
PROGRAM 9.CLASS TO STORE 10 ITEMS AND
TO PRINT LARGEST PRICE AS WELL AS THE
SUM OF ALL THE PRICES :-
#include<iostream.h>
#include<conio.h>

class item {
intitemcode[5];
floatit_price[5];

public :
void initialize(void);
float largest(void);
float sum(void);
voiddisplay_items(void);

};

void item :: initialize(void)


{
for(int i=0;i<5;i++)
{
cout<<"\nItem no : "<<(i+1);
cout<<"\nEnter item code : ";
cin>>itemcode[i];
cout<<"Enter item price : ";
cin>>it_price[i];
cout<<"\n";
}
}

float item :: largest(void)


{
float large=it_price[0];
for(int i=1;i<5;i++)
{
if(large<it_price[i])
large=it_price[i];
}
return large;
}

float item :: sum(void)


{
float sum=0;
for(int i=0;i<5;i++)
sum+=it_price[i];
return sum;
}

void item :: display_items(void)


{
cout<<"\nCode Price\m";
for(int i=0;i<5;i++)
{
cout<<"\n"<<itemcode[i];
cout<<" "<<it_price[i];
}
cout<<"\n";
}

int main()
{
item order;
order.initialize();
floattotal,biggest;
intch;
clrscr();
do
{
cout<<"\nMain menu";
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 price is "<<total<<"\n";
break;
case 3: order.display_items();
break;
default :cout<<"\nWrong Choice ";
break;}
}
while(ch>=1&&ch<=3);
getch();
return 0;

}OUTPUT
PROGRAM 10. TO CONVERT STRING TO
UPPERCASE:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
charstr[80];
int i;
cout<<"Enter any string(max 80) chars";
gets(str);
str[0]=toupper(str[0]);
for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
str[i+1]=toupper(str[i+1]);
cout<<"\nUpdated string is:"<<str;
getch();
}

OUTPUT
PROGRAM 11. TO STORE THE MARKS OF A
STUDENT IN A MARKS.DAT FILE:-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int main()
{ ofstreamfilout;
filout.open("marks.dat",ios::out);
charans='y';
introllno; float marks;
while(ans=='y'||ans=='y')
{ cout<<"\n enter roll no.:";
cin>>rollno;
cout<<"\n enter marks:";
cin>>marks;
filout<<rollno<<'n'<<marks<<'\n';
cout<<"\n want to enter more records?(y/n)....";
cin>>ans;
}
filout.close();
getch();
}

OUTPUT
PROGRAM 12. TO DELETE ELEMENTS FROM
ARRAY:-
#include<iostream.h>
#include<process.h>
#include<conio.h>
int linear(int [],int,int);
void main()
{
clrscr();
intn,i,j,x,del,ary[50];
charch;
cout<<"Enter the number of elements in the array ";
cin>>n;
cout<<"Enter the array : ";
for(i=0;i<n;i++)
{
cin>>ary[i];
}
cout<<"Press y to delete any element ";
cin>>ch;
while(ch=='y'||ch=='Y')
{
cout<<"Enter element to be deleted ";
cin>>del;
if(n==0)
{
cout<<"underflow !!\n";
exit(1);
}
x=linear(ary,n,del);
if(x !=-1)
ary[x]=0;

else
cout<<"Sorry!! No such element in the array\n";
cout<<"\nThe array now is as shown below...\n";
cout<<"Zero(0) signifies deleted elements \n";
for(i=0;i<n;i++)
cout<<ary[i]<<" ";
cout<<endl;
cout<<"After this emptied space will be shifted to the end of array
\n";
for(i=x;i<n;i++)
{
ary[i]=ary[i+1];
}
n-=1;
cout<<"\n Want to delete more elements ?(y/n)...";
cin>>ch;
}
cout<<"\nThe array after shifting All emptied spaces towards right is
...\n";
for(i=0;i<n;i++)
cout<<ary[i]<<" ";
cout<<endl;
getch();
}
int linear(intary[],int size, int del)
{
for(int i=0;i<size;i++)
{
if(ar[i]==del)
return i;
}
return -1;
}

OUTPUT
PROGRAM 13. TO SHOW THE NUMBERS IN
DESCENDING ORDER:-
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
float a, b, c, lar, lar1, lar2;
cout<<setw(59)<<"Program to show the numbers in descending order \n";
cout<<setw(40)<<"Enter first number : ";
cin>>a;
cout<<setw(40)<<"Enter second number : ";
cin>>b;
cout<<setw(40)<<"Enter third number : ";
cin>>c;
lar=a;
if(b>lar)
lar=b;
if(c>lar)
lar=c;
if(a==lar)
{ if(b>c)
{lar1=b;
lar2=c;
}
else
{
lar1=c;
lar2=b;
}
}
else if(b==lar)
{ if(a>c)
{lar1=a;
lar2=c;
}
else
{
lar1=c;
lar2=a;
}
}
else if(c==lar)
{
if(a>b)
{
lar1=a;
lar2=b;
}
else
{ lar1=b;
lar2=a;
}
}
cout<<"\n";
cout<<setw(40)<<"Descending order : "<<lar<<" > "<<lar1<<" > "<<lar2;
getch();
}

OUTPUT
PROGRAM 14. TO CHECK THE GIVEN STRING IS
PALINDROME OR NOT :-
#include<iostream.h>

#include<conio.h>

#include<stdio.h>

int main( )

clrscr( );

charstr[80];

cout<<"Enter a string:";

gets(str);

for(int L=0;str[L]!='\0';L++); //To find length of the string

for(int C=0;(C<L/2) && (str[C]==str[L-C-1]);C++);

if(C==L/2)

cout<<"Palindrome";

else

cout<<"Not a palindrome";

getch();

return 0;}

OUTPUT
PROGRAM 15. TELEPHONE DIARY:-
#include<iostream.h>
#include<conio.h>
#include<string.h>
constint size=10;
struct Person {
charfname[15];
charIname[25];
longtel;
} td[size],t;
void swap(Person&s1,Person&s2)
{
strcpy(t.fname,s1.fname);
strcpy(t.Iname,s1.Iname);
t.tel=s1.tel;
strcpy(s1.fname,s2.fname);
strcpy(s1.Iname,s2.Iname);
s1.tel=s2.tel;
strcpy(s2.fname,t.fname);
strcpy(s2.Iname,t.Iname);
s2.tel=t.tel;
}
void main()
{
inti,j;
for(i=0;i<size;i++)
{
cout<<"Person"<<i+1<<endl;
cin>>td[i].fname>>td[i].Iname>>td[i].tel;
}
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(strcmp(td[j].Iname,td[j+1].Iname)>0)
swap(td[j],td[j+1]);
else if(strcmp(td[j].Iname,td[j+1].Iname)==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].Iname<<" "<<td[i].fname<<","<<td[i].tel<<endl;
}
getch();
}
OUTPUT
PROGRAM 16. BUBBLE SELECTION SORT:-
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
intch;
inti,j,x,k,z,l,m,n,o,p,a[50],small;
q :
cout<<"Enter the choice 1:Selection 2:Bubble 3:Exchange Selection 4:Insertion
5:Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the limit "<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements"<<endl;

for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}

for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)
{
if(small>a[i])
{
small=a[i];
p=i;
}
}
for(k=p;k>j;k--)
{
a[k]=a[k-1];
}
a[j]=small;
}
cout<<"Result"<<endl;
for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}
goto q;
case 2:
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
for(j=0;j<n;j++)
{
for(i=0;i<n-1;i++)
{
if (a[i]>a[i+1])
{
x=a[i+1];
a[i+1]=a[i];
a[i]=x;
}
}
}
cout<<"Result"<<endl;
for (i=0;i<n;i++)
{
cout<<a[i];
cout<<endl;
}
break;
case 3 :
cout<<"Enter the limit "<<endl;
cin>>n;
cout<<endl;
cout<<"Enter the elements"<<endl;

for(i=0;i<n;i++)
{
cin>>a[i];
cout<<endl;
}

for(j=0;j<n;j++)
{
small=a[j];
p=j;
for(i=j;i<n;i++)
{
if(small>a[i])
{
small=a[i];
p=i;
}
}
a[p]=a[j];
a[j]=small;
}
cout<<"Result"<<endl;
for(z=0;z<n;z++)
{
cout<<a[z];
cout<<endl;
}
goto q;

case 4 :
int m=-32767;
cout<<"enter the no. of elements"<<endl;
cin>>n;
cout<<"enter the array"<<endl;
for(i=1;i<=n;i++)
{cin>>a[i];}
a[0]=m;
for(i=1;i<=n;i++)
{for(j=0;j<i;j++)
{if(a[i]<a[j])
{p=a[i];
for(k=i-1;k>=j;k--)
{a[k+1]=a[k];}
a[j]=p;}}}
for(i=1;i<=n;i++)
{cout<<a[i];}
goto q;

case 5:
exit(0);
default:
cout<<"Wrong choice";
goto q;

}
getch();
}

OUTPUT
PROGRAM 17. CIRCULAR QUEUE:-
#include<iostream.h>
#include<conio.h>
#include<process.h>

class queue
{int data[10];
intfront,rear;
public:
queue()
{front=-1;
rear=-1;
}
void add();
void remove();
void display();
};

void queue::add()
{if((rear+1==front)||(rear==9&&front==0))
{cout<<"Overflow ";
}
else
{if((rear==-1) &&(front==-1))
{rear=0;
front=0;
}
else if(rear==9)
{rear=0;
}
else
{rear++;
}
cout<<"Enter the element ";
cin>>data[rear];
}
}

void queue::remove()
{if(front==-1&&rear==-1)
{cout<<"Underflow ";
}
else
{if(front==9)
{front=0;
}
else if(front==rear)
{front=-1;
rear=-1;
}
else
{front++;
}
}
}

void queue::display()
{int i=0,n=9;
if(rear==-1)
{cout<<"No elements.."<<endl;
}
else
{ if(rear>front)
{for(i=0;i<front;i++)
{cout<<"_";
}
for(i=front;i<=rear;i++)
{cout<<data[i];
}
for(i=rear+1;i<n;i++)
{cout<<"_";
}
}
else
{for(i=0;i<=rear;i++)
{cout<<data[i];
}
for(i=rear+1;i<front;i++)
{cout<<"_";
}
for(i=front;i<n;i++)
{cout<<data[i];
}
} }
}

void main()
{clrscr();
intch;
queuequeue;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{case 1:queue.add();
goto X;
case 2:queue.remove();
goto X;
case 3:queue.display();
goto X;
case 4:exit(0);
}
getch();
}
OUTPUT
PROGRAM 18. TO STORESTUDENT RECORD:-
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<fstream.h>

struct stud
{
char name[50];
intrno;
};

void main()
{

clrscr();
studst;
int i;
ofstreamgpj("abcabc.dat",ios::binary|ios::trunc);
if(!gpj)
{cout<<"File Cannot Be Created"<<endl;
}
else
{
int n;

cout<<"How many Students U Want to Enter ???? "<<endl;


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Name"<<endl;
gets(st.name);
cout<<"Enter Roll No"<<endl;
cin>>st.rno;
gpj.write((char*)&st,sizeof(st));
}gpj.close();
}
int a=0;
ifstreamgp("abcabc.dat",ios::binary);
if(!gp)
{
cout<<"File Error"<<endl;
}
else
{
gp.seekg(0);
while(gp.read((char*)&st,sizeof(st)))
{
a++;
}
cout<<"No = "<<a;
gp.close();
}
getch();}
OUTPUT
PROGRAM 19. TO GET THE SUM OF BOTH
DIAGONALS:-
#include<iostream.h>
#include<conio.h>
#include<process.h>

int sum1(int a[50][50],int r)


{
int s=0;
for(int i=0;i<r;i++)
{ s=s+a[i][i];}
return(s);
}

int sum2(int a[50][50],int r)


{
int s=0;
int j;
j=r-1;
for(int i=0;i<r;i++)
s=s+a[i][j--];
return(s);
}

void row(int a[50][50],int r)


{
int s=0;
for(int i=0;i<r;i++)
{
s=0;
for(int j=0;j<r;j++)
{
s=s+a[i][j];

}
cout<<"Sum of Row "<<i+1<<" = "<<s<<endl;
}}

void col(int a[50][50],int r)


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

s=0;
for(int j=0;j<r;j++)
{
s=s+a[j][i];

}
cout<<"Sum of Column "<<i+1<<" = "<<s<<endl;
}}

void main()
{
clrscr();
inti,s,j,r,c,ch,a[50][50];
x:
cout<<"Entr Array Limit--(Enter only Row as R=C)"<<endl;
cin>>r;

cout<<"Enter Array"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
cin>>a[i][j];
}
}
y:
cout<<endl<<endl<<" Enter Choice :"<<endl<<"Sum of---- 1:main
2:Secondary 3.Rows 4.Columns 5.Re-enter 6.Exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
s=sum1(a,r);
cout<<"Sum = "<<s<<endl;
goto y;

case 2 :
s=sum2(a,r);
cout<<"Sum = "<<s<<endl;
goto y;

case 3:
row(a,r);
goto y;

case 4:
col(a,r);
goto y;

case 5:
goto x;

case 6:
exit(0);

default :
cout<<"Wrong Choice"<<endl;
break;

}
getch();
}
OUTPUT
PROGRAM 20.BINARY SEARCH:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intch,l,u,mid,c=0,i,n,e,a[50];
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<endl<<"Enter the elemnts"<<endl;
for(i=0;i<n;i++)
{
cout<<endl;
cin>>a[i];
}
cout<<"Enter the element to be searched for"<<endl;
cin>>e;
cout<<"Enter Your Choice 1:Linear 2:Binary Search";
cin>>ch;
switch(ch)
{
case 1 :
for(i=0;i<n;i++)
{
if(a[i]==e)
cout<<endl<<"Element is at "<<i+1<<" Position";
c=c+1;
}
if (c==0)
cout<<"Element Not Present";
break;

case 2:
c=0;
l=0;
u=n-1;
while(l<u)
{
mid=(l+u)/2;
if (a[mid]==e)
{
cout<<"Element is at "<<mid+1<<" Position "<<endl;
c++;
break;
}
if(e<a[mid])
u=mid-1;

if (e>a[mid])
l=mid+1;
}
if (c==0)
cout<<"Element not Present"<<endl;
break;

default :
cout<<"Wrong Choice"<<endl;
break;

}
getch();
}

OUTPUT

You might also like