Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Mathematics ProgramRSS Feeds

Program to print addition of two matrices using pointers

Posted By: Reuben Brown     Category: C Programming     Views: 18158

Program to print addition of two matrices using pointers.

Code for Program to print addition of two matrices using pointers in C Programming

#include<stdio.h>
#include<conio.h>

int a[5][5],b[5][5],row,col;

void add(int(*)[5]);

int main()
{
    int c[5][5],i,j;
    clrscr();
    printf("Enter row : ");
    scanf("%d",&row);
    printf("Enter column : ");
    scanf("%d",&col);
    printf("Enter matrix A :\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter matrix B :\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    add(c);
    printf("Addition :\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d\t",c[i][j]);
        }
        printf("\n");
    }
    getch();
    return 0;
}

void add(int c[5][5])
{
    int i,j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }
}

/*
Enter row : 3
Enter column : 3
Enter matrix A :
3 3 3
3 3 3
3 3 3
Enter matrix B :
5 5 5
5 5 5
5 5 5
Addition :
8 8 8
8 8 8
8 8 8
*/
  
Share: 


Didn't find what you were looking for? Find more on Program to print addition of two matrices using pointers Or get search suggestion and latest updates.

Reuben Brown
Reuben Brown author of Program to print addition of two matrices using pointers is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Rahul Gonnade from India Comment on: Aug 11
why is this not working ?


#include<stdio.h>
#include<conio.h>
int * accept(int *a,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",a[i][j]);
return a;
}

int *add(int *a,int *b,int r,int c)
{
int i,j,*d;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
d[i][j]=a[i][j]+b[i][j];
return d;
}

int main()
{
int *p,*p1,*p3;
p=accept(p,2,2);
p1=accept(p1,2,2);
p3=add(p,p1,2,2);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",p3[i][j]);
}
printf("\n");
}
return 0;
}








View All Comments