User defined array size?

Jan 7, 2013 at 4:18pm
So I want to create an array size depending on what the user specifys. I have tried this in visual studio 2010 and it tells me it has to be a constant value. How do I let a user input and define a constant ? :(
Jan 7, 2013 at 4:26pm
Use a vector, as in, std::vector<int> v(size); (or other type instead of int)
Jan 7, 2013 at 4:27pm
When you declare array like this:

int myArray[10];

the compiler needs to know at compile time what the size of the array is, because it is going to set a block of memory in the local store aside for you to use.

To create arrays dynamically, you need to create memory on the free store, and you do this using the "new" keyword in c++

int* myArray = 0;
myArray = new int[10];

This creates an integer pointer called myarray, and sets the pointer address to the start of the array that the new keyword created.

For this to occur, you need to also free the memory using the keyword delete.
Jan 7, 2013 at 4:29pm
If you create the array with new the size don't have to be a compile time constant but I recommend that you use a vector instead.
http://www.cplusplus.com/reference/vector/vector/
Jan 7, 2013 at 4:30pm
It is C that allows to use VLA.
Jan 7, 2013 at 4:45pm
I tried using new and delete to create memory but you can't with a multidimensional 2d array :(

I want the user to be able to choose from 3 board sizes. 2x2, 5x5 and 10x10. I want array to be the size of boardsize variable.

sorry I'm a beginner :(
Jan 7, 2013 at 4:50pm
yes you can. You would need to define a double pointer, or a pointer to a pointer:

int** myArray = 0;

then create memory:

myArray = new int*[10];
for(iunsigned int x = 0; x <10; x++)
mtArray[x] = new int[10];

to delete this you do the opposite:

for(unsigned int x = 0; x < 10; x++)
delete[] myArray[x];

delete[] myArray;

Jan 7, 2013 at 4:50pm
try this:


1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>

int main()
{
	int size;
	
	std::cin >> size;
	int *A = new int[size];

	/*your code here*/

	delete[] A;
}
Last edited on Jan 7, 2013 at 4:51pm
Jan 7, 2013 at 4:55pm
closed account (D80DSL3A)
Another option is to declare a static 10x10 array then use only the size x size portion of it that you need.
This is practical because the upper limit for the array size is small.
Jan 7, 2013 at 5:04pm
If you have predetermined sizes for your boards, one option (definitely not the best, but it'll get the job done) is to have three different functions which create your arrays based on the size the user wants. Then, have all of the user's interaction with the program take place in whichever function they chose, going back to main only to exit.

For example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <iostream>

void board2();
void board5();
void board10();

int main()
{
    int choice;
    std::cin >> choice;
   
    if (choice == 2) 
    {
          board2();
    }

    else if (choice == 5)
    {
          board5();
    }

    else if (choice == 10)
    {
          board10();
    }

    return 0;

}

void board2()
{
    int board[2][2];
    //rest of code
}

void board5()
{
    int board[5][5];
    //rest of code
}

void board10()
{
    int board[10][10];
    //rest of code
}


This definitely isn't your best option, but like I said, it would get the job done, if you can't get anything else to work for a multidimensional array.
Last edited on Jan 7, 2013 at 5:05pm
Topic archived. No new replies allowed.