1

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
2
  • 1
    If your 2nd dimension is constant "3", and you're looping over this 3 items limit times, no wonder something goes wrong. Additionally, no wonder something goes wrong when limit is 0 when your sizing your arrays. Commented Oct 7, 2020 at 8:13
  • Turn on your compiler warnings. Commented Oct 7, 2020 at 8:22

3 Answers 3

3

In:

int limit = 0, sum[limit][3];
int a[limit][3];
int b[limit][3];

limit is 0, your arrays will be:

int sum [0][3];
int a[0][3];
int b[0][3];

They will have space for nothing.

You should declare your arrays only after the limit input.

Also note that the second dimension of the array is fixed at 3, in your inner for cycles, instead of using limit you should use that constant value, otherwise, if limit is 4 or more, your program will access the array outside its bounds, invoking undefined behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! got expected output.
1

There are multiple problems:

  • the arrays are defined with a dimension set to 0:

      int limit = 0, sum[limit][3];
      int a[limit][3];
      int b[limit][3];
    

you should define the arrays after you read the value og limit.

  • the nested loops use an incorrect boundary test:

      for (int i = 0; i < limit; i++) {
          for (int j = 0; j < limit; j++) {
    

j should iterate from 0 to 3 excluded.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int limit = 0;

    printf("Enter the size of arrays: \n");
    fflush(stdout);
    if (scanf("%d", &limit) != 1 || limit < 1)
        return 1;

    int sum[limit][3];
    int a[limit][3];
    int b[limit][3];

    printf("Enter the values of Array 1");
    fflush(stdout);
    for (int i = 0; i < limit; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("Array 1: \n");
    fflush(stdout);
    for (int i = 0; i < limit; i++) {
        for (int j = 0; j < 3; 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 < 3; j++) {
            scanf("%d", &b[i][j]);
        }
    }
    printf("Array 2: \n");
    for (int i = 0; i < limit; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d\t", b[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < limit; i++) {
        for (int j = 0; j < 3; 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 < 3; j++) {
            printf("%d\t", sum[i][j]);
        }
        printf("\n");
    }

    return EXIT_SUCCESS;
}

Comments

1

In the inner nested loops,j should iterate till j(excluding it) not limit. Required nested loop should be like-

for (int i = 0; i < limit; i++) {
      for (int j = 0; j < 3; j++) {
.
.
}

Tip:Declare your array after getting the input/size of the array from the user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.