fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. void print_board(int const height,
  8. int const width,
  9. char const char_1,
  10. char const char_2)
  11. {
  12. bool toggle = false;
  13. size_t length = to_string(height).length();
  14.  
  15. for (int i = 1; i <= height; ++i)
  16. {
  17. cout << setw(length) << i << ' ';
  18. for (int j = 0; j < width; ++j){
  19. cout << (toggle ? char_2 : char_1);
  20. toggle = !toggle;
  21. }
  22. cout << endl;
  23. }
  24.  
  25. cout << setw(length+1) << ' ';
  26. for (int j = 0; j < width; ++j){
  27. cout << char('A'+j);
  28. }
  29. cout << endl;
  30. }
  31.  
  32. int main()
  33. {
  34. int width {};
  35. int height {};
  36. char char_1 {};
  37. char char_2 {};
  38.  
  39. for(int i = 0; i < 2; ++i){
  40. cout << "Enter height and width: ";
  41. cin >> height >> width;
  42. cout << endl << "Enter characters: ";
  43. cin >> char_1 >> char_2;
  44. cout << endl;
  45.  
  46. print_board(height, width, char_1, char_2);
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 5512KB
stdin
5 4 H S
3 7 / !
stdout
Enter height and width: 
Enter characters: 
1 HSHS
2 HSHS
3 HSHS
4 HSHS
5 HSHS
  ABCD
Enter height and width: 
Enter characters: 
1 /!/!/!/
2 !/!/!/!
3 /!/!/!/
  ABCDEFG