fork 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. const char arr[] = {char_1, char_2};
  13. int index = 0;
  14.  
  15. size_t length = 0;
  16. if (height < 10)
  17. length = 1;
  18. else if (height < 100)
  19. length = 2;
  20. else
  21. length = 3;
  22.  
  23. for (int i = 1; i <= height; ++i)
  24. {
  25. cout << setw(length) << i << ' ';
  26. for (int j = 0; j < width; ++j){
  27. cout << arr[index];
  28. index = (index + 1) % 2;
  29. }
  30. cout << endl;
  31. }
  32.  
  33. cout << setw(length+1) << ' ';
  34. for (int j = 0; j < width; ++j){
  35. cout << char('A'+j);
  36. }
  37. cout << endl;
  38. }
  39.  
  40. int main()
  41. {
  42. int width {};
  43. int height {};
  44. char char_1 {};
  45. char char_2 {};
  46.  
  47. for(int i = 0; i < 2; ++i){
  48. cout << "Enter height and width: ";
  49. cin >> height >> width;
  50. cout << endl << "Enter characters: ";
  51. cin >> char_1 >> char_2;
  52. cout << endl;
  53.  
  54. print_board(height, width, char_1, char_2);
  55. }
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5360KB
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