hollow-diamond.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // ==================================================
  2. // Programmer: Jonathan Landrum
  3. // Date: 4 February 2012
  4. // ==================================================
  5. // Program: hollow-diamond.cpp
  6. // Purpose: Takes user input of one integer and
  7. // returns a diamond of that size.
  8. // Assumptions: 1.) Input should be an odd integer:
  9. // - Real input is rounded down.
  10. // - Even input is incremented.
  11. // ==================================================
  12. #include <iostream>
  13. #include <string>
  14. using namespace std;
  15. // --------------------------------------------------
  16. // main():
  17. // Does ALL the things!
  18. // --------------------------------------------------
  19. int main () {
  20. // ------------------------------------------
  21. // Declare variables, not war
  22. // ------------------------------------------
  23. int input;
  24. int spaces;
  25. int counter;
  26. // ------------------------------------------
  27. // Introduce the program
  28. // ------------------------------------------
  29. cout << endl;
  30. cout << "---------------------------" << endl;
  31. cout << "- Hollow Diamond -" << endl;
  32. cout << "---------------------------" << endl;
  33. cout << endl;
  34. cout << " *" << endl;
  35. cout << " * *" << endl;
  36. cout << " * *" << endl;
  37. cout << " * *" << endl;
  38. cout << " * *" << endl;
  39. cout << " * *" << endl;
  40. cout << " * *" << endl;
  41. cout << " * *" << endl;
  42. cout << " *" << endl;
  43. cout << endl;
  44. cout << "This program asks for an" << endl;
  45. cout << "integer, and returns a" << endl;
  46. cout << "diamond of that size." << endl;
  47. cout << endl;
  48. // ------------------------------------------
  49. // Main processing loop
  50. // ------------------------------------------
  51. cout << "Enter a number for the size of your diamond: ";
  52. cin >> input;
  53. if (input % 2 == 0)
  54. input++;
  55. counter = 1;
  56. while (counter < input) {
  57. spaces = (input - counter) / 2;
  58. for (int c = 0; c < spaces; c++)
  59. cout << " ";
  60. cout << "*";
  61. if (counter == 1)
  62. spaces = 0;
  63. else
  64. spaces = counter - 2;
  65. for (int i = 0; i < spaces; i++)
  66. cout << " ";
  67. if (counter != 1)
  68. cout << "*" << endl;
  69. else
  70. cout << endl;
  71. counter += 2;
  72. } // End while
  73. while (counter > 0) {
  74. spaces = (input - counter) / 2;
  75. for (int j = 0; j < spaces; j++)
  76. cout << " ";
  77. cout << "*";
  78. if (counter == 1)
  79. spaces = 0;
  80. else
  81. spaces = counter - 2;
  82. for (int n = 0; n < spaces; n++)
  83. cout << " ";
  84. if (counter != 1)
  85. cout << "*" << endl;
  86. else
  87. cout << endl;
  88. counter -= 2;
  89. } // End while
  90. return (0);
  91. } // End main