0% found this document useful (0 votes)
2 views

lab5

The document outlines three experiments involving matrix operations, addition of two numbers using pointers, and swapping two numbers using pointers. It provides algorithms and C programs for each experiment, detailing steps for reading matrices, performing multiplication, and using pointers for arithmetic operations. Additionally, it includes sample outputs for the programs demonstrating their functionality.

Uploaded by

Anandhu Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lab5

The document outlines three experiments involving matrix operations, addition of two numbers using pointers, and swapping two numbers using pointers. It provides algorithms and C programs for each experiment, detailing steps for reading matrices, performing multiplication, and using pointers for arithmetic operations. Additionally, it includes sample outputs for the programs demonstrating their functionality.

Uploaded by

Anandhu Krishnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT NO: 15.

MATRIX OPERATIONS USING FUNCTION

AIM : To write a program for performing matrix multiplication . Use functions to (i) read two
matrix (ii) find the product of two matrices (iii)display a matrix.
ALGORITHM of main()

Step 1: Start

Step 2: Declare 2 matrices a,b and their rows and columns(m1, n1, m2, n2) and a choice
variable op
Step 3: Read m1, n1
Step 4: Call the function readmatrix(a,m1,n1)
Step 5: Read m2, n2
Step 6: Call the function readmatrix(b,m2,n2)
Step 7: To print Matrix a, call the function displaymatrix(a,m1,n1);
Step 8: To print Matrix b, call the function displaymatrix(b,m2,n2);
Step 9:To fid the product of two matrices , call the function multmatrix(a,b,m1,n1,n2)

ALGORITHM to read_matrix(matrix[10][10],int row,int col)

Step 1: Start
Step 2: i=0,j=0
Step 3: if(i<row)then goto step 4

else goto step 8

Step 4: j=0

Step 5: if(j<col) then read a[i][j],goto step 6 else goto step 7

Step 6: j=j+1,goto step 5

Step 7: i=i+1,goto step 3


Step 8: Stop

ALGORITHM to displaymatrix(int a[][100],int m,int n)

Step 1:Start

Step 2: =0,j=0

Step 3: if(i<m)then goto step 4 else goto step 10

Step 4: j=0

Step 5: if(j<n) then print a[i][j],goto step 6 else goto step 8

Step 6: Print a space

Step 7: j=j+1,goto step 5

Step 8: Print a new line

Step 9: i=i+1,goto step 3

Step 10: Stop

ALGORITHM of multmatrix(int a[][100],int b[][100],int m1,int n1,int n2)

Step 1: Start
Step 2:
Declare a
matrix c Step
3: i=0,j=0

Step 4: if(i<m1)then goto step 5 else


goto step 12 Step 5: j=0
Step 6: if(j<n2) then c[j][i]=0 ,goto step 7
else goto step 8 Step 7: k=0

Step 8: if(k<n1) then c[i][j]=c[i][j]+ a[i][k] * b[k][j],goto step 9 else goto step 10
Step 9: k=k+1,goto step 8

Step 10: j=j+1,goto step 6


Step 11: i=i+1,goto step 4
Step 12: Call the function
displaymatrix(c,m1,n2) Step 13:
Stop

PROGRAM
#include <stdio.h>
void readmatrix(int a[][100],int m,int n)
{
int i,j;
printf("enter the elements row by row\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void displaymatrix(int a[][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
}
void multmatrix(int a[][100],int b[][100],int m1,int n1,int n2)
{
int c[100][100],i,j,k;

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


{
for (j = 0; j < n2; j++)
{
c[i][j] = 0;
for (k = 0; k < n1; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
printf("Product of matrix...\n");
displaymatrix(c,m1,n2);
}
void main()
{
int a[100][100],b[100][100],m1,n1,m2,n2,op;
printf("Enter the size of the matrix A row,column\n");
scanf("%d%d",&m1,&n1);
printf("Enter Matrix A\n");
readmatrix(a,m1,n1);
printf("Enter the size of the matrix B row column\n");
scanf("%d%d",&m2,&n2);
printf("Enter Matrix B\n");
readmatrix(b,m2,n2);
printf("Matrix A..\n");
displaymatrix(a,m1,n1);
printf("Matrix B..\n");
displaymatrix(b,m2,n2);
multmatrix(a,b,m1,n1,n2);
}

EXPERIMENT NO: 16

ADD TWO NUMBERS USING POINTERS


AIM: To write a program Add two numbers using pointer

ALGORITHM
Step 1: Start
Step 2: Initialize two integer pointers p,q.
Step 3: Read a,b
Step 4: Reference the pointers to variables using '&' operator.//p=&a,q=&b
Step 5: Now, add the values, using * operator//sum=*p+*q
Step 6: Print the sum.
Step 7: Stop

PROGRAM

#include <stdio.h>
void main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
}

OUTPUT
administrator@administrator-Vostro-3800:~/cplab$ gcc add.c
administrator@administrator-Vostro-3800:~/cplab$ ./a.out
Enter two integers to add
45 23
Sum of the numbers = 68

EXPERIMENT NO: 17

SWAP TWO NUMBERS USING POINTERS

AIM: To write a program to swap two numbers using a user defined function.

ALGORI
THM

Step 1: Start
Step 2: Initialize two integer pointers p,q.

Step 3: Read x,y


Step 4: Reference the pointers to variables using '&' operator.//p=&x,q=&y
Step 5: Now, interchange the values, using * operator//t=*p,*p=*q,*q=t
Step 6: Print the swapped values using * operator.//Print *p,*q
Step 7: Stop

PROGRAM

#include <stdio.h>

void swap(int *p, int *q)

{
int temp = *p;

*p = *q;
*q= temp;

}
void main()

{
int x, y;

printf("Enter Value of x ");


scanf("%d", &x);

printf("\nEnter Value of y ");

scanf("%d", &y);
swap(&x, &y);

printf("\nAfter Swapping: x = %d, y = %d\n", x, y);


}

OUTPUT

administrator@administrator-Vostro-3800:~/cplab$ gcc swap.c

administrator@administrator-Vostro-3800:~/cplab$ ./a.out
Enter Value of x 56
Enter Value of y 98

After Swapping: x = 98, y = 56


EXPERIMENT NO: 20
PRINT THE ELEMENTS OF AN ARRAY USING
POINTERS

AIM To write a program Input and Print the elements of an


array using pointers

ALGORITHM

Step 1: Start
Step 2: Read size of the array to n

Step 3: Initialize one integer pointer *ptr


Step 4: i=0
Step 5: ptr=&a[0]
Step 6: if(i<n) then goto step 7 else goto step 10

Step 7: Read a number to ptr


Step 8: ptr=ptr+1

Step 9: i=i+1,goto step 6

Step 10: i=0


Step 11: ptr=&a[0]
Step 12: if(i<n) then goto step 13 else goto step 16
Step 13: Print a number using pointer ptr
Step 14: ptr=ptr+1

Step 15: i=i+1,goto step 12

Step 16: Stop


PROGRAM

#include <stdio.h>
void main()
{
int arr[100];
int n, i;
int * ptr = arr;
printf("Enter size of array: ");
scanf("%d", &n);
printf("Enter elements in array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", (ptr + i));
}
printf("Array elements: \n");
for (i = 0; i < n; i++)
{
printf("%d\n", *(ptr + i));
}
}

OUTPUT

administrator@administrator-Vostro-3800:~/cplab$ gcc
ptrarray.c

administrator@administrator-Vostro-3800:~/cplab$ ./a.out
Enter size of array: 5

Enter elements in array:

Array elements:

You might also like