Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Need a program for generating the Triangular set of stars without using spaces.


    I need a program for the output

    *
    **
    ***
    ****
    *****

    i.e a Triangular set of stars without using spaces in between stars...
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Well we're not going to write it for you.

    "Without using spaces"... I personally wouldn't have thought to use them in the first place. Do you have a version that does use stars? Maybe we can just help you fix that to not use them.
    Or maybe you have started on something but have a problem? What's your code so far and what is the problem?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    I have code


    Ya I have a code with spaces in between star's.



    Code:
    int main()
    {
      
            int c=1;
            for(int i=1;i<=5;i++)
            {
                for(int j=i;j<5;j++)
                {
                cout<<" ";
                }
                
                for(int k=1;k<=c;k++)
                {
                    if(k%2==0)
                        cout<<" ";
                    else
                        cout<<"*";
                }
            cout<<"\n";
            c+=2;
            }
    
    
    }
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Code:
    if(k%2==0)
        cout<<" ";
    else
        cout<<"*";
    Well, that second line there is where it outputs the space. Try removing it. Keep in mind you can't literally just delete the line - you need to alter the if block a bit too.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by requinix
    Code:
    if(k%2==0)
        cout<<" ";
    else
        cout<<"*";
    Well, that second line there is where it outputs the space. Try removing it. Keep in mind you can't literally just delete the line - you need to alter the if block a bit too.
    Ya.. however altered, the output is not arriving... :(
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    What's your code after the changes? And what do you mean, exactly, by "not arriving"?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    It means that I cant get any idea to get the output... Tats y asking help in this forum..
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    you can not generate such pattern without space.
    if u want to make triangle without space then u should follow another pattern.

    eg

    *
    ***
    *****
    *******
  16. #9
  17. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Lemme tell you exactly what I did to get three different "Triangular set of stars without using spaces in between stars" outputs:
    1. Created a .cpp file
    2. Typed the line "#include <iostream>"
    3. Typed the line "using namespace std;"
    4. Copy/pasted the code from your second post

    Now the changes:
    5. Commented out three lines, compiled and ran, and got
    Code:
        *
       ***
      *****
     *******
    *********
    6. Seeing that it was adding two stars per line when I wanted it to add one, made it only add one star per line and got
    Code:
        *
       **
      ***
     ****
    *****
    7. Wanted to make the triangle face right so I commented out an entire loop and got
    Code:
    *
    **
    ***
    ****
    *****
    Now, let's try this again:
    * You want the third of those outputs, right?
    * What code did you try that wasn't working?
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    * You want the third of those outputs, right?
    i think he want same pattern but without spaces.
    and that's not possible.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    I was asked to generate a program for this kind of pattern in the interview :(

    The output I need is
    *
    **
    ***
    ****
    *****

    Simple HTML generates this output... But not C :(
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    ^^its because html has alignment option and c does not have such option.


    check same pattern is gives different output.
    Left:
    *
    **
    ***
    ****
    *****

    Center:
    *
    **
    ***
    ****
    *****

    Right:
    *
    **
    ***
    ****
    *****

    so it can adjust spaces by itself.
    but in c we have to adjust spaces.

    and we cant add half space so pattern u want without space is not possible in c.
  24. #13
  25. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Originally Posted by eramit2010
    and we cant add half space so pattern u want without space is not possible in c.
    Still not sure how it's impossible without spaces between the stars. All three patterns you (and I) showed don't have spaces between the stars.
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by requinix
    Still not sure how it's impossible without spaces between the stars. All three patterns you (and I) showed don't have spaces between the stars.

    he want this pattern

    *
    **
    ***
    ****
    *****

    so if we want to make such pattern in then we need half space and i think that's not possible.

    we can make such pattern with odd number of starts.
    ie.
    *
    ***
    *****
    *******
    *********
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Code:
    #include <stdio.h>
    
    main()
    {
    	int line, space, variable;
    	
      	for(line=1; line<=9; line+=2)
    	{
    		for(space=7; space>=line; space-=2)
    		{
    			printf(" ");
    		}
    		for(variable=1; variable<=line; variable++)
    		{
    			printf("*");
    		}
    		printf("\n");
    	}
    }
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo