Home > Software design >  A program that types out a board containing characters
A program that types out a board containing characters

Time:12-03

I found a simple task on a programming discord channel and as a newbie I can not seem to solve it.

The program should use a subprogram that takes width, height, char_1 and char_2 as parameters and the main program should handle texts and inputs only. You could assume that the user doesn't put a width greater than 26 and a height greater than 100.

Input height and width: 5 4
Input characters your program should consist of: H S
1 HSHS
2 HSHS
3 HSHS
4 HSHS
5 HSHS
  ABCD
Input height and width: 3 7
Input characters your program should consist of: / !
1 /!/!/!/
2 !/!/!/!
3 /!/!/!/
  ABCDEFG

I can only come up with a solution where it types out height.

How do I solve this? Any tips are greatly appreciated.

Here's my start:

#include <iostream>
#include <string>

using namespace std;

void print_board(int const width,
                int const  height,
                char const char_1,
                char const char_2)
{
    for (int i {1}; i <= height;   i)
    {
        cout << i << endl;
    }
}
             
int main()
{
    int width {};
    int height {};
    char char_1 {};
    char char_2 {};

    cout << "Enter width and height: ";
    cin >> width >> height;
    cout << endl << "Enter characters: ";
    cin >> char_1 >> char_2;
    cout << endl;

    print_board(width, height, char_1, char_2);

    return 0;
}

CodePudding user response:

For the main board, you need 2 loops - one to print height number of rows, and one to print width number of characters per row.

You also need another loop after the board to print out a row of width number of alphabet letters.

The following works for me:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void print_board(int const height,
                int const width,
                char const char_1,
                char const char_2)
{
    bool toggle = false;
    size_t length = to_string(height).length();

    for (int i = 1; i <= height;   i)
    {
        cout << setw(length) << i << ' ';
        for (int j = 0; j < width;   j){
            cout << (toggle ? char_2 : char_1);
            toggle = !toggle;
        }
        cout << endl;
    }   

    cout << setw(length 1) << ' ';
    for (int j = 0; j < width;   j){
        cout << char('A' j);
    }
    cout << endl;
}
             
int main()
{
    int width {};
    int height {};
    char char_1 {};
    char char_2 {};

    cout << "Enter height and width: ";
    cin >> height >> width;
    cout << endl << "Enter characters: ";
    cin >> char_1 >> char_2;
    cout << endl;

    print_board(height, width, char_1, char_2);

    return 0;
}

Online Demo

Alternatively:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void print_board(int const height,
                int const width,
                char const char_1,
                char const char_2)
{
    const char arr[] = {char_1, char_2};
    int index = 0;

    size_t length = 0;
    if (height < 10)
        length = 1;
    else if (height < 100)
        length = 2;
    else
        length = 3;

    for (int i = 1; i <= height;   i)
    {
        cout << setw(length) << i << ' ';
        for (int j = 0; j < width;   j){
            cout << arr[index];
            index = (index   1) % 2;
        }
        cout << endl;
    }   

    cout << setw(length 1) << ' ';
    for (int j = 0; j < width;   j){
        cout << char('A' j);
    }
    cout << endl;
}
             
int main()
{
    int width {};
    int height {};
    char char_1 {};
    char char_2 {};

    cout << "Enter height and width: ";
    cin >> height >> width;
    cout << endl << "Enter characters: ";
    cin >> char_1 >> char_2;
    cout << endl;

    print_board(height, width, char_1, char_2);

    return 0;
}

Online Demo

CodePudding user response:

The error in your code is trying to change the value of height which is declared as constant.

That is why your code will not compile, and your compiler, whatever it is, probably told you this.

But fortunately, you don't need to change the value of height!

Printing the height is a simple, straight forward for loop, from 1 to the value of height:

for (int i = 1; i <= height; i  ) {
    cout << i << endl;
}

The tricky part in this puzzle would be to actually print the contents of the lines, because as the second example shows, you need to iterate between the two characters continuously, and not start every line with the first character.

But there is a simple trick to do this!
I will explain it but leave the code to you:

  1. Put your two characters in a character array.
  2. Multiply counters from the two loops to calculate the number of the current character.
  3. Use the power of modulus (%) to figure out which character should be printed.

Have fun!

  • Related