char-diamond.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ==================================================
  2. // Programmer: Jonathan Landrum
  3. // Date: 4 February 2012
  4. // ==================================================
  5. // Program: char-diamond.cpp
  6. // Purpose: Takes user input of one character and
  7. // returns a diamond of that size.
  8. // Assumptions: 1.) Input is a single letter (char).
  9. // Any other input will return un-
  10. // expected results.
  11. // ==================================================
  12. #include <iostream>
  13. #include <string>
  14. using namespace std;
  15. // --------------------------------------------------
  16. // Global Variables
  17. // --------------------------------------------------
  18. char letter[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  19. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  20. // --------------------------------------------------
  21. // main():
  22. // Does ALL the things!
  23. // --------------------------------------------------
  24. int main () {
  25. // ------------------------------------------
  26. // Declare variables, not war
  27. // ------------------------------------------
  28. char in;
  29. int counter;
  30. int input;
  31. int level;
  32. int spaces;
  33. // ------------------------------------------
  34. // Introduce the program
  35. // ------------------------------------------
  36. cout << endl;
  37. cout << "---------------------------" << endl;
  38. cout << "- Character Diamond -" << endl;
  39. cout << "---------------------------" << endl;
  40. cout << endl;
  41. cout << " A" << endl;
  42. cout << " B B" << endl;
  43. cout << " C C" << endl;
  44. cout << " D D" << endl;
  45. cout << " E E" << endl;
  46. cout << " D D" << endl;
  47. cout << " C C" << endl;
  48. cout << " B B" << endl;
  49. cout << " A" << endl;
  50. cout << endl;
  51. cout << "This program asks for a" << endl;
  52. cout << "character, and returns a" << endl;
  53. cout << "diamond of that size." << endl;
  54. cout << endl;
  55. // ------------------------------------------
  56. // Main processing loop
  57. // ------------------------------------------
  58. cout << "Enter a single letter for the size of your diamond: ";
  59. cin >> in;
  60. input = toupper(in) - 64;
  61. input = input * 2 - 1;
  62. counter = 1;
  63. level = 0;
  64. while (counter < input) {
  65. if (input % 2 == 0)
  66. spaces = ((input - counter) / 2) + 1;
  67. else
  68. spaces = (input - counter) / 2;
  69. for (int c = 0; c < spaces; c++)
  70. cout << " ";
  71. cout << letter[level];
  72. if (counter == 1)
  73. spaces = 0;
  74. else
  75. spaces = counter - 2;
  76. for (int i = 0; i < spaces; i++)
  77. cout << " ";
  78. if (counter != 1)
  79. cout << letter[level] << endl;
  80. else
  81. cout << endl;
  82. counter += 2;
  83. level++;
  84. } // End while
  85. while (counter > 0) {
  86. if (input % 2 == 0)
  87. spaces = ((input - counter) / 2) + 1;
  88. else
  89. spaces = (input - counter) / 2;
  90. for (int j = 0; j < spaces; j++)
  91. cout << " ";
  92. cout << letter[level];
  93. if (counter == 1)
  94. spaces = 0;
  95. else
  96. spaces = counter - 2;
  97. for (int n = 0; n < spaces; n++)
  98. cout << " ";
  99. if (counter != 1)
  100. cout << letter[level] << endl;
  101. else
  102. cout << endl;
  103. counter -= 2;
  104. level--;
  105. } // End while
  106. return (0);
  107. } // End main