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;
}
}
|