some slight help..

This is a discussion on some slight help.. within the C Programming forums, part of the General Programming Boards category; i'm trying to print out a triangular form of a set of numbers should be printing out like this 1 ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    25

    some slight help..

    i'm trying to print out a triangular form of a set of numbers
    should be printing out like this
    1
    22
    333
    4444
    55555
    666666
    up to 9.

    but i'm getting
    111111111
    222222222
    333333333
    up to 9
    then i get a little 10 hanging on the end..
    like so

    888888888
    999999999
    10

    here's my code..

    main()
    {
    int n, col, row;

    printf("Enter value n: ");
    scanf("%d", &n);

    while (n <= 9)
    {
    for(row=0; row<=9; row++)
    {
    /*for(col=n; col<=9; col++)
    {
    printf("%d", n);
    }*/
    printf("%d", n);
    }
    printf("\n");
    n++;
    }

    printf("%d", n);
    scanf("%d");
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    1. use code tags
    2. one of your loops should run 1 to n, not 1 to 9
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        int n, i, j;
        printf("Enter value n: ");
        scanf("%d", &n);
    
        for(i=0;i<n+1;i++)
        {
            for(j=0;j<i;j++)
            {
                printf("%d",i);
            }
            printf("\n");
        }
        getch();
        return 0;
    }

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by sand_man
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        int n, i, j;
        printf("Enter value n: ");
        scanf("%d", &n);
    
        for(i=0;i<n+1;i++)
        {
            for(j=0;j<i;j++)
            {
                printf("%d",i);
            }
            printf("\n");
        }
        getch();
        return 0;
    }
    Whats up with the non-standard header and function call?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kermit
    Whats up with the non-standard header and function call?
    More importantly, what's up with doing the whole thing for them? If you're going to do it, do something interesting with it:
    Code:
    for( i = 0; i < k; i += printf("\n") )
        for( j = 0; j < i+1; j += printf("%d",i+1) );
    This works because the code is restricted to just 1-9. (The other code posted will also break if you input higher than 9.) Naturally, there are more ways to do this. Since you already know that you can never go past nine...
    Code:
    char *buf[9] = { 
        "1",
        "22", 
        "333", 
        "4444", 
        "55555",
        "666666",
        "7777777",
        "88888888",
        "999999999" };
    for( i = 0; i < k; i++ )
        printf("%s\n", buf[i] );
    Or perhaps...
    Code:
    char buf[]={"1\n22\n333\n4444\n55555\n666666\n7777777\n88888888\n999999999\n"};
    puts( buf );
    [preview]
    There's an interesting bug with this forum. It seems you cannot make the above line. It doesn't work. Oh, sure, you see it. But the way you see it is not the way the actual line is. There are no spaces in the true above line. This forum apparently inserts whitespace after the eights. I have no idea why, nor have I found a way around this. Splitting this line so that the quotation marks are the first and last characters on the line doesn't change the forum-generated visual. There is no way to get this line to actually appear the way it really does using [code] tags. It is also interesting to note, that if I use [quote] tags, it inserts one space, where as the [code] tags insert what appears to be two spaces.

    At any rate, assume there are no spaces in the above char line.
    [/preview]
    You could even shorten the above to a string literal, and puts or printf it directly.

    Or you could always do:
    Code:
    for( i = 0; i < k; i++, putchar('\n') )
        for( j = 0; j < i+1; j++, putchar( '1'+i ) );
    You know, something amusing that the instructor will appreciate.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by quzah
    Code:
    for( i = 0; i < k; i++, putchar('\n') )
        for( j = 0; j < i+1; j++, putchar( '1'+i ) );
    You know, something amusing that the instructor will appreciate.

    Quzah.
    You know I've never really though about putting function calls inside the for loop like that. Oh man I'm gonna have some fun

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Here's a fun ditty which prints the triangle upside down

    Code:
    #include <stdio.h>
    
    void foo ( int x, int y ) {
        if ( x == 0 ) return;
        else if ( y == 0 ) {
            putchar('\n');
            foo(x-1,x-1);
        } else {
            putchar('0'+x);
            foo(x,y-1);
        }
    }
    
    int main ( void ) {
        foo(9,9);
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    Whats up with the non-standard header and function call?
    sorry, its from when i was compiling it in borland. i just copy and pasted it and forgot to remove it.

    More importantly, what's up with doing the whole thing for them?
    sorry

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    the main code is :
    for(i=1;i<=10;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("%d",i);
    }
    printf("\n");
    }

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The only problem I see is that you print 10 as well. Your first post suggested that the last line should be 9s. You can fix it here:

    >for(i=1;i<=10;i++)
    for ( i = 1; i < 10; i++ )
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help removing slight switch flicker
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2006, 04:20 AM
  2. slight problem
    By duvernais28 in forum C Programming
    Replies: 4
    Last Post: 02-03-2005, 10:03 AM
  3. slight problem on win app tut
    By JustPlay4Fun in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2005, 10:42 AM
  4. Slight problem...
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-10-2002, 01:32 PM
  5. slight problem just can't see the answer
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2002, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21