Thread: Can someone help me with this "triangle" problem.

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    Can someone help me with this "triangle" problem.

    For example, a pattern of size 5 had 5 rows and 5 columns and each row is made up of characters @ and a digit of the size given. This digit must be between 2 and 9.

    sample:
    5@@@@
    @5@@@
    @@5@@
    @@@5@
    @@@@5

    and

    @@@@@
    5@@@@
    55@@@
    555@@
    5555@


    This is what I have so far and I dont know what to change. And yes, I am a total beginner.

    Code:
    int i;
    int j;
    
    for (i=0;i<size;i++) {
             for (j=0;j<size;j++) {
                  printf("@");
                  
             }
             printf("\n");
        }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That's a good start, your code prints a square of @ symbols.

    Inside your "j" loop you'll need an if statement that decides whether to print a @ or the digit.

    If I'm in row 2, column 4, do I need to print a @ or a 5? How about row 1, column 1? Can you describe the pattern that decides whther it should be a @ or a 5? Once you can do that, it should be very easy to put that into your if statement.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    Quote Originally Posted by TheBigH View Post
    That's a good start, your code prints a square of @ symbols.

    Inside your "j" loop you'll need an if statement that decides whether to print a @ or the digit.

    If I'm in row 2, column 4, do I need to print a @ or a 5? How about row 1, column 1? Can you describe the pattern that decides whther it should be a @ or a 5? Once you can do that, it should be very easy to put that into your if statement.
    The pattern I see for the first triangle is that for the row/column 1, it will print the digit and then row/column 2, it will print the digit again, and so on. But I just can't put it in code...

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Good. So if the number of the row (variable i) is equal to the number of the column (variable j) print a 5, otherwise print a @.

    If I just hand out the code you won't learn anything, but the previous sentence is pretty much all you need. Just translate it into code form. The bolded parts are hints.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    Yea, now it makes sense...Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-17-2012, 09:02 AM
  2. Replies: 2
    Last Post: 03-13-2008, 11:34 AM
  3. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM