This program is written totally with pointer, where rows and columns are allocated dynamically.
There are 3 sets of array with equal rows and columns. The program generate automatic values for first 2 array by using rand() function. Then it uses 2 functions which are also defined. One is to add the 2 matrix and assign the result into a third and another subtract 2 matrix and assign the result into the third.
Here is the program.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
void printArray(int **arr,int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d\t",*(*(arr + i) + j));
printf("\n\n");
}
}
void sum(int **retarr,int **farr,int **sarr,int row,int col)
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
*(*(retarr + i) + j)=*(*(farr + i) + j) + *(*(sarr + i) + j);
}
void diff(int **retarr,int **farr,int **sarr,int row,int col)
{
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
*(*(retarr + i) + j)=*(*(farr + i) + j) - *(*(sarr + i) + j);
}
void main()
{
int **firstArr;
int **secondArr;
int **resultArr;
int col,row;
int i,j;
printf("\nEnter number of columns : ");
scanf("%d",&col);
printf("\nEnter number of rows : ");
scanf("%d",&row);
firstArr = (int **)malloc(row);
secondArr = (int **)malloc(row);
resultArr = (int **)malloc(row);
for(i=0;i<row;i++)
{
*(firstArr+i) = (int *)malloc(col);
for(j=0;j<col;j++)
*(*(firstArr + i) + j) = rand()%100+1;
}
for(i=0;i<row;i++)
{
*(secondArr+i) = (int *)malloc(col);
for(j=0;j<col;j++)
*(*(secondArr + i) + j) = rand()%100+1;
}
for(i=0;i<row;i++)
*(resultArr+i) = (int *)malloc(col);
diff(resultArr,firstArr,secondArr,row,col);
printArray(firstArr,row,col);
printf("\n........................\n");
printArray(secondArr,row,col);
printf("\n........................\n");
printArray(resultArr,row,col);
}
#clanguage #cprogramming #programminginc #turboc #matrix
No comments:
Post a Comment