Thread: can someone help me with this loop ?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    can someone help me with this loop ?

    im making a loop to print an inverted triangle at N=5

    my work so far in the part of the loop:

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int N,row,space,column;
    printf("Please insert the value of N");
    scanf("%d",&N);
    for (row=0; row<=N ; row++)
    {
          for (space=0;space<=N-5+row;space++)
          {
            printf(" ");
          }
          for (column=1;column<=          ;column++)
              {
                 printf("*");
               }
       printf("\n");
    }
    getch();
    }
    the required output is like this,without the "-" of course:
    *********
    -*******
    --*****
    ---***
    ----*


    can anyone help me plz with the column part, im not able to link the asterisks to the row
    Last edited by everyone0; 04-18-2010 at 08:20 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    What you have is mostly wrong. First of all, if you do "for(row = 0; row <= something; row++)", then the loop will be executed something+1 times: for 0, 1, 2, 3, ..., something. You want it one time less.

    I'm not going to give you the answer here. First, you tell me: how do you calculate, given the row, the number of spaces or stars required? Think of it in normal math first, before you translate it to C, if that helps you. Just write down, here, the formula for both.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    well spaces = N-5+row
    stars = -2*row+9

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    nvm lol i already tried it and it worked , why did u say that i was wrong ! ??

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by everyone0 View Post
    nvm lol i already tried it and it worked , why did u say that i was wrong ! ??
    I'd imagine he did because it's true.

    With the code you first posted the outside loop does run once more than you want it to (although it may not print anything depending on what your printing test loop does). This isn't to mention the obvious error/typo with:
    Code:
    for (column=1;column<=          ;column++)
    And even now, your code seems to assume that N is 5. If it does that then there is no point in actually getting input.

    For example, if N is 6, then N - 5 + row will yield 1, so you'd have one space on your first row, which isn't what you said you needed. -2 * row + 9 will give -1 on the last row (when N is 6, your last row is five).


    EDIT: Why would you even post it if YOU didn't think it was wrong? (at the time of posting)

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    im posting so that experts like u could correct me and also because i dont know if im right, sorry about that . , so what is the equation is supposed to be like so that it be correct ?
    Last edited by everyone0; 04-18-2010 at 09:54 AM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Think about it, if 5 works for the case when N is 5, then maybe 6 will work for when N is 6 and so on. (It isn't a guarantee, but it can usually be indicative of a pattern)

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    what if change the equation of the space to make it like this :

    Code:
    for(space=0;space<=row;space++)
    {
        printf(" ");
    }

    so what do u think ?

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Don't you have a compiler under which you can compile and then test your code?

    It'll make your development process a whole lot easier (on us and you) and quicker if you can just test your code to see if it works or not.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    well i tested it and it works its just that i tested the first code and it still worked and printed the same triangle but u said that the first code is incorrect , thats why im asking

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by everyone0 View Post
    well i tested it and it works its just that i tested the first code and it still worked and printed the same triangle but u said that the first code is incorrect , thats why im asking
    Really? It works? Try with values other than 5. It won't work. Also, find out how many spaces you have in front of each line that shouldn't be there. And how many blank lines on the bottom that shouldn't exist.

  12. #12
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    *sigh*.
    This is exactly the same problem you had yesterday at can someone help me with this loop ? except you need to print the rows in reverse.

    So just use the same thing, but reverse your "row" loop. If you don't know how,
    Code:
    /* A forwards loop */
    for (i=0; i<M; ++i)
    {
    }
    
    /* A "reverse" loop */
    for (i=M-1; i>=0; --i)
    {
    }
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    i mean it works after correcting it , and the teacher who teaches me c told me blanks is ok , the program wouldnt care if u leave blanks or u dont, in most of the cases .

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by everyone0 View Post
    i mean it works after correcting it , and the teacher who teaches me c told me blanks is ok , the program wouldnt care if u leave blanks or u dont, in most of the cases .
    Well, maybe if you did anything anyone in this thread said, someone would still bother to help you. But so far, I haven't seen anything from your part but some buggy code you fail to accept is buggy.

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    lol i do accept that my code is buggy im not in denial . here is admit "My Code Is Buggy".

    thanks alot NeonBlack u just made my life easier with the reverse loop idea : here is my code after using it and after taking the advice of all the other programmers who are here :

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int  N,row, column,space;
    printf("Enter the triangle-drawing value n:");
    scanf("%d",&N);
    for (row=N-1;row>=0; row--)
    {
    for (space=0;space<=N-1-row;space++)
    {
    printf(" ");
    }
    for (column=1;column<=2*row+1;column++)
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM