Thread: someone pliz help!

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Question someone pliz help!

    can someone pllllllllllllllllz help me with this!!!!!!!!

    my problem is

    i have to write a c++ program that inputa a character and an integer. The out put should be a diamond composed of the character and extencing the widht specified byt he integer. for example, if the integer is 11 and the character is an asterisk (*), the diamond would look like this:
    Code:
              *
            ***
           *****
          *******
         *********
       ***********
         *********
           *******
             *****
               ***
                 *
    if the input integer is an even number, it should be increased to the next odd number.
    i have tried this out..but i think my logic is wrong...pliz help me on this
    Code:
    # include <iostream>
    
    using namespace std;
    
    
    
    int main()
    { 
    	int n, r, oddInt;
    	char c;
    
    	
    	// Get int and char
    
    	cout << "Enter number of rows: "<< flush;
    	cin  >> n;
    	cout << "Enter a character: "<< flush;
    	cin  >> c;
    	
    	// determine whether n is odd or even
    	
    	if (n > 1 && n < 23)
    	{
    
    		oddInt = 3;
    		while ( oddInt <= 23)
    		{
    			oddInt = oddInt + 1;
    		
    		}
    	
    		
    	}
    	else if ( n < 1 && n > 23)
    	{
    		cout << "Illegal integer"<< endl;
    	}
    	
    	// draw triangle
    		// r = row
    		// s = space
    		
    	r= 1;
    	while ( r <= n)
    	{		
    		c = 1;
    		
    		while (c <= r )
    		{ 
    			cout << c;
    			c++;
    		}
    
    		r++;
    		cout << endl;
    	
    
    	}	
    
    	return 0;
    }
    [b]Code Tags added by Kermi3[/code]
    Last edited by Nikisha; 03-16-2003 at 10:44 PM.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    A more efficient means of checking for an even number would be
    division by 2 with the mod operator (%). This will return the
    remainder of a number divided by another. For example:
    Code:
    int iNum1 = 20;
    int iNum2 = 2;
    int iResult = 0;
    
    iResult = iNum1 % iNum2;
    // iResult will equal zero
    Just put this logic into a conditional statement to determine if
    even or odd.

    Next, try using two separate loops. One for incrementing and one for decrementing.
    Code:
    // start with one and work up to the number input by the user, 
    // incrementing by 2
    for (int i = 1; i <= iMaxNum; i+=2)
    {
        // Output the number of characters here - format is up to you.
        // A function would be ideal for this.
    }
    
    // Start working your way back done to 1 character using the 
    // input number minus 2 as a starting position, decrementing by 2.
    for (int j = iMaxNum - 2; j >= 1; j-=2)
    {
        // Again, output the number of characters here.
    }
    This is just one way of accomplishing this. Others may offer
    different options, but I felt that this would be easy to follow.

    Hope that helps.
    David
    Last edited by Strider; 03-17-2003 at 07:36 AM.
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed