I try to put 4 symbols on a user-sized board. Firstly, it works for `n x n` but when I want to convert `m x n` I can't get expected result. It works for some tryings but not for all. For example:
Original algorithm:
Code:
table[center][center - 1] = p_symbol;
table[center - 1][center] = p_symbol;
table[center][center] = c_symbol;
table[center - 1][center - 1] = c_symbol;
Code:
Expected result:
row = 4, col = 8
abcdefgh
1........
2...xo...
3...ox...
4........
Output result:
abcdefgh
1........
2........
3...xo...
4...ox...
Expected result:
row = 12, col = 6
abcdef
1......
2......
3......
4......
5......
6..xo..
7..ox..
8......
9......
10......
11......
12......
Output result:
abcdef
1......
2......
3......
4......
5......
6....xo
7....ox
8......
9......
10......
11......
12......
Code:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
char const c_symbol = 'x';
char const p_symbol = 'o';
char const what_symbol = '.';
void show(char const table[][100], short const row_size, short const col_size)
{
cout << endl << setw(3);
for(short i = 0 ; i < col_size ; ++i)
cout << static_cast<char>(i + 'a');
cout << endl;
for(short i = 0; i < row_size; ++i)
{
cout << setw(2) << i + 1;
for(short j = 0; j < col_size; j++)
cout << table[i][j];
cout << endl;
}
cout << endl;
}
int main()
{
int row_size;
int col_size;
char table[100][100];
cin >> row_size >> col_size;
short center = 0;
for(short i = 0; i < row_size; ++i)
for(short j = 0; j < col_size; ++j)
{
table[i][j] = what_symbol;
}
if (row_size < col_size)
{
center = col_size / 2;
if (col_size % 2 == 0)
{
table[center - 1][center - 1] = p_symbol;
table[center - 2][center] = p_symbol;
table[center - 1][center] = c_symbol;
table[center - 2][center - 1] = c_symbol;
}
else
{
table[center][center - 1] = p_symbol;
table[center - 1][center] = p_symbol;
table[center][center] = c_symbol;
table[center - 1][center - 1] = c_symbol;
}
}
else
{
center = row_size / 2;
if (row_size % 2 == 0)
{
table[center][center - 2] = p_symbol;
table[center - 1][center - 1] = p_symbol;
table[center][center - 1] = c_symbol;
table[center - 1][center - 2] = c_symbol;
}
else
{
table[center][center - 1] = p_symbol;
table[center - 1][center] = p_symbol;
table[center][center] = c_symbol;
table[center - 1][center - 1] = c_symbol;
}
}
show(table, row_size, col_size);
return 0;
}
How can be the issue overcome? I have thought for hours but am stuck.