Affan M N M
Affan M N M

Reputation: 9

please identify and solve the error in this 2D Array Addition using functions in C

Here is my code for adding 2D arrays:

#include <stdio.h>

void getArray(int array[][], int n, int m) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      printf("Enter value for array[%d][%d]: ", i, j);
      scanf("%d", &array[i][j]);
    }
  }
}

void addArray(int array1[][], int array2[][], int result[][],
              int n, int m) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      result[i][j] = array1[i][j] + array2[i][j];
    }
  }
}

void displayArray(int array[][], int n, int m) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  }
}

int main() {
  int array1[10][10], array2[10][10], result[10][10], n, m;
  printf("Enter num of rows and columns: ");
  scanf("%d %d", &n, &m);
  printf("Enter the values for array1:\n");
  getArray(array1, n, m);
  printf("\nEnter the values for array2:\n");
  getArray(array2, n, m);
  addArray(array1, array2, result, n, m);
  printf("\nThe sum of the two arrays is:\n");
  displayArray(result, n, m);

  return 0;
}

I got a different solution for my question but I need to know where I went wrong. These are the errors shown by the compiler:

error: type of formal parameter 1 is incomplete
error: array type has incomplete element type 'int[]'

Upvotes: -1

Views: 61

Answers (1)

chqrlie
chqrlie

Reputation: 144780

The array arguments in your function definitions must have at least all but the first dimension specified either as constants (eg: int array[][10]) or as the value of another function argument passed before them:

void getArray(int n, int m, int array[n][m]) {

Note however that these dimensions must match the actual array definition. So in your case you should use int array[][10] or int array[10][10].

Here is a modified version:

#include <stdio.h>

void getArray(int array[][10], int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("Enter value for array[%d][%d]: ", i, j);
            if (scanf("%d", &array[i][j]) != 1) {
                fprintf(stderr, "invalid input\n");
                return 1;
            }
        }
    }
}

void addArray(int array1[][10], int array2[][10], int result[][10],
              int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            result[i][j] = array1[i][j] + array2[i][j];
        }
    }
}

void displayArray(int array[][10], int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int array1[10][10], array2[10][10], result[10][10], n, m;

    printf("Enter num of rows and columns: ");
    if (scanf("%d %d", &n, &m) != 2) {
        fprintf(stderr, "invalid input\n");
        return 1;
    }
    if (n <= 0 || n > 10 || m <= 0 || m > 10) {
        fprintf(stderr, "invalid sizes\n");
        return 1;
    }
    printf("Enter the values for array1:\n");
    getArray(array1, n, m);
    printf("\nEnter the values for array2:\n");
    getArray(array2, n, m);
    addArray(array1, array2, result, n, m);
    printf("\nThe sum of the two arrays is:\n");
    displayArray(result, n, m);
    return 0;
}

To handle larger sizes, you could allocate the arrays and pass the dimensions before the array pointers:

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

void getArray(int n, int m, int array[n][m]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("Enter value for array[%d][%d]: ", i, j);
            if (scanf("%d", &array[i][j]) != 1) {
                fprintf(stderr, "invalid input\n");
                return 1;
            }
        }
    }
}

void addArray(int n, int m, int array1[n][m], int array2[n][m],
              int result[n][m]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            result[i][j] = array1[i][j] + array2[i][j];
        }
    }
}

void displayArray(int n, int m, int array[n][m]) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int n, m;

    printf("Enter num of rows and columns: ");
    if (scanf("%d %d", &n, &m) != 2) {
        fprintf(stderr, "invalid input\n");
        return 1;
    }
    if (n <= 0 || m <= 0) {
        fprintf(stderr, "invalid sizes\n");
        return 1;
    }
    int (*array1)[m] = malloc(sizeof(*array1) * n);
    int (*array2)[m] = malloc(sizeof(*array2) * n);
    int (*array3)[m] = malloc(sizeof(*array3) * n);
    if (!array1 || !array2 || !array3) {
        fprintf(stderr, "cannot allocate arrays\n");
        return 1;
    }
    printf("Enter the values for array1:\n");
    getArray(n, m, array1);
    printf("\nEnter the values for array2:\n");
    getArray(n, m, array2);
    addArray(n, m, array1, array2, result);
    printf("\nThe sum of the two arrays is:\n");
    displayArray(n, m, result);
    free(array1);
    free(array2);
    free(array3);
    return 0;
}

The syntax int (*array1)[m] = malloc(sizeof(*array1) * n) can be used to allocate an array of n elements of the type pointed to by array1 (namely int[m]). It is possibly more readable to write this instead:

int (*array1)[m] = malloc(sizeof(int[n][m]));

But be aware that the compiler will not flag any inconsistency betweem the int in the destination type and the int in the sizeof expression, should one of these types be changed and not the other. For this reason, the syntax used in the above code is more reliable, albeit less readable and somewhat error prone. To avoid these pitfalls, you could also write:

    int (*array1)[m] = malloc(sizeof(array1[0][0]) * n * m);

Upvotes: 2

Related Questions