Passing Enum Through 2-D Array

I am trying to pass a 2d array with an enum and call it. I am not sure How I do it though. Here is what I have.

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

#include<iostream>
using namespace std;

const int ROWS = 5;
const int COLS = 5;
enum SYMBOLS {AT = '@', STAR = '*', X = 'X', ZERO = 0, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};

void putStar(int initialSymbol[ROWS][COLS], int size); // I don't know if I need size.
	
int main()
{
	SYMBOLS symbolArr[ROWS][COLS];
		
	putStar(symbolArr); // How Do I call it

	return 0;
}
void putStar(int initialSymbol[ROWS][COLS], int size)
{
	//creating the rows for board
	for (int i = 0; i < ROWS; i++)
	{
		//creating the column for the rows.
		for (int j = 0; j < COLS; j++)
		{
			//Assign Enum '*' to The Board. How Do I do that?
			 
			
		}
	}
//Parameter are the two dimensional array and size	

	return;
}


What I am trying to do is call the STAR function as an enum and fill a five by five.
To be more specific I am trying to:
"You will initialize the board using a function, passing in the two-dimensional array and
the size. This function will simply initialize each position on the board to the enumerated
type representing the initial value, which is the character ‘*’."

so it should look like this.

*****
*****
*****
*****
*****

PS: does the preview button work for anyone here? It never seems to work for me.
Last edited on
//creating the column for the rows.
for (int j = 0; j < COLS; j++)
{
//Assign Enum '*' to The Board. How Do I do that?


initialSymbol[i][j] = STAR;



however because your array isn't explicitly type char you will probably get an array full of (int)('*') values, that is, the ascii integer value of '*'. I would advise making the array of type char, or you can cast it carefully when you go to print it.



Last edited on
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
#include<iostream>

const int ROWS = 5;
const int COLS = 5;

// ZERO = '0': probably a good idea not to mix characters and integers
enum symbol : char { AT = '@', STAR = '*', X = 'X', ZERO = '0', ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};

// 'array_type' is a succinct name (an alias) for 'two-dimensional array of ROWSxCOLS symbols'
using array_type = symbol[ROWS][COLS] ;

void fill( array_type& symbolArr, symbol sym ) // fill the array with symbol 'sym'
{
    for( auto& row : symbolArr ) // for each row in the array
        for( symbol& s : row ) s = sym ; // assign 'sym' to each symbol in the row
}

void putStar( array_type& symbolArr ) { fill( symbolArr, STAR ) ; }

void print( const array_type& symbolArr )
{
    for( const auto& row : symbolArr ) // for each row in the array
    {
        for( symbol s : row ) std::cout << char(s) ; // print each symbol in the row as a character
        std::cout << '\n' ; // and then, a new line
    }
}

int main()
{
	symbol symbolArr[ROWS][COLS] ;

	putStar(symbolArr);
	print(symbolArr) ;

	std::cout << '\n' ;
	fill( symbolArr, SEVEN ) ;
	print(symbolArr) ;
}

http://coliru.stacked-crooked.com/a/1e5f88be93e818ac
Topic archived. No new replies allowed.