0

I keep getting "error invalid types 'int int ' for array subscript"

This is my code, any help is appreciated. #include <iostream> using namespace std; int i; int j; int k; int m; int n; int b; void product(int array[], int x=1) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { for(k=0;k<b;k++) { x=x*array[i][j][k]; } } } cout<<"Product: "<<x<<endl; } void suma(int array[], int y=0) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { for(k=0;k<b;k++) { y=y+array[i][j][k]; } } } cout<<"Sum: "<<y<<endl; } void numberElements(int array[], int z=0) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { for(k=0;k<b;k++) { z=z+1; } } } cout<<"Total number of elements: "<<z<<endl; } int main() { int i, j, k, m, n, b, temp; int arr[m][n][b]; cout<<"Array dimensions:"<<endl; cin>>m>>n>>b; for(i=0;i<m;i++) { for(j=0;j<n;j++) { for(k=0;k<b;k++) { cout<<"Insert value on ["<<i<<"]["<<j<<"]["<<k<<"] "; cin>>arr[i][j][k]; } } } cout<<endl; cout<<"Array:"<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { for(k=0;k<b;k++) { cout<<arr[i][j][k]; } cout<<endl; } cout<<endl; } product(arr); sum(arr); numberElements(arr); return 0; }

25th Mar 2020, 10:05 AM
Zadnipro Ion
1 ответ
+ 1
1. You are passing a three-dimensional array to the functions, which in turn only expect a one-dimensional array. Passing multi-dimensional arrays is more complex: https://stackoverflow.com/questions/13326021/how-to-pass-a-3d-array-as-a-parameter-to-function-c-also-do-global-variables 2. You are trying to set up the array before retrieving its dimensions, while the variables are still not initialized. 3. Variable-lenght arrays are anyway not standard C++ and only supported through compiler extensions. If you don't know the dimensions at compile time, you technically would have to allocate the array by yourself. 4. Using std::vector from the Standard Template Library could simplify the program by a lot: https://en.cppreference.com/w/cpp/container/vector
25th Mar 2020, 10:50 AM
Shadow
Shadow - avatar