Slip
6_2 sum of elememts of mxn matrix using dynamic memory allocation */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int
**m,i,j,sum,r,c; clrscr();
printf("\n
Enter how many row and column for matrix : "); scanf("%d
%d",&r,&c);
m=(int **)malloc(r*sizeof(int));
for(i=0;i<c;i++)
{
m[i]=(int *)malloc(c*sizeof(int));
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n Enter data : ");
scanf("%d",&m[i][j]); //scanf("%d",m+i);
}
}
printf("\n
Martix is : \n"); sum=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum=sum+m[i][j];
//sum=sum+(*(m+i)); printf("%d\t ",m[i][j]);
}
printf("\n");
}
printf("\n
Sum of elements of matrix is = %d ",sum); getch();
}
No comments:
Post a Comment