I am studying C programming now, I am not getting correct output when I am adding two elements in array, i am expecting your help to know the issue.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int limit = 0, sum[limit][3];
int a[limit][3];
int b[limit][3];
printf("Enter the size of arrays: \n");
fflush(stdout);
scanf("%d", &limit);
printf("Enter the values of Array 1");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Array 1: \n");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
printf("Enter the values of Array 2");
fflush(stdout);
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
scanf("%d", &b[i][j]);
}
}
printf("Array 2: \n");
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
printf("%d\t", b[i][j]);
}
printf("\n");
}
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
sum[i][j]= a[i][j] + b[i][j];
}
}
printf("Sum of 2 arrays: \n");
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return EXIT_SUCCESS;
}
Current output:
Enter the size of arrays:
3
Enter the values of Array 1
12
12
12
12
12
12
12
12
12
Array 1:
12 12 12
12 12 12
12 12 12
Enter the values of Array 2
11
11
11
11
11
11
11
11
11
Array 2:
11 11 11
11 11 11
11 11 11
Sum of 2 arrays:
22 22 22
22 22 22
22 22 22
The problem i found that the exact array i gave in code that doesn't workout. My expected output is:
23 23 23
23 23 23
23 23 23
limittimes, no wonder something goes wrong. Additionally, no wonder something goes wrong whenlimitis 0 when your sizing your arrays.