0% found this document useful (0 votes)
22 views2 pages

Addition of Two Matrices Using Pointers

This document discusses two methods for adding matrices using pointers in C. The first method defines a function to add the matrices. The second method adds the matrices without a separate function by manipulating the pointers directly in main.

Uploaded by

Boomslang
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)
22 views2 pages

Addition of Two Matrices Using Pointers

This document discusses two methods for adding matrices using pointers in C. The first method defines a function to add the matrices. The second method adds the matrices without a separate function by manipulating the pointers directly in main.

Uploaded by

Boomslang
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/ 2

Addition of two matrices using pointers and functions

#include<stdio.h>
void add(int *,int *,int,int);
void main()
{
int a[3][3],b[3][3],i,j,r,c;
printf("Enter the row and column of the array");
scanf("%d%d",&r,&c);
printf("\nEnter the elements of the 'a'array");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",(&a[0][0]+i*c+j));
printf("\nEnter the elements of the 'b'array");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",(&b[0][0]+i*c+j));
add(*a,*b,r,c);
}
void add(int *p,int *q,int r,int c)
{
int i,j,d[3][3];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=*(p+i*c+j)+*(q+i*c+j);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",d[i][j]);
printf("\n");
}}

Addition of two matrices using pointers without using a function


#include<stdio.h>
void main()
{
int a[3][3],b[3][3],d[3][3],i,j,r,c;
int *p,*q,*g;
p=a;
q=b;
g=d;
printf("Enter the row and column of the array");
scanf("%d%d",&r,&c);
printf("\nEnter the elements of the 'a'array");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",(p+i*c+j));
printf("\nEnter the elements of the 'b'array");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",(q+i*c+j));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
*(g+i*c+j)=*(p+i*c+j)+*(q+i*c+j);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",*(g+i*c+j));
printf("\n");
}
}

You might also like