Dynamic arrays

Hi all

I want to create a multidimensional array where the user can can change it. First I need to know how to make a grid. Something like this:

A b c d e f
1 x x x x 0 x
2 o x x x x x
3 x x o o o x
4 o o o o x x
5 x o x x o o

Where the user can choose an option like e3 and change the o to an x. How do I do that

Still very new to programming so need it explained simply

Thanks in advance
Last edited on
If you want it to be dynamically allocated, it will be a bit hard.
There are no a, b, c... coordinates, so you'll have to translate them to the desired number contained in [0 ... n). (Counting starts at zero, and ends at n-1)

  0 1 2 3
0 x x x x
1 x x x x
2 x x x B
3 x x x x


B is at [2, 3].

Some links may help.
http://www.cplusplus.com/forum/articles/7459/
http://www.cplusplus.com/forum/beginner/63/

If they don't, post a reply saying where you're stuck.

Edit: removed some trivial things that OP already knew.
Last edited on
Google C++ 2d array to read all about it
Hi Catfish2

I made a mistake in my title, i meant multidimensional arrays, not dynamic.

Thanks

This is what i have done so far:

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
#include <iostream>
using namespace std;

int main(){
	char grid[6][6] = {
        {' ','A','B','C','D','E'},
        {'1','O','O','O','O','O'},
        {'2','O','O','O','O','O'},
        {'3','O','O','O','O','O'},
        {'4','O','O','O','O','O'},
        {'5','O','O','O','O','O'}};
	char character = 'X';
    
	int position[6] = {6,6};
    
	// Draw the grid once
	for (int i =0; i < 6; i ++){
		for (int j =0; j < 6; j++){
			if (i == position[4] && j == position[4])
				cout << character;
			else
				cout << grid[i][j];
			cout << " ";
		}
		cout << endl;
	}
    
	return 0;
}


How can i get the user to decide where x Goes on this grid?
Last edited on
Ask the user?
http://www.cplusplus.com/reference/iostream/cin/

Hint: If you plan on displaying the board more than once, you might want to put it in a function.

It will go into a function at some point, but need to get it working. I know about cin but I need to get the user to put the x on the grid anywhere they choose. So A5 or c3. How do I get them to choose the poistion?
If you think you can just get the user to click on the grid section to choose it, you're going to be disappointed.

You will ask the user, and they will type it in.
std::cin will get their input. And then you'll translate it into the actual coordinates.
I already wrote that a, b, c, etc. are not valid coordinates.
In your case, it just so happens that the number coordinates are valid, but you'll have to substitute the letters with their corresponding numbers, e.g. in your case: A is 1, B is 2, ... E is 5.

obviously i know you cant click on the area and change it, and you have to use coordinates. so i get that part..so can i have an example code of how they can enter in these coordinates. and the x will move

thanks for your help
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <vector>

void displayGrid(const std::vector<std::vector<char> > &grid)
{
    std::size_t widthMarkings = 0, heightMarkings = 0;

    std::cout << '\t';

    while (widthMarkings < grid.front().size())
        std::cout << widthMarkings++ << ' ';

    std::cout << '\n';

    for (std::vector<std::vector<char> >::const_iterator cit1 = grid.begin();
        cit1 != grid.end();
        ++cit1)
    {
        std::cout << heightMarkings++ << '\t';

        for (std::vector<char>::const_iterator cit2 = (*cit1).begin();
            cit2 != (*cit1).end();
            ++cit2
            )
            std::cout << *cit2 << ' ';

        std::cout << '\n';
    }

    std::cout << std::endl;
}

int main()
{
    std::size_t height = 0, width = 0;
    const char defaultFiller = '.';
    const char defaultMarker = 'X';

    std::cout << "Enter the height of the grid: ";
    std::cin >> height;
    std::cout << "Enter the width of the grid: ";
    std::cin >> width;

    std::vector<std::vector<char> > grid;

    grid.resize(height);

    for (std::vector<std::vector<char> >::iterator it = grid.begin();
        it != grid.end();
        ++it)
    {
        (*it).resize(width);
        std::fill((*it).begin(), (*it).end(), defaultFiller);
    }

    displayGrid(grid);

    while (true)
    {
        std::cout << "Hit Control-C to quit!\n---------------------\n";
        std::cout << "Enter the height coordinate of " << defaultMarker
            << " (0 to " << grid.size() - 1 << "): ";
        std::cin >> height;
        std::cout << "Enter the width coordinate of " << defaultMarker
            << " (0 to " << grid.front().size() - 1 << "): ";
        std::cin >> width;
        grid.at(height).at(width) = defaultMarker;
        displayGrid(grid);
    }
}

Topic archived. No new replies allowed.