By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

class project

P: 4
I need to know how i could right a program that will take as input a character and an integer. If the integer is even, then add one to it. Using the for loop to make the output a diamond composed of a character and as wide as the int.
e.i. Enter a character and an integer:
@ 4

@
@@
@@@
@@@@
@@@
@@
@

I would really appreciate it i've been trying to figure it out for the entire weekend
Oct 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: 27
Try this out, let me know if it does what you want.

Wasn't sure about the adding one to an even number thing, I might have been mis-reading your question, but this should start you out:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.     char symbol;
  8.     int numPrints, i, j;
  9.  
  10.     cout << "Enter symbol: ";
  11.     cin >> symbol;
  12.     cout << endl << endl;
  13.     cout << "Enter Integer: ";
  14.     cin >> numPrints;
  15.     cout << endl << endl;
  16.  
  17.     for (i = 1; i <= numPrints; i++)
  18.     {
  19.         for (j = 1; j <= i; j++)
  20.         {
  21.             cout << symbol;
  22.         }
  23.         cout << endl;
  24.     }
  25.  
  26.     for ( i = numPrints - 1; i > 0; i--)
  27.     {
  28.         for (j = i; j > 0; j--)
  29.         {
  30.             cout << symbol;
  31.         }
  32.         cout << endl;
  33.     }
  34.  
  35.     return 0;
  36. }
  37.  
Anthony
Oct 3 '06 #2

100+
P: 293
D_C
He didn't use (code) tags, so the whitespace was not preserved. He meant
Expand|Select|Wrap|Line Numbers
  1.    @
  2.   @@@
  3.  @@@@@
  4. @@@@@@@
  5.  @@@@@
  6.   @@@
  7.    @
You need two loops, one to do the top half of the diamond, one to do the second half of the diamond. Then each loop needs to first print some spaces, then print that symbol several times.
Expand|Select|Wrap|Line Numbers
  1. string(numberOfSymbol,symbol)
Prints the (char)symbol (int)numberOfSymbol times. I'll pseudocode the printing part.
Expand|Select|Wrap|Line Numbers
  1. // Top half
  2. numSymbols = 1;
  3. while(numSymbols < maxSymbols)
  4. {
  5.   numSpaces = // A function of numSymbols and maxSymbols
  6.   cout << string(numSpaces,' ');
  7.   cout << string(numSymbols,symbol) << endl;
  8.   numSymbols += 2;
  9. }
  10. // Bottom half
  11. while(numSymbols > 0)
  12. {
  13.   numSpaces = // The same function of numSymbols and maxSymbols
  14.   cout << string(numSpaces,' ');
  15.   cout << string(numSymbols,symbol);
  16.   numSymbols -= 2;
  17. }
Oct 3 '06 #3

P: 27
Ahh, I see - sorry for the confusion. Didn't realize the absence of code tags.

Thanks, D_C, for clearing that up, and answering the question.
Oct 3 '06 #4

P: 4
thank you for your help
Oct 4 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.