lab5
lab5
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)
Step 1: Start
Step 2: i=0,j=0
Step 3: if(i<row)then goto step 4
Step 4: j=0
Step 1:Start
Step 2: =0,j=0
Step 4: j=0
Step 1: Start
Step 2:
Declare a
matrix c Step
3: i=0,j=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
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;
EXPERIMENT NO: 16
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
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.
PROGRAM
#include <stdio.h>
{
int temp = *p;
*p = *q;
*q= temp;
}
void main()
{
int x, y;
scanf("%d", &y);
swap(&x, &y);
OUTPUT
administrator@administrator-Vostro-3800:~/cplab$ ./a.out
Enter Value of x 56
Enter Value of y 98
ALGORITHM
Step 1: Start
Step 2: Read size of the array to n
#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
Array elements: