-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?

4
  • 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. Commented May 8, 2018 at 17:12
  • Arrays have a fixed size. How many ints are in this array? int arr[] = {} Commented May 8, 2018 at 17:12
  • well, the number of ints has to be entered by user. Therefore, I have declared "size" Commented May 8, 2018 at 17:16
  • My suggestion is to implement a simple matrix class based around a std::vector. If you ae not allowed to use std::vector for some reason, it gets trickier. 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. std::vector does all this for you. Commented May 8, 2018 at 17:53

2 Answers 2

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)

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

Comments

-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;
}

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.