#include<stdio.h>
void AddMatrix(int[2][3],int[2][3],int[2][3],int,int);


int main()
{


int i,j;
int matrixA[2][3], matrixB[2][3], matrixC[2][3];
printf("This Program is to find the summation of matrixA and matrixB\n\n");


for(i=0;i<=1;i++){
for(j=0;j<=2;j++){
printf("Please enter matrixA[%d][%d]:",i+1,j+1);
scanf("%d", &matrixA[j]);
}
}


printf("\n\n");
for(i=0;i<=1;i++){
for(j=0;j<=2;j++){
printf("Please enter matrixB[%d][%d]:",i+1,j+1);
scanf("%d", &matrixB[j]);
}
}
printf("\n\n");
AddMatirix(matrixA,matrixB,matrixC);
return 0;
}
void AddMatrix(int matrixC[2][3], int matrixA[2][3], int matrixB[2][3], int i, int j)
{


for(i=0;i<=1;i++){
for(j=0;j<=2;j++){


matrixC[2][3] = matrixA[j] + matrixB[j];


}
}



for(i=0;i<=1;i++){
for(j=0;j<=2;j++){
printf("matrixC is the summation of both matrixA and matrixB: %d", matrixC[j]);
}
}
printf("\n\n");
return;
}

please help this out..
I'm confused..

Dani AI

Generated

The code in the first post has a few separate, repeatable mistakes. Fixing those will make the program behave predictably.

  • Out‑of‑bounds indexing and loop bounds: arrays declared as int a[2][3] have valid indexes 0..1 for rows and 0..2 for columns. Use for (i = 0; i < rows; ++i) and for (j = 0; j < cols; ++j) — avoid <=. Writing to a[2][3] is past the end of the array and causes undefined behavior.
  • Input addresses: when reading an element you must pass the address of that specific element (the element at [i][j]), not a single-dimension pointer or &array[j].
  • Function prototype / call / order: the function declaration, definition and the call must agree exactly (name, parameter types and their order). Do not pass loop indices as parameters unless you intend them to be inputs. Prefer a signature that takes rows and cols explicitly and the three arrays in the same order each time.
  • Use local loop counters inside the add/display functions instead of sharing globals. Globals like i and j make reasoning about code harder and are a common source of errors.
  • Check scanf return values and print the result with a clear layout (one row per line) so you can inspect values easily.
  • Spelling: the call uses a misspelled function name; this prevents the correct function from being invoked.

Notes for : your display/input parameter types are incorrect for a true 2D array. For an int mat[10][10], the parameter should be declared as int m[][10] or int (*m)[10]. The current int *m[][10] is not the right type and leads to confusing pointer arithmetic. Also avoid printing values with the wrong format string (the printf in addition looks like it prints indices but actually prints element values).

In short: 1) fix loop bounds to use <, 2) use &array[i][j] for scanf, 3) make the add function take rows/cols and the three arrays (in the same order everywhere), 4) use local indices and check scanf returns. These steps remove the common causes seen in and were hinted at by and .

Recommended Answers

All 3 Replies

please use code tags...so that your code is more readable...this way more people can help you....

for c++ code, type (code=c++)//your code goes here(/code)

e.g.

#include<stdio.h>
void AddMatrix(int[2][3],int[2][3],int[2][3],int,int);

int main()
{

    int i,j;
    int matrixA[2][3], matrixB[2][3], matrixC[2][3];
    printf("This Program is to find the summation of matrixA and matrixB\n\n");

    for(i=0;i<=1;i++){
        for(j=0;j<=2;j++){
            printf("Please enter matrixA[%d][%d]:",i+1,j+1);
            scanf("%d", &matrixA[i][j]);
        }
    }

    printf("\n\n");
    for(i=0;i<=1;i++){
        for(j=0;j<=2;j++){
            printf("Please enter matrixB[%d][%d]:",i+1,j+1);
            scanf("%d", &matrixB[i][j]);
        }
    }
    printf("\n\n");
    AddMatirix(matrixA,matrixB,matrixC);
    return 0;
}
void AddMatrix(int matrixC[2][3], int matrixA[2][3], int matrixB[2][3], int i, int j)
{

    for(i=0;i<=1;i++){
        for(j=0;j<=2;j++){

            matrixC[2][3] = matrixA[i][j] + matrixB[i][j];

        }
    }


    for(i=0;i<=1;i++){
        for(j=0;j<=2;j++){
            printf("matrixC is the summation of both matrixA and matrixB: %d", matrixC[i][j]);
        }
    }
    printf("\n\n");
    return;
}

First, check your spelling. In line 26, you call function AddMatirix, which of course does not exist.

Your function is written with two int parameters, i and j, which do not convey any information to the function, as written. Why not delete those parameters, declare loop variables in the function.

In line 35, where you store the sum of each pair of elements of the matrix, you are using fixed indexes into matrixC. It should use the loop variables the same as the A and B matrices, as in:

matrixC[i][j] = matrixA[i][j] + matrixB[i][j];

Also, matrixC[2][3] is an element outside the bounds of your matrix. The last legal element is matrixC[1][2]

In the function header, why do you have the matrices out of order.

void AddMatrix(int matrixC[2][3], int matrixA[2][3], int matrixB[2][3], int i, int j)

What you end up doing is storing to the second source matrix the results of adding the first source and the destination, as seen from the main function. Your display of resulting C matrix is in fact showing you the initial content of the B matrix.

#include <stdio.h>

int i;	int j;	int row;	int column;
int sum[10][10];
int sub[10][10];

void dimension(void)
{
	printf("\n\n\tEnter The Dimensions Of The Matrix : ");
	printf("\n\n\t\tROW    :  ");
	scanf("%d", &row);
	printf("\n\n\t\tCOLUMN :  ");
	scanf("%d", &column);
}

void display(int *m[][10],int r,int c)
{
	for(i = 0;i < r;i++)
	{
		printf("\n\n\t");
		for(j = 0;j < c;j++)
		{
			printf("%d\t",(*(*(m + i) + j)));
		}
	}
}

void input(int *m[][10],int r,int c)
{
	printf("\n\n\tEnter The Elements : \n\n");
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			printf("\t");
			scanf("%d",&(*(*(m + i) + j)));
		}
	}
}

void addition(int m1[][10],int m2[][10],int r ,int c)
{
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			sum[i][j] = m1[i][j] + m2[i][j];
			printf("[%d][%d]\n",m1[i][j],m2[i][j]);
		}
	}
}

void subtraction(int m1[][10],int m2[][10],int r ,int c)
{
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			sub[i][j] = m1[i][j] - m2[i][j];
		}
	}
}

int mat1[10][10];
int mat2[10][10];

int main()
{
	dimension();
	input(mat1,row,column);
	input(mat2,row,column);

	printf("\n\n\tMATRIX 1 : ");
	display(mat1,row,column);
	printf("\n\n\tMATRIX 2 : ");
	display(mat2,row,column);


	addition(mat1,mat2,row,column);
	subtraction(mat1,mat2,row,column);

	printf("\n\n\tRESULT OF ADDITION    : ");
	display(sum,row,column);
	printf("\n\n\tRESULT OF SUBTRACTION : ");
	display(sub,row,column);

	return 0;
}

Tell me wats wrong in my code. Urgent!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.