Вы находитесь на странице: 1из 62

SREE NARAYANA COLLEGE

KOLLAM

B.Sc Physics with Computer Application


COMPUTER
PRACTICAL RECORD
YEAR 2008-2011

NAME : Jayu Prakash

CLASS No: 742 BATCH : 2nd B.Sc Physics With Computer Application

REG.No OF 2008 – 2011

(Seal) Certified
Bonafide Record

Head of the Department Lecturer in charge

Valued with Reg.No Of

Date

Examiners:

1| record
Index

P.no Program Date Page.No Initial of


Teacher
1. Program to insert an element into a 5/08/09 4
sorted array
2. Program to delete an element from
an array 7/08/09 7
3. Program to insert elements to an array
and print it in Matrix format 12/08/09 10

4. Program to liniar sort an array 14/08/09 12

5. Program to bubble sort an array 19/08/09 14

6. Program to insertion sort an array 21/08/09 16

7. Program to selection sort an array 26/08/09 18

8. Program to merge sort an array 28/08/09 20

9. Program to binary search an array 16/09/09 22

10. Program to leniar search an array 16/09/09 24

11. Program to push elements into a stack 18/09/09 26

12. Program to pop elements from a stack 23/09/09 28


13. Program to insert an element into the
queue 25/09/09 31
14. Program to delete an element from a
queue 30/09/09 34
15. Program to insert and delete elements
from a circular queue 7/09/09 37

2| record
16. Program to insert elements in the
beginning of a linked list 41
17. Program to insert elements in the
end of a linked list 44
18. Program to traverse elements in a
linked list 47
19. Program to delete elements from a
linked list 50
20. Program to perform matrix operations 53

3| record
1. Program to insert an element into a sorted array

#include<iostream.h>
#include<conio.h>
#include<process.h>
int findpos(int[],int,int);
void main()
{clrscr();
int ar[50],item,n,index;
cout<<"How many elements do U want to create an array?(max 50) : ";
cin>>n;
cout<<"\n Enter array elements (must be sorted in assending order)\n";
for(int i=0;i<n;i++)
cin>>ar[i];
char ch='y';
while(ch=='y'||ch=='Y')
{cout<<"\n Enter elements to be inserted :\n";
cin>>item;
if(n==50)
{cout<<"\noverflow";
exit(1);
}
index=findpos(ar,n,item);
for(i=n;i>index;i--)
ar[i]=ar[i-1];
ar[index]=item;
n++;
cout<<"\nWant to insert more elements(y/n): ";
cin>>ch;
}
cout<<"The array now is as shown below\n ";
for(i=0;i<n;i++)
{
cout<<ar[i]<<" ";
}
cout<<"\n";
getch();
}
int findpos(int ar[],int size,int item)
{int pos;
if(item<ar[0])
pos=0;
else
{for(int i=0;i<size-1;i++)
{if(ar[1]<=item&& item<ar[i+1])
{pos=i+1;
break;

4| record
}}
if(i==size-1)
pos=size;
}
return pos;
}

5| record
Output

6| record
2.Program to delete an element from an array

# include<iostream.h>
# include<conio.h>
# include<process.h>
int lsearch(int[],int,int);
void main()
{int ar[50],item,n,index,i;
clrscr();
cout<<"\n How many elements do u want to enter :";
cin>>n;
cout<<"\n Enter the Array elements\n";
for(i=0;i<n;i++)
cin>>ar[i];
char ch='y';
while(ch=='y'||ch=='Y')
{

if(n==0)
{cout<<"\nUnder flow!!\n";
getch();
exit(1);
}
cout<<"\n Enter elements to be deleted :";
cin>>item;
index=lsearch(ar,n,item);
if(index==-1)
{cout<<"\n sorry.. No such element found";
getch();
goto l;
}
else
ar[index]=0;
cout<<"\n After this the array is zero indicates empty spaces :\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<"\n";
cout<<" The Emptied space is shifted to right";
n-=1;
for(i=index;i<n;i++)
ar[i]=ar[i+1];
l:
cout<<"\n Do u want to delete more (y/n)";
cin>> ch;
}
cout<<"\n The new array is\n";

7| record
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<"\n";
getch();
}
int lsearch(int ar[],int n, int item)
{
for(int i=0;i<n;i++)
{
if(ar[i]==item)
{return(i);
}
}

return(-1);

8| record
Output

9| record
3.Program to insert elements to an array and print it in Matrix
format

#include<iostream.h>
#include<conio.h>
#include<process.h>
void mat(int[],int);
void main()
{
int i,ar[20],n,item;
clrscr();
cout<<"\n enter Number Of Terms : ";
cin>>n;
if(n<=0)
{
cout<<"\n Underflow !!! \n";
getch();
exit(0);
}
cout<<"\n Enter Elements one by one\n";
for(i=0;i<n;i++)
cin>>ar[i];
cout<<" The matrix is \n";
mat(ar,n);
getch();
}
void mat (int ar[],int n)
{
int i,j,item;
for(i=0;i<n;i++)
cout<<"\t"<<ar[i];
for(j=1;j<n;j++)
{
cout<<"\n\n";
item=ar[0];
for(i=0;i<n;i++)
ar[i]=ar[i+1];
ar[n-1]=item;
for(i=0;i<n;i++)
cout<<"\t"<<ar[i];
}
}

10 | r e c o r d
Output

11 | r e c o r d
4.Program to liniar sort an array

#include<iostream.h>
#include<conio.h>
void sort(int[],int);
void main()
{
int ar[10],i,j,n,temp;
clrscr();
cout<<"\nEnter the size of Array\n";
cin>>n;
cout<<"\nEnter the elements one by one\n";
for(i=0;i<n;i++)
{cin>>ar[i];
}
sort(ar,n);
cout<<"The sorted array is\n";
for(i=0;i<n;i++)
{cout<<ar[i]<<"\n";
}
getch();
}
void sort(int ar[],int n)
{ int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(ar[i]>ar[j])
{temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
}

12 | r e c o r d
Output

13 | r e c o r d
5.Program to bubble sort an array

#include<iostream.h>
#include<conio.h>
void bsort(int[],int);
void main()
{
int a[50],i,j,n;
clrscr();
cout<<"\n Enter the size of the array : ";
cin>>n;
cout<<"\n Enter the elements one by one:\n";
for(i=0;i<n;i++)
cin>>a[i];
bsort(a,n);
cout<<"\n The sorted array is : \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\n";
getch();
}
void bsort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

14 | r e c o r d
Output

15 | r e c o r d
6.Program to insertion sort an array

#include<iostream.h>
#include<conio.h>
#include<limits.h>
void insort(int[],int);
void main()
{
int ar[50],item,n,index;
clrscr();
cout<<"How many Elements do you want to create an array with : ";
cin>>n;
cout<<"\n Enter Array elements\n";
for(int i=1;i<=n;i++)
cin>>ar[i];
insort(ar,n);
cout<<"\n\n Sorted array is shown below\n";
for(i=1;i<=n;i++)
cout<<ar[i]<<" ";
cout<<"\n";
getch();
}
void insort(int ar[],int size)
{
int temp,j;
ar[0]=INT_MIN;
for(int i=1;i<=size;i++)
{
temp=ar[i];
j=i-1;
while(temp<ar[j])
{
ar[j+1]=ar[j];
j--;
}
ar[j+1]=temp;
}
}

16 | r e c o r d
Output

17 | r e c o r d
7.Program to selection sort an array

#include<iostream.h>
#include<conio.h>
void select(int[],int);
void main()
{
int ar[50],item,n,index;
clrscr();
cout<<"How many elements do u want to enter\n";
cin>>n;
cout<<"\n enter Array elements\n";
for(int i=0;i<n;i++)
cin>>ar[i];
select(ar,n);
cout<<"\nThe array after sort is:\n";
for(i=0;i<n;i++)
{
cout<<ar[i]<<" ";
}
getch();
}
void select(int ar[],int size)
{
int small,pos,temp;
for(int i=0;i<size;i++)
{
small=ar[i];
for(int j=i+1;j<size;j++)
{
if(ar[j]<small)
{
small=ar[j];
pos=j;
temp=ar[i];
ar[i]=ar[pos];
ar[pos]=temp;
}
}
cout<<"\n Array after pos"<<i+1<<" is ";
for(j=0;j<size;j++)
cout<<ar[j]<<" ";

18 | r e c o r d
}
}

Output

19 | r e c o r d
8.Program to merge sort an array

#include<iostream.h>
#include<conio.h>
void merge(int[],int,int[],int,int[]);
void main()
{
int A[50],B[50],C[50],mn=0,m,n,i;
clrscr();
cout<<"\How many elements do u want to create first array with(max 50:\n";
cin>>m;
cout<<"\n Enter the first array's elements (ascending)\n";
for(i=0;i<m;i++)
cin>>A[i];
cout<<"\n How many elements do u want to create array with (max 50):\n";
cin>>n;
mn=m+n;
cout<<"\n Enter second array's elements (descending )\n";
for(i=0;i<n;i++)
cin>>B[i];
merge(A,m,B,n,C);
cout<<"\n The merged array is shown below:\n";
for(i=0;i<mn;i++)
cout<<C[i]<<" ";
cout<<"\n";
getch();
}
void merge(int A[],int m,int B[], int n,int C[])
{int a,b,c;
for(a=0,b=n-1,c=0;a<m&&b>=0;)
{ if(A[a]<=B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
}
if(a<m)
{while(a<m)
C[c++]=A[a++];
}
else
{ while(b>=0)
C[c++]=B[b--];

20 | r e c o r d
}
}

Output

21 | r e c o r d
9.Program to binary search an array

#include<iostream.h>
#include<conio.h>
void bsearch(int[],int n,int item);
void main()
{
int a[50],i,j,n,item;
clrscr();
cout<<"\n Enter the size of the array: ";
cin>>n;
cout<<"\n Enter the elements one by one: \n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter the element to be searched : ";
cin>>item;
bsearch(a,n,item);
getch();
}
void bsearch(int a[],int n,int item)
{
int end,beg=0,mid,fl=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
cout<<"\nThe element is found at the positon : "<<mid+1<<"\n";
fl=1;
break;
}
else if(a[mid]>item)
end=mid-1;
else
beg=mid+1;
}

if(fl==0)
cout<<"\nThe number is not found !!!";

22 | r e c o r d
}

Output.

23 | r e c o r d
10.Program to leniar search an array

#include<iostream.h>
#include<process.h>
#include<conio.h>
void search(int[],int,int);
void main()
{
int a[50],i,n,item;
clrscr();
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\nEnter the elements:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nEnter the searching element:";
cin>>item;
search(a,n,item);
getch();
}
void search(int a[],int n,int item)
{
int fl=0;
for(int i=0;i<n;i++)
{
if(a[i]==item)
{
cout<<"\nElement is found at the position "<<i+1;
fl=1;
}
}
if(fl==0)
{cout<<"\nElement is not found";
getch();
exit(0);
}

24 | r e c o r d
}

Output

25 | r e c o r d
11.Program to push elements into a stack

# include<iostream.h>
# include<conio.h>
# include<process.h>
int push(int[],int&,int);
void display(int[],int);
const int size=50;
void main()
{int stack[size],item,top=-1,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter Item Of insertion\n";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{
cout<<" \nOverflow!!!...\n";
getch();
exit(1);
}
cout<<"\n The stack is\n";
display(stack,top);
cout<<"\n want to insert more elements?(y/n)\n";
cin>>ch;
}
getch();
}
int push(int stack[],int &top,int ele)
{
if(top==size-1)
return (-1);
else
{
top++;

26 | r e c o r d
stack[top]=ele;
}
return 0;
}
void display(int stack[],int top)
{
cout<<stack[top]<<"<------"<<"\n";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"\n";
}}
Output

27 | r e c o r d
12.Program to pop elements from a stack

# include<iostream.h>
# include<conio.h>
# include<process.h>
int push(int[],int&,int);
int pop(int[],int&);
void display(int[],int&);
const int size=50;
void main()
{int stack[size],item,top=-1,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<" Enter Item Of insertion : ";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{
cout<<" \nOverflow!!!...\n";
exit(1);
}
cout<<" The stack is\n";
display(stack,top);
cout<<"\n want to insert more elements?(y/n) : ";
cin>>ch;
}
cout<<"\nnow the deletion of element begins";
ch='y';
while(ch=='y'||ch=='Y')
{
res=pop(stack,top);
if(res==-1)
{cout<<"Underflow!!! aborting";

28 | r e c o r d
getch();
exit(1);
}
else
{cout<<"\n Element deleted is :"<<res<<"\n";
cout<<" The stack now is:\n";
display(stack,top);
}
cout<<"\nWant to delete more elements:(y/n) :";
cin>>ch;
}
getch();
}

int push(int stack[],int &top,int ele)


{
if(top==size-1)
return (-1);
else
{
top++;
stack[top]=ele;
}
return 0;
}
void display(int stack[],int &top)
{ if(top==-1)
return;
cout<<stack[top]<<"<------"<<"\n";
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<"\n";
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return(-1);
else
{ ret=stack[top];
top--;
}
return ret;

29 | r e c o r d
Output

30 | r e c o r d
13.Program to insert an element into the queue

#include<process.h>
#include<conio.h>
#include<iostream.h>
int insert(int[],int);
void display(int[],int,int);
const int size=50;
int queue[size],front=-1,rear=-1;
void main()
{
int item,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter item for insertion\n";
cin>>item;
res=insert(queue,item);
if(res==1)
{
cout<<"Overflow!!!";
exit(1);
}
cout<<"Now the Queue ( front...to...rear)is\n\n";
display(queue,front,rear);
cout<<"\n\n\nWant to insert more elements?(y/n)";
cin>>ch;
}
}
insert(int Queue[],int ele)
{ if(rear==size-1)
{
return -1;
}
else if(rear==-1)

31 | r e c o r d
{
front=rear=0;
queue[rear]=ele;
}
else
{ rear++;
queue[rear]=ele;
}
return 0;
}
void display(int queue[],int front,int rear)
{
if(front==-1)
{
return;
}
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<"\t";
}
}

32 | r e c o r d
Output

33 | r e c o r d
14.Program to delete an element from a queue

#include<iostream.h>
#include<conio.h>
#include<process.h>
int remove(int[]);
int insert(int[],int);
void display(int[],int,int);
const int size=50;
int queue[size],front=-1,rear=-1;
void main()
{
int item,res;
char ch='y';
clrscr();
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter item for insertion : ";
cin>>item;
res=insert(queue,item);
if(res==-1)
{
cout<<"Overflow!!! aborting";
exit(1);
}
cout<<"Now the queue (front..to..rear)is \n";
display(queue,front,rear);
cout<<"\nWant to insert more elements?(y/n) ";
cin>>ch;
}
cout<<"Now deletion of element begins\n";
ch='y';
while(ch=='y'||ch=='Y')
{
res=remove(queue);
if(res==-1)

34 | r e c o r d
{
cout<<"\n Underflow!!! aborting";
getch();
exit(1);
}
else
{
cout<<"\n the element deleted is: "<<res<<"\n";
cout<<"Now the queue (front..to..rear)is:\n ";
display(queue,front,rear);
}
cout<<"\nWant to delete more elements?(y/n) : ";
cin>>ch;
}
}
int insert(int queue[],int ele)
{
if (rear==size-1)
return(-1);
else if(rear==-1)
{front=rear=0;
queue[rear]=ele;
}
else
{
rear++;
queue[rear]=ele;
}
return(0);
}
int remove(int queue[])
{
int ret;
if(front==-1)
return -1;
else
{
ret=queue[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return ret;
}
void display(int queue[],int front,int rear)
{
if(front==-1)

35 | r e c o r d
return;
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<"\t";

}
}

Output

36 | r e c o r d
15.Program to insert and delete elements from a circular queue

#include<iostream.h>
#include<conio.h>
#include<process.h>
int insert(int[],int);
int front,rear;
void display(int[],int);
const int size=7;
int insert_in_cq(int cqueue[],int ele)
{
if((front==0&&rear==size-1)||(front==rear+1))
return(-1);
else if(rear==-1)
front=0;
else if(rear==size-1)
rear=0;
else
rear++;
cqueue[rear]=ele;
return(0);
}
void display(int cqueue[],int front,int rear)
{
int i=0;
cout<<"\n circular queue is \n"<<"\nfront shown is)>>rear as<<<and free space as-)\n";
if(front==-1)
return;
if(rear>=front)
{
for(i=0;i<front;i++)
cout<<"_";
cout<<">>>";
for(i=front;i<rear;i++)
cout<<cqueue[i]<<"<-";
cout<<cqueue[rear]<<"<<<"<<endl;

37 | r e c o r d
}
else
{
for(i=0;i<rear;i++)
cout<<cqueue[i]<<"<-";
cout<<cqueue[rear]<<"<<<";
for(;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<size;i++)
cout<<cqueue[i]<<"<-";
cout<<"\t wrap around";
}
}
int del_in_cq(int cqueue[])
{
int ret;
if(front==-1)
return(-1);
else
{
ret=cqueue[front];
if(front==rear)
front=rear=-1;
else if(front==size-1)
front=0;else
front++;
}
return (ret);
}
void main()
{
int item,res,ch,cqueue[50];
do
{
clrscr();
cout<<"\t\t\t circular queue menu \n";
cout<<"\t 1.insert \n";
cout<<"\t 2.delete \n";
cout<<"\t 3.display \n";
cout<<"\t 4.exit\n";
cout<<"enter your choice(1-4) : ";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n enter item for insertion : ";
cin>>item;
res=insert_in_cq(cqueue,item);

38 | r e c o r d
if(res==-1)
cout<<"overflow!!\n";
else
{
cout<<"\n now the queue is :\n";
display(cqueue,front,rear);
}
getch();
break;
case 2:item=del_in_cq(cqueue);
cout<<"element deleted is<<element\n";
display(cqueue,front,rear);
getch();
break;
case 3:display(cqueue,front,rear);
getch();
break;
case 4:break;
default:cout<<"valid choices are 1--4 only \n";
getch();
break;
}
}
while(ch!=4)
}

39 | r e c o r d
Output

40 | r e c o r d
16.Program to insert elements in the beginning of a linked list

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int inf;
node *next;
}*start,*newptr,*save,*ptr;
node *create(int n)
{
ptr=new node;
ptr->inf=n;
ptr->next=NULL;
return (ptr);
}
void insert(node *np)
{
if(start==NULL)
start=np;
else
{
save =start;
start=np;
np->next=save;
}
}
void display(node*np)
{
while(np!=NULL)
{
cout<<np->inf<<"->";
np=np->next;
}
}

41 | r e c o r d
void main()
{
start=NULL;
int inf;
char ch;
clrscr();
ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n enter info for the new node : ";
cin>>inf;
newptr=create(inf);

if(newptr!=NULL)
cout<<"\n the node is create\n";
else
{
cout<<"\n sorry you are failed !!";
exit(0);
}

insert(newptr);
cout<<"\n now the list is : ";
display(start);
cout<<"\n press y to add N to exit : ";
cin>>ch;
}
getch();
}

42 | r e c o r d
Output

43 | r e c o r d
17.Program to insert elements in the end of a linked list

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int inf;
node *next;
}*start,*newptr,*rear,*ptr;
node *create(int n)
{
ptr=new node;
ptr->inf=n;
ptr->next=NULL;
return (ptr);
}
void insert(node*np)
{
if(start==NULL)
start=rear=np;
else
{rear->next=np;
rear=np;
}
}

void display(node*np)
{
while(np!=NULL)
{
cout<<np->inf<<"->";
np=np->next;
}
}
void main()

44 | r e c o r d
{
clrscr();
start=rear=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')
{

cout<<"\n enter info for the new node : ";


cin>>inf;

newptr=create(inf);
if(newptr!=NULL)

cout<<"\n the node is created ";


else
{
cout<<"\n sorry you are failed !!";
}
insert(newptr);
cout<<"\n now the list is\n ";
display(start);
cout<<"\n press y to add N to exit : ";
cin>>ch;
}
getch();
}

45 | r e c o r d
Output

46 | r e c o r d
18.Program to traverse elements in a linked list

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int inf;
node *next;
}*start,*newptr,*rear,*ptr;
node *create(int n)
{
ptr=new node;
ptr->inf=n;
ptr->next=NULL;
return (ptr);
}
void insert(node *np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void traverse(node*np)
{
while(np!=NULL)
{
cout<<np->inf<<"->";
np=np->next;
}
}

47 | r e c o r d
void main()
{
start=rear=NULL;
int inf;
char ch;
clrscr();
ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n enter info for the new node : ";
cin>>inf;
newptr=create(inf);
if(newptr==NULL)
cout<<"\n the node is not create";
insert(newptr);
cout<<"\n do you want to insert more elemets : ";
cin>>ch;
}
cout<<"\n the list is :\n";
traverse (start);
getch();
}

48 | r e c o r d
Output

49 | r e c o r d
19.Program to delete elements from a linked list

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr,*rear;
node *create_new_node(int);
void insert(node*);
void display(node*);
void delnode();
void main()
{
start=rear=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')
{
clrscr();
cout<<"\n enter information for the new node : ";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{
cout<<"\n cannot create new node!!!aborting";
getch();
exit(1);
}
insert(newptr);
cout<<"\n press Y to enter more nodes N to exit : ";
cin>>ch;
}
clrscr();

50 | r e c o r d
do
{
cout<<"\n the list now is\n";
display(start);
getch();
cout<<"want to delete first node?(y/n) : ";
cin>>ch;
if(ch=='y'||ch=='Y')
delnode();
}
while(ch=='y'||ch=='Y');
}
node *create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return(ptr);
}
void insert(node *np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void delnode()
{
if(start==NULL)
{
cout<<"UNDERFLOW!!!";
getch();
exit(0);
}
else
{
ptr=start;start=start->next;
delete ptr;
}
}
void display(node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";

51 | r e c o r d
np=np->next;
}
cout<<"!!!\n";
}

Output

52 | r e c o r d
20.Program to perform matrix operations
#include<iostream.h>
#include<conio.h>
#include<process.h>

void main()
{clrscr();
char ch;
int a[10][10],b[10][10],d[10][10];
int m,n,p,q,r,c,s,flag=0;
l:
clrscr();
cout<<"\n Matrix Operations \n 1. addition \n 2. substraction \n 3. multiplication \n 4. transpose \n
5. Symmetric \n 6. exit\n Enter Choice: ";
cin>>ch;
switch(ch)
{
case '1':cout<<"\nEnter order of 1st matrix :\n";
cin>>m>>n;
cout<<"\nEnter order of second matrix:\n";
cin>>p>>q;
if(m!=p&&n!=q)
{
cout<<"\n Matrix addition not possible";
getch();
goto l;
}
else

53 | r e c o r d
{
cout<<"\nEnter 1st matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>a[r][c];
}
cout<<"\nEnter 2nd matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>b[r][c];
}
for(r=0;r<m;r++)
{
for(c=0;c<q;c++)
d[r][c]=a[r][c]+b[r][c];
}
clrscr();
cout<<"\n The 1st matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<a[r][c];
}
cout<<"\n The 2nd matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<b[r][c];
}

cout<<"\n The resultant matrix is\n";


for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<d[r][c];
}
}
getch();
goto l;

case '2':cout<<"\nEnter order of 1st matrix :\n";


cin>>m>>n;
cout<<"\nEnter order of second matrix:\n";
cin>>p>>q;
if(m!=p&&n!=q)
{

54 | r e c o r d
cout<<"\n Matrix subtraction not possible";
getch();
goto l;
}
else
{
cout<<"\nEnter 1st matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>a[r][c];
}
cout<<"\nEnter 2nd matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>b[r][c];
}
for(r=0;r<m;r++)
{
for(c=0;c<q;c++)
d[r][c]=a[r][c]-b[r][c];
}
clrscr();
cout<<"\n The 1st matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<a[r][c];
}
cout<<"\n The 2nd matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<b[r][c];
}
cout<<"\n The resultant matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<d[r][c];
}
}
getch();
goto l;
case '3':cout<<"\nEnter order of 1st matrix :\n";
cin>>m>>n;
cout<<"\nEnter order of second matrix:\n";

55 | r e c o r d
cin>>p>>q;
if(m!=q)
{
cout<<"\n Matrix multipication not possible";
getch();
goto l;
}
else
{
cout<<"\nEnter 1st matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>a[r][c];
}
cout<<"\nEnter 2nd matrix\n";
for(r=0;r<p;r++)
{
for(c=0;c<q;c++)
cin>>b[r][c];
}
for(r=0;r<m;r++)
{
for(c=0;c<q;c++)
{ d[r][c]=0;
for(s=0;s<n;s++)
d[r][c]=d[r][c]+a[r][s]*b[s][c];
}
}
clrscr();
cout<<"\n The 1st matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<n;c++)
cout<<"\t"<<a[r][c];
}
cout<<"\n The 2nd matrix is\n";
for(r=0;r<p;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<b[r][c];
}
cout<<"\n The resultant matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<q;c++)
cout<<"\t"<<d[r][c];
}

56 | r e c o r d
}
getch();
goto l;
case '4':cout<<"\n Enter order of matrix\n";
cin>>m>>n;
cout<<"\nEnter the matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>a[r][c];
}
clrscr();
cout<<"\noriginal matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<n;c++)
cout<<"\t"<<a[r][c];
}
cout<<"\ntranspose matrix is\n";
for(r=0;r<n;r++)
{cout<<"\n";
for(c=0;c<m;c++)
cout<<"\t"<<a[c][r];
}
getch();
goto l;
case '5': cout<<"\n Enter order of matrix\n";
cin>>m>>n;
if(m!=n)
{cout<<"invalid option!!! Should be a square matrix";
getch();
goto l;
}
else
{
cout<<"\nEnter the matrix\n";
for(r=0;r<m;r++)
{
for(c=0;c<n;c++)
cin>>a[r][c];
}
clrscr();
for(r=0;r<m;r++)
{for(c=0;c<n;c++)
{if(a[r][c]!=a[c][r])
flag=1;
}
}

57 | r e c o r d
if(flag==1)
{cout<<"\n\nThe given matrix is not symmetric" ;
getch();
goto l;
}
else
{
cout<<"\nOriginal matrix is\n";
for(r=0;r<m;r++)
{cout<<"\n";
for(c=0;c<n;c++)
cout<<"\t"<<a[r][c];
}
cout<<"\nTranspose matrix is\n";
for(r=0;r<n;r++)
{cout<<"\n";
for(c=0;c<m;c++)
cout<<"\t"<<a[c][r];
}

cout<<"\nThe given matrix is symmetric";


getch();
goto l;
}
}
case '6': exit(0);

default : "\n Invalid Choice!!! ";


getch();
goto l;

58 | r e c o r d
Output

59 | r e c o r d
60 | r e c o r d
61 | r e c o r d
62 | r e c o r d

Вам также может понравиться