1

I am writing a program to print the sum of the elements of a user entered square matrix in the form of a 2D array without using vector. However I am getting 2 errors:

  • Error 1: error:array has incomplete element type 'int []'.

  • Error 2: error: expected expression cout<<sumarr(arr[][]

This is my program:

int sumarr(int arr[][])                 // ERROR 1
{
    // finging no. pf rows(or coloums) of the square matrix
    int n = sizeof(arr) / (2 * sizeof(int));
    int sum = 0;
    for (int i = 0; i < n; i++)         // calculating sum of elements
    {
        for (int j = 0; j < n; j++)
        {
            sum += arr[i][j];
        }
    }
}
int main()
{
    int n;                            // No. of rows(or coloumns) of the square matrix
    cin >> n;
    int arr[n][n];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)   // inputting array elements
        {
            cin >> arr[i][j];
        }
    }
    cout << sumarr(arr[][]);          // ERROR 2
    return 0;
}

Can someone suggest why I am getting these errors and how to resolve them?

anastaciu
  • 20,013
  • 7
  • 23
  • 43
atmish01
  • 19
  • 1

1 Answers1

1

In the function parameter (Error 1) you'll need to specify at least the last dimension of the array:

int sumarr(int arr[][SIZE]){ /*...*/}

In the call to the function you need to use the array name only, no dereferencing necessary (Error 2):

cout << sumarr(arr);

The problem then becomes the fact that Variable Length Arrays are not allowed in C++, so you probably should rethink the possibility of using vectors for this task.

Another thing worth mentioning is that sizeof(arr) inside the function will not render you the size of the array but the size of the pointer, which is what arr becomes when passed as an argument.

Alternatively you can manually allocate memory for the array, it could look more or less like this:

Live sample

#include <iostream>
#include <cassert>
// since sizeof arr can't work you should pass the size as argument
int sumarr(int **arr, int n) 
{
    assert(n > 0 && n < 1000); // confirm that n is valid, > 0 and a suitable upper limit
    
    int sum = 0;
    for (int i = 0; i < n; i++) //calculating sum of elements
    {
        for (int j = 0; j < n; j++)
        {
            // input and sum in the same loop will save you a O(N^2) operation
            std::cin >> arr[i][j];
            sum += arr[i][j];
        }       
    }
    return sum;
}
int main()
{
    int n; //No. of rows(or columns) of the square matrix
    std::cin >> n;

    // memory allocation for 2D array
    int **arr = new int *[n];
    for (int i = 0; i < n; i++)
    {
        arr[i] = new int[n];
    }
    //end
    
    int sum = sumarr(arr, n); // passing array and size
    std::cout << "Sum: " << sum;
     
    // freeing memory after use
    for (int i = 0; i < n; i++)
    {
        delete [] arr[i];
    }
    delete [] arr;
    //end
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
anastaciu
  • 20,013
  • 7
  • 23
  • 43