class project

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phuker420
    New Member
    • Oct 2006
    • 4

    class project

    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
  • apusateri
    New Member
    • Oct 2006
    • 27

    #2
    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:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	char symbol;
    	int numPrints, i, j;
    
    	cout << "Enter symbol: ";
    	cin >> symbol;
    	cout << endl << endl;
    	cout << "Enter Integer: ";
    	cin >> numPrints;
    	cout << endl << endl;
    
    	for (i = 1; i <= numPrints; i++)
    	{
    		for (j = 1; j <= i; j++)
    		{
    			cout << symbol;
    		}
    		cout << endl;
    	}
    
    	for ( i = numPrints - 1; i > 0; i--)
    	{
    		for (j = i; j > 0; j--)
    		{
    			cout << symbol;
    		}
    		cout << endl;
    	}
    
    	return 0;
    }
    Anthony

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      He didn't use (code) tags, so the whitespace was not preserved. He meant
      Code:
         @
        @@@
       @@@@@
      @@@@@@@
       @@@@@
        @@@
         @
      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.
      Code:
      string(numberOfSymbol,symbol)
      Prints the (char)symbol (int)numberOfSy mbol times. I'll pseudocode the printing part.
      Code:
      // Top half
      numSymbols = 1;
      while(numSymbols < maxSymbols)
      {
        numSpaces = // A function of numSymbols and maxSymbols
        cout << string(numSpaces,' ');
        cout << string(numSymbols,symbol) << endl;
        numSymbols += 2;
      }
      // Bottom half
      while(numSymbols > 0)
      {
        numSpaces = // The same function of numSymbols and maxSymbols
        cout << string(numSpaces,' ');
        cout << string(numSymbols,symbol);
        numSymbols -= 2;
      }

      Comment

      • apusateri
        New Member
        • Oct 2006
        • 27

        #4
        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.

        Comment

        • phuker420
          New Member
          • Oct 2006
          • 4

          #5
          thank you for your help

          Comment

          Working...