solid-diamond.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // ==================================================
  2. // Programmer: Jonathan Landrum
  3. // Date: 4 February 2012
  4. // ==================================================
  5. // Program: solid-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 up.
  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 << "- Solid 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. } // End for
  61. for (int i = 0; i < counter; i++) {
  62. cout << "*";
  63. } // End for
  64. cout << endl;
  65. counter += 2;
  66. } // End while
  67. while (counter > 0) {
  68. spaces = (input - counter) / 2;
  69. for (int j = 0; j < spaces; j++) {
  70. cout << " ";
  71. } // End for
  72. for (int n = 0; n < counter; n++) {
  73. cout << "*";
  74. } // End for
  75. cout << endl;
  76. counter -= 2;
  77. }
  78. return (0);
  79. } // End main