'''
#include<stdio.h>
int main()
{
int n,m;
scanf("%d %d", &n, &m);
int *a1 = (int*) malloc((m*n)*sizeof(int));
if (a1==NULL)
{
printf("a");
exit(0);
}
else
{
int a2;
int cnt=0;
int k;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d", &k);
a1[cnt] = k;
cnt++;
}
}
cnt=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d", &a2);
printf("%d ", (a1[cnt])+a2);
cnt++;
}
printf("\n");
}
}
return 0;
}
THIS IS A SIMPLE MATRIX ADDITION CODE FOR MATRIX WITH N ROWS AND M COLUMNS. I'm stuck while using pointers. The code does not take the no. of inputs it should. I cannot find my mistake. The expected output is a matrix, which is the addition of 2 matrices in input. Sample Input:
2 3
1 3 4
2 2 2
1 1 1
1 1 1
Sample Output:
//no output
Process returned -1073741819 (0xC0000005) execution time : 4.427 s
The only program behavior I see that I can correlate with the claim that "The code does not take the no. of inputs it should" is that when run interactively, it is prone to interspersing its outputs with its inputs. For instance, if you run the program interactively and enter data in the form shown in the question, then the program will print the first row of the result after you enter the first row of the second matrix, before you enter the second. But at that point it is still waiting for the second row of the second matrix, and when you enter it, the program will output the second row of the result, so ultimately it does take the number of inputs it should.
That behavior directly reflects your code, in which you compute each element of the result matrix immediately after reading the corresponding element of the second matrix, and print that result element right away:
scanf("%d", &a2); printf("%d ", (a1[cnt])+a2);
That also interacts with line buffering by the terminal in which you run the program. It sends data a line at a time, after you press the <enter> key, so if you enter elements a row at a time then that's also the way you will get the results.
I would suggest storing the computed result in memory until you have computed all the elements, then printing it all. For instance, you could update the elements of a1
by changing the above code to this:
scanf("%d", &a2);
a1[cnt] += a2;
, and removing the printf("\n");
in its outer loop.
Then write a third, separate loop nest to print out the final values stored in a1
.
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n,m;
printf("Enter size of matrices: ");
scanf("%d %d", &n, &m);
int *a1 = (int*) malloc((m*n)*sizeof(int));
int *a2 = (int*) malloc((m*n)*sizeof(int));
int *result = (int*) malloc((m*n)*sizeof(int));
if (a1==NULL)
{
printf("a");
return -1;
}
if (a2==NULL)
{
printf("a2");
free(a1);
return -1;
}
if (result==NULL)
{
printf("a3");
free(a1);
free(a2);
return -1;
}
else
{
int k;
// Taking inputs
printf("Enter matrix 1: ");
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d", &k);
a1[i*m+j] = k;
}
}
// Taking inputs
printf("Enter matrix 2: ");
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d", &k);
a2[i*m+j] = k;
}
}
// Calculate and display result
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
result[i*m+j] = a1[i*m+j] + a2[i*m+j];
printf("%d ", result[i*m+j]);
}
// Print newline after each row
printf("\n");
}
}
}
User contributions licensed under CC BY-SA 3.0