I have written codes to print a box with # outline but I am trying to print out a box which looks something exactly like that instead:
# # # # # # # # # # #
8# #
7# #
6# #
5# #
4# #
3# #
2# #
1# #
0# #
# # # # # # # # # # #
0 1 2 3 4 5 6 7 8
The codes I currently have and the output attached image:
int i = Xend;
int j = Yend;
int side = Yend; // Yend is = 8;
for (i = 0; i < Xend; i ) // Xend is = 8;
{
for (j = 0; j < side; j ) // Ystart is 0;
{
if (i == 0 || i == side - 1 || j == 0 || j == side - 1)
{
cout << "#"
<< " ";
}
else
{
cout << " ";
}
}
cout << "\n";
}
CodePudding user response:
I am very sorry. But I have to do this . . .
There are that many potential solutions and here is one of them:
#include <iostream>
#include <string_view>
constexpr std::string_view box(R"(
# # # # # # # # # # #
8# #
7# #
6# #
5# #
4# #
3# #
2# #
1# #
0# #
# # # # # # # # # # #
0 1 2 3 4 5 6 7 8
)");
int main() {
std::cout << box;
}
You can also do programatically:
#include <iomanip>
#include <string>
int main() {
// Print upper box row
for (int i = 0; i < 11; i)
std::cout << " #";
std::cout << '\n';
// Now print out 9 rows with number and left and right box delimiter
for (int i = 0; i < 9; i) {
std::cout << (8 - i) << '#';
for (int k = 0; k < 9; k) std::cout << " ";
std::cout << " #\n";
}
// Print lower box row
for (int i = 0; i < 11; i)
std::cout << " #";
std::cout << '\n';
// Print row with numbers
std::cout << " ";
for (int i = 0; i < 9; i)
std::cout << i << ' ';
std::cout << '\n';
}
CodePudding user response:
Slighty more generic answer which will work for 1-9 rows. You could make it work for all sizes if you fiddle with the padding and make it conditional on number of digits used.
int numX = 8;
int numY = 8;
int Yincrementer = 0;
const int Xend = numX 3;
const int Yend = numY 3;
for (int i = Xend; i >= 0; i--) {
for (int j = 0; j < Yend; j ) {
//Handle top and bottom lines
if (i == Xend || i == 1) {
if (j == 0) {
std::cout << " # ";
}
else if (j == Yend - 1) {
std::cout << "#";
}
else {
std::cout << "# ";
}
}
//Handle numerical bottom line
else if (i == 0) {
if (j == 0 || j == 1) {
std::cout << " ";
}
else {
if (Yincrementer <= numY) {
std::cout << " " << Yincrementer;
Yincrementer ;
}
}
}
//Handle grid lines
else{
if (j == 0) {
if (numX >= 0) {
std::cout << numX << "# ";
numX--;
}
else {
std::cout << " # ";
}
}
else if (j == Yend -1) {
std::cout << "#";
}
else {
std::cout << " ";
}
}
}
}