How to test if function works

I'm new to this forum and just started taking computer software classes. Could someone please help me with trying to figure out the best way to test if my function works in main with cout with something like this
int result1 = printkey(grid)????
cout << "printKey9grid(??????) is " << result1 << endl;
not sure how to write this part to show that ROWS and COLS are the same but two separate parameters.


const int ROWS = 12;
const int COLS = 10;

void printKey(const char grid[ROWS][COLS]){
int i ,j;
for (i = 0; i < COLS; i++){
for (j = 0; i < ROWS; i++){
if(grid [i][j] == ' '){
cout << "_";
}
else{
cout << grid[i][j];
}
}
}
}

here's what ROWS and COLS needs to print it's a work search that I'm doing for my class. Thanks for any help.
{{'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',},
{'E', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',},
{'A', ' ', 'D', ' ', ' ', ' ', ' ', ' ', ' ', ' ',},
{'P', 'R', 'O', 'G', 'R', 'A', 'M', ' ', ' ', ' ',},
{' ', ' ', 'U', ' ', ' ', ' ', ' ', ' ', ' ', ' ',},
{' ', ' ', 'B', ' ', ' ', ' ', 'M', ' ', ' ', ' ',},
{' ', ' ', 'L', ' ', ' ', ' ', 'A', ' ', ' ', ' ',},
{' ', ' ', 'E', ' ', 'V', 'O', 'I', 'D', ' ', ' ',},
{' ', ' ', ' ', ' ', ' ', ' ', 'N', ' ', ' ', ' ',},
{'S', 'T', 'A', 'C', 'K', ' ', ' ', ' ', ' ', ' ',},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',}
};
The return type of printKey is void, so it doesn't have a return value that should be used. You're printing (to cout) within the function itself.

Just do:
printKey(grid); (assuming grid is defined as an 2d array of the correct size).

Also, please format your code with [code] and [/code] tags.
Last edited on
Hello geek4coding,

Some small things that might help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
constexpr int ROWS = 12;
constexpr int COLS = 10;

void printKey(const char grid[ROWS][COLS])
{
    int i, j;  // <--- Best to define "i" and "j" in the for loops.

    for (i = 0; i < COLS; i++)  // <--- for (int i = 0; i < COLS; i++). Also the outer loop should deal wit "ROWS".
    {
        for (j = 0; i < ROWS; i++)
        {
            if (grid[i][j] == ' ')
            {
                cout << "_";
            }
            else
            {
                cout << grid[i][j];
            }
        }
    }
}


And this is easier to follow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void printKey(const char grid[ROWS][COLS])
{
    for (int row = 0; row < ROWS; row++)
    {
        for (int col = 0; col < COLS; col++)
        {
            if (grid[row][col] == ' ')
            {
                cout << "_";
            }
            else
            {
                cout << grid[row][col];
            }
        }
    }
}

Using "i", "j" and "k" in for loops is fine, but sometimes a better name makes a big difference.

And when you ger to initializing the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char grid[ROWS][COLS]
{ 
    { 'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
    { 'E', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
    { 'A', ' ', 'D', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
    { 'P', 'R', 'O', 'G', 'R', 'A', 'M', ' ', ' ', ' ', },
    { ' ', ' ', 'U', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
    { ' ', ' ', 'B', ' ', ' ', ' ', 'M', ' ', ' ', ' ', },
    { ' ', ' ', 'L', ' ', ' ', ' ', 'A', ' ', ' ', ' ', },
    { ' ', ' ', 'E', ' ', 'V', 'O', 'I', 'D', ' ', ' ', },
    { ' ', ' ', ' ', ' ', ' ', ' ', 'N', ' ', ' ', ' ', },
    { 'S', 'T', 'A', 'C', 'K', ' ', ' ', ' ', ' ', ' ', },
    { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
    { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', }
};

As you can see this allows each column of each row to line up which makes it easier to work with.

Andy
Perhaps as:

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
#include <iostream>

const int ROWS {12};
const int COLS {10};
using Grid = char[ROWS][COLS];

std::ostream& operator<<(std::ostream& os, const Grid& grid)
{
	for (int row = 0; row < ROWS; ++row) {
		for (int col = 0; col < COLS; ++col)
			os << (grid[row][col] == ' ' ? '_' : grid[row][col]);

		os << '\n';
	}

	return os;
}

int main()
{
	const Grid grid
	{
		{ 'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
		{ 'E', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
		{ 'A', ' ', 'D', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
		{ 'P', 'R', 'O', 'G', 'R', 'A', 'M', ' ', ' ', ' ', },
		{ ' ', ' ', 'U', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
		{ ' ', ' ', 'B', ' ', ' ', ' ', 'M', ' ', ' ', ' ', },
		{ ' ', ' ', 'L', ' ', ' ', ' ', 'A', ' ', ' ', ' ', },
		{ ' ', ' ', 'E', ' ', 'V', 'O', 'I', 'D', ' ', ' ', },
		{ ' ', ' ', ' ', ' ', ' ', ' ', 'N', ' ', ' ', ' ', },
		{ 'S', 'T', 'A', 'C', 'K', ' ', ' ', ' ', ' ', ' ', },
		{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', },
		{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', }
	};

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



H_________
E_________
A_D_______
PROGRAM___
__U_______
__B___M___
__L___A___
__E_VOID__
______N___
STACK_____
__________
__________

Last edited on
Topic archived. No new replies allowed.