Thread: printing a triangle with $ inside

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    3

    printing a triangle with $ inside

    Hello guys!

    I need to print a shape like this:
    *
    *$*
    *$$$*
    *$ $$*
    *$$$ $$*
    *$$$ $$$$*
    *$ $$$$$ $*
    *$$$$ $$$$$ *
    *****************


    Instead of a space, a dollar is printed, and from the 2nd line it counts 5$ and then prints a double space.

    Now, let's say that the number of rows is 5, so I have this:

    *
    * *
    *$$$*
    *$$$$$*
    *$$$$$$$*

    and I don't know what condition I need to write within the loop, so that it will work. Any suggestions?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to post your diagrams in [code][/code] bbcode tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    From your input which you shared,
    Check the logic on
    1. last line printing.
    2. Why there is space instead of $ symbol on second line.

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    Ok, here's my code:
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    
    
    int main()
    {
    
    
        int rowNum, row, colNum, col1, col2, counter = 0;
    
    
        printf("Enter row number\n");
        scanf("%d", &rowNum);
        colNum = rowNum - 1;
    
    
        for (row = 1; row <= rowNum; row++)
        {
            for (col1 = 1; col1 <= colNum; col1++)
            {
                printf(" ");
            }
    
    
            colNum--;
            for (col2 = 0; col2 <= row; col2++)
            {
                if (col2 == 0 || col2 == row)
                    printf("*");
    
    
                else
                {
                    counter = counter + 1;
                    if (counter == 5) {
                        printf(" ");
                        counter = 0;
                    }
                    else printf("$");
                }
                     
            }
            printf("\n");
        }
    
    
        
    
    
        getchar();
        getchar();
    }
    What should I fix?

  5. #5
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    I can not understand what result you want? By what laws should alternate dollars and spaces?

  6. #6
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    Sorry guys, I had a mistake in the triangle.. the spaces should be every 5 "$", so here's my fixed code:


    Code:
    # include <stdio.h>
    # include <stdlib.h>
    
    
    int main()
    {
    
    int t,spaceNum=0,d=0,counter=0,i, j, k, n;
    
    printf("Enter triangle height\n");
    scanf("%d", &n);
    if (n < 2) return 0;
    printf("Enter the number of $\n");
    scanf("%d", &d);
    printf("Enter number of spaces\n");
    scanf("%d", &spaceNum);
    printf("\n");
    for (i = 0; i< n ; i++)
    printf(" ");
    
    printf("*\n");
    for (i = 1; i<n-1 ; i++)
    {
    for (j = n - i; j>0; j--)
    printf(" ");
    printf("*");
    for (k = 0; k<2 * i-1; k++)
    
    if (k >= 1 || k<(2*i-1))
    {
    counter = counter + 1;
    if (counter == d+1)
    {
    for (t = 1; t <= spaceNum; t++)
    {
    printf(" ");
    }
    counter = 0;
    }
    else printf("$");
    }
    printf("*\n");
    }
    printf(" ");
    for (i = 1; i<=2* n-1 ; i++)
    printf("*");
    
    printf("\n");
    printf("\n");
    
    printf("The height of triangle is %d\n", n);
    printf("The number of $ is %d\n", d );
    printf("The number of space is %d\n", spaceNum);
    
    getchar();
    getchar();
    }

  7. #7
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    for (k = 0; k<2 * i-1; k++) // Don't omit the curly braces.
    
    if (k >= 1 || k<(2*i-1)) // This conditional expression is always true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a Triangle:
    By jocdrew21 in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2013, 03:46 PM
  2. Replies: 3
    Last Post: 09-29-2013, 05:25 AM
  3. Printing a triangle
    By chaudhryali55 in forum C Programming
    Replies: 1
    Last Post: 11-21-2009, 06:37 AM
  4. printing a triangle
    By viaxd in forum C Programming
    Replies: 13
    Last Post: 01-21-2004, 04:39 PM

Tags for this Thread