Making a grid using nested for-loops

Hello everyone! For a project, I have to make a simple C++ program that makes a grid, but I cannot use arrays for this project; I need to use nested for loops instead. In addition, I have to prompt the user to enter a start position, an end position, and a step size which takes that same grid and puts "**" over the squares that aren't needed if that makes any sense.

Here's a sample execution:

Enter the board size (rows columns): 4 4

00 01 02 03
04 05 06 07
08 09 10 11
12 13 14 15

Enter the start position: 1
Enter the end position: 15
Enter the step size: 5

** 01 ** **
** ** 06 **
** ** ** 11
** ** ** ** 


For the most part, the program is finished, but I am having trouble with printing the final grid. I was hoping somebody could point me in the right direction. Here's the code that I have now:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <iomanip>
using namespace std;

void getBoardSize(int& rows, int& columns);
void printInitialBoard(int r, int c);
int getStartPosition();
int getEndPosition();
int getStepSize();
void printFinalBoard(int rows, int columns, int startPos, int endPos, int stepSize);

int main()
{
	int rows = 0;
	int columns = 0;

	getBoardSize(rows, columns);
	printInitialBoard(rows, columns);
	int startPos = getStartPosition();
	int endPos = getEndPosition();
	int stepSize = getStepSize();
	printFinalBoard(rows, columns, startPos, endPos, stepSize);

        return 0;
}

void getBoardSize(int& rows, int& columns)
{
	cout << "Enter the board size (rows columns): ";
	cin >> rows >> columns;
	cout << endl;
	while(rows > 8 || columns > 8)
	{
		cout << "The board cannot exceed 8x8. Try again: ";
		cin >> rows >> columns;
		cout << " " << endl;
	}
}

void printInitialBoard(int rows, int columns)
{
	for(int r = 0; r < rows; r++)
	{
		for(int c = 0; c < columns; c++)
		{
			cout << setw(2) << setfill('0') << (r * columns) + c;
			cout << " ";
		}
		cout << endl;
	}
}

int getStartPosition()
{
	int startPos;

	cout << "\nEnter the start position: ";
	cin >> startPos;
	while(startPos < 0)
	{
		cout << "The start position must be non-negative, enter again: ";
		cin >> startPos;
	}

	return startPos;
}

int getEndPosition()
{
	int endPos;

	cout << "Enter the end position: ";
	cin >> endPos;

	return endPos;
}

int getStepSize()
{
	int stepSize;

	cout << "Enter the step size: ";
	cin >> stepSize;
	cout << endl;
	while(stepSize <= 0)
	{
		cout << "The step size must be positive, enter again: ";
		cin >> stepSize;
	}
        
        return stepSize;
}

void printFinalBoard(int rows, int columns, int startPos, int endPos, int stepSize)
{
	for(int r = 0; r < rows; r++)
	{
		for(int c = 0; c < columns; c++)
		{
			cout << setw(2) << setfill('0') << (r * columns) + c;
			cout << " ";
		}
		cout << endl;
	}
}


Finally, here's my output (using different step size and positions):

Enter the board size (rows columns): 4 4

00 01 02 03
04 05 06 07
08 09 10 11
12 13 14 15

Enter the start position: 00
Enter the end position: 15
Enter the step size: 2

00 01 02 03
04 05 06 07
08 09 10 11
12 13 14 15


As you can see, I temporarily used the exact same code in the "printFinalBoard" function that was used to print the initial board. I know that the code is supposed to be similar, but I am having trouble covering up the squares after the user has entered their desired numbers.
Last edited on
1
2
3
4
5
value = r*columns + c
if value<start or value>end:
   print('***')
else:
   print(value)
¿can you figure out the step part?
Thank you for the response! I got that to work, but the step part is what I had the most difficulty with. Here's another execution using different numbers:

Enter the board size (rows columns): 8 8

00 01 02 03 04 05 06 07
08 09 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

Enter the start position: 10
Enter the end position: 42
Enter the step size: 4

** ** ** ** ** ** ** **
** ** 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 ** ** ** ** **
** ** ** ** ** ** ** **
** ** ** ** ** ** ** **
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void printFinalBoard(int rows, int columns, int startPos, int endPos, int stepSize)
{
    for(int r = 0; r < rows; r++)
    {
        for(int c = 0; c < columns; c++)
        {
            int pos = r * columns + c;
            cout << setw(2) << setfill('0');

            if ( pos >= startPos && pos <= endPos && ( pos - startPos ) % stepSize == 0 ) cout << pos;
            else                                                                          cout << "**";

            cout << " ";
        }
        cout << endl;
    }
}


Enter the board size (rows columns): 8 8

00 01 02 03 04 05 06 07 
08 09 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 

Enter the start position: 10
Enter the end position: 42
Enter the step size: 4
** ** ** ** ** ** ** ** 
** ** 10 ** ** ** 14 ** 
** ** 18 ** ** ** 22 ** 
** ** 26 ** ** ** 30 ** 
** ** 34 ** ** ** 38 ** 
** ** 42 ** ** ** ** ** 
** ** ** ** ** ** ** ** 
** ** ** ** ** ** ** ** 
Last edited on
Thank you both for the help!
Topic archived. No new replies allowed.