0
N-dimensional array
Is there a way that I can input the number n and the function (like some) will create n-dimensional array? F.e.: if I input 3 it will create int array[] [] [].
8 Respostas
+ 5
You could try something like this:
https://code.sololearn.com/cQCGKo7DXZBA/#cpp
#include <iostream>
using namespace std;
int main() {
int arrSize;
cout << "What size do you want array?\n";
cin >> arrSize;
int *myArr = new int[arrSize];
for(int i = 0; i < arrSize; ++i)
{
myArr[i] = (i+2)*4; // diff num than index for illustration purposes
cout << "myArr[" << i << "] is " << myArr[i] << endl;
}
return 0;
}
:::: INTPUT :::::
7
:::: OUTPUT ::::
What size do you want array?
myArr[0] is 8
myArr[1] is 12
myArr[2] is 16
myArr[3] is 20
myArr[4] is 24
myArr[5] is 28
myArr[6] is 32
PS - I agree with Aaron. Vectors is better method.
+ 4
Arrays usually have a fixed size at compile time so changing their size depending on user input doesn't work (though some compiler might allow this). The best way to implement this would be to use vectors.
+ 2
@Bennett Post
You're absolutely right. I can't read today. lol :D
+ 2
Very good explanation, that is exactly the method my friend recommended, but as you mentioned, might get tricky with large N's.
0
You see, I was trying to write some Program that could write out all the variations with repetitions, given the elements f.e.: {1,2,3} and n spaces, if n was 4 it would go from 1111,1112,1113.....3331,3332,3333.