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

Adding Matrices by Calling Header File: Hello.c

The document shows how to add two 2D matrices by including an add.h header file that defines an add function. The main function declares two 2D arrays as matrices, passes their pointers to the add function, which uses nested for loops to iterate through the matrices and sum their elements, storing the result in a third matrix and printing it.

Uploaded by

lawrenzi
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)
28 views2 pages

Adding Matrices by Calling Header File: Hello.c

The document shows how to add two 2D matrices by including an add.h header file that defines an add function. The main function declares two 2D arrays as matrices, passes their pointers to the add function, which uses nested for loops to iterate through the matrices and sum their elements, storing the result in a third matrix and printing it.

Uploaded by

lawrenzi
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

Adding matrices by calling header file :

Hello.c:

#include<stdio.h>

#include "add.h"

void main(){

int a[2][2] = {{1,2},{3,5}};

int b[2][2] = {{5,8},{9,1}};

int (*p1)[2] = a;

int (*p2)[2] = b;

add(p1,p2);

Add.h :

void add(int (*p1)[2], int (*p2)[2]){

int c[2][2],a[2][2],b[2][2],i,j,k,l;

for (int k = 0; k < 2; k++)

for (int l = 0; l < 2; l++)

{
a[k][l] = *(*(p1+k)+l);

b[k][l] = *(*(p2+k)+l);

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

c[i][j] = a[i][j] + b[i][j];

printf("%d\n",c[i][j] );

You might also like