Guest User

main.cpp

a guest
Jun 13th, 2014
1,541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. /* Prints a Daimond of characters to the screen.
  8.  
  9.     Explanation: We want to use printf to generate a diamond,
  10.     and we want to use as few for loops as possible.
  11.  
  12.     We need two things.
  13.  
  14.     -First, the * specifier, which specifies the
  15.     desired width of the string. If the string we print
  16.     out is less than the number we specify for *, then it
  17.     gets padded to the left by spaces.
  18.  
  19.     -Second, the .* specifier. This specifies the number
  20.     of letters that we want to print from our string.
  21.  
  22.     By combining *.* we get to use both of these together,
  23.     which lets us customize the shape. The full print statement is
  24.  
  25.     printf("%*.*s\n", stringWidth, numChars, chars.c_str());
  26.  
  27.     -stringWidth is the desired width, and we get it with the formula:
  28.     stringWidth = halfHeight + i + 1;
  29.     when i >= 0 and i < halfHeight. This is because we need at least
  30.     halfHeight + 1 to be our width, and each time i increases, we want
  31.     the string to get bigger by 1, since we are adding 2 characters to
  32.     the end but moving the string to the left by 1. 2 - 1 = 1.
  33.  
  34.     numChars is the number of letters we want the string to print.
  35.     At the top, it should be 1. Then as we go down the diamond towards
  36.     the middle, it should increase by 2 each time. So 1, 3, 5, 7 ect.
  37.     so we can use the formula:
  38.     numChars = i * 2 + 1;
  39.     when i >= 0 and i < halfHeight;
  40.  
  41.     when we want to print the bottom half of the diamond, we just do
  42.     the exact same formulas, but with a for loop that goes backward
  43.     starting at halfHeight and ending at 0.
  44.  
  45.     Made by: Ben Arnold
  46. */
  47.  
  48. int main() {
  49.     //this is the letter we want to use
  50.     char inputChar;
  51.     //this is the height of the daimond
  52.     int height;
  53.  
  54.     cout << "*** Daimond Generator! ***\n\n";
  55.  
  56.     cout << "Enter the height of the Daimond: ";
  57.     cin >> height;
  58.     cout << endl;
  59.     cout << "Enter the character you want to use to draw it: ";
  60.     cin >> inputChar;
  61.     cout << endl;
  62.  
  63.     int halfHeight = height / 2;
  64.     int maxWidth = height + 1;
  65.  
  66.     //the chars string will hold maximum number of chars in
  67.     //a single row so we can easily print it to the screen
  68.  
  69.     //This specific constructor makes a string of size HEIGHT
  70.     //that has all inputChar's for characters!
  71.     string chars(height * 2 + 1, inputChar);
  72.  
  73.     //the width of the string to put on the screen
  74.     int stringWidth;
  75.     //number of chars to put on the screen
  76.     int numChars;
  77.    
  78.     //top half
  79.     for (int i = 0; i < halfHeight; i++) {
  80.         stringWidth = halfHeight + i + 1;
  81.         numChars = i * 2 + 1;
  82.         printf("%*.*s\n", stringWidth, numChars, chars.c_str());
  83.     }
  84.  
  85.     //bottom half (backwards for loop)
  86.     for (int i = halfHeight; i >= 0; i--) {
  87.         stringWidth = halfHeight + i + 1;
  88.         numChars = i * 2 + 1;
  89.         printf("%*.*s\n", stringWidth, numChars, chars.c_str());
  90.     }
  91.  
  92.     system("PAUSE");
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment