Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to add to matrices using pointers

Posted By: Adalbert Fischer     Category: C Programming     Views: 9223

WRITE A PROGRAM CONTAINING A USER DEFINED FUNCTION
USING POINTER TO ADD TWO MATRICES TO RETURN THE
RESULTANT MATRIX TO THE CALLING FUNCTION.

Code for Program to add to matrices using pointers in C Programming

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

main()
{
    int *m1=0,*m2=0,*m3,i,j;
    int row,col;
    int * add(int *m1,int *m2,int row,int col);
    row=col=3;
    clrscr();
    printf("\nENTER VALUE FOR THE FIRST ARRAY:->");
    m1=(int*) malloc(sizeof(int)*row*col);
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",m1+i*col+j);
        }
    }
    printf("\nENTER VALUE FOR THE SECOND ARRAY:->");
    m2=(int*) malloc(sizeof(int)*row*col);
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",m2+i*col+j);
        }
    }
    m3=(int*) malloc(sizeof(int)*row*col);
    m3=add(m1,m2,row,col);
    printf("THE RESULT IS :->");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d ",*(m3+i*col+j));
        }
    }

}

int * add(int *m1,int *m2,int row,int col)
{
    int *m3=(int*) malloc(sizeof(int)*row*col);
    int i,j,k,a1;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            a1=*(m1+i*col+j)+*(m2+i*col+j);
            *(m3+i*col+j)=a1;
        }
    }
    return m3;
}
  
Share: 


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

Adalbert Fischer
Adalbert Fischer author of Program to add to matrices using pointers is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

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

 
No Comment Found, Be the First to post comment!