0

Below is the function I copied to get the answer...so plz do tell us the simple method for this.....

int ** addmatrix(int **a,int x1,int y1,int **b,int x2,int y2)
	{
	int i,j,**c;
	c=(int**)malloc(x1*sizeof(int*));
	for(i=0;i<x1;i++)
	*(c+i)=(int*)malloc(y1*sizeof(int));
	if(x1==x2 && y1==y2)
	for(i=0;i<x1;i++)
		for(j=0;j<y1;j++)
		*(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
		return(c);
	}
3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by N1GHTS
0

The simplest way to avoid this, on this forum, is to stay AWAY from "NIGHTS". He's a pointer fan, big time! ;)

Seriously, use indeces (aka indexes), instead of pointers. Let C handle the change from index to pointer value.

Are you trying to add up the whole array?

Edited by Adak: n/a

0

@Adak

Your right. Pointers make me salivate.

@advait_123

c=(int**)malloc(x1*sizeof(int*));
for(i=0;i<x1;i++)
   *(c+i)=(int*)malloc(y1*sizeof(int));

Highly inefficient. malloc wastes tons of CPU to do its job, so use it as little as possible.

I'd suggest making one big array and performing math to divide it into rows and columns. Its faster than your code.

And by doing it that way you don't need nested for loops, you can just add the arrays in one for loop. That would make the code faster to execute, more efficient, and less lines of code.

Edited by N1GHTS: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.