-1

So, I need to create a square matrix with a size entered by user (also filled with random numbers). I tried this:

#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int size;
cin >> size;
int arr[] = {};
for (int i = 0; i < size; i++){
    cout << endl;
    for (int j = 0; j < size; j++){
        arr[i][j] = rand() % 10;
        cout << arr[i][j] << ' ';

    }
} 
return 0;
}

However, every time my g++ output is :

6.cpp: In function ‘int main()’:
6.cpp:12:21: error: invalid types ‘int[int]’ for array subscript
         arr[i][j] = rand() % 10;
                 ^
6.cpp:13:29: error: invalid types ‘int[int]’ for array subscript
         cout << arr[i][j] << ' ';

Where have I messed up?

Max Popov
  • 347
  • 5
  • 15
  • `int arr[] = {};` I'm not even sure this is valid, but regardles off that indexing `arr` will return an int, you're trying to index the int. This cannot work. – Borgleader May 08 '18 at 17:12
  • Arrays have a fixed size. How many ints are in this array? `int arr[] = {}` – Drew Dormann May 08 '18 at 17:12
  • well, the number of ints has to be entered by user. Therefore, I have declared "size" – Max Popov May 08 '18 at 17:16
  • My suggestion is to implement [a simple matrix class based around a `std::vector`](https://stackoverflow.com/a/43552983/4581301). If you ae not allowed to use `std::vector` for some reason, [it gets trickier](https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op). Note the declared-but-unimplemented copy constructor and assignment operator in the linked code. This is [the secret sauce to making an easy-to-use matrix around a pointer](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). `std::vector` does all this for you. – user4581301 May 08 '18 at 17:53

2 Answers2

0

First off you are using a 1 dimensional array like a 2 dimensional one. This is wrong.

Assuming you fix that, If this had been C99 and not C++, variable length arrays are actually okay. You can get away with something like

/*
   get input size
*/
int arr[size][size];

But in C++ (un)fortunately there are no variable length arrays in the same style.

In classic C++ you would have had to use memory allocation to create dynamic arrays like this with sizes that are determined at runtime

int size;
cin >> size;
int **arr = new int*[size];
for(int i = 0; i < size; i++) {
    arr[i] = new int[size];
}

However in more modern C++ SO typically advises you to use vectors instead

vector< vector< int> > arr;

and instead of arr[i][j] = rand() % 10; use arr[i].push_back(rand() %10)

or you could resize before putting stuff the vectors. OR you could use reserve if you don't want to put placeholder junk in the vector.

OR you could pass size to the vector's constructor

vector< vector < int > > arr(size, vector < int > (size));

A caveat though, for small matrices vector of vectors has noticeably worse performance than plain old array. (I did not personally measure the metrics, but I have seen reliable metric for this on SO)

Srini
  • 1,619
  • 1
  • 19
  • 34
-1
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int main(){
int size;
cin >> size;

std::vector<std::vector<int> > arr(size,std::vector<int>(size,0));
for (int i = 0; i < size; i++){
    cout << endl;
    for (int j = 0; j < size; j++){
        arr[i][j] = rand() % 10;
        cout << arr[i][j] << ' ';

    }
} 
return 0;
}
mystic_coder
  • 462
  • 2
  • 10