Thread: Wish to create a pattern of numbers using loops

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    7

    Unhappy Wish to create a pattern of numbers using loops

    Hello,

    I wish to create the following patterns with numbers.



    1

    2 6

    3 7 10

    4 8 11 13

    5 9 12 14 15

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nothing's stopping you.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sounds good, so go ahead and do it
    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

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    7

    Question I just can't code it

    Hello I just can't code it
    can anyone please help me to write its code?

    I have just written
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int i=0,base=0,j=0,p=0,diff=0;
    printf("Enter the value of base: ");
    scanf("%d",&base);
    for(i=1;i<=base;i++)
    {
    printf("%d\t",i);
    //getch();
    for(j=1;j<=i-1;j++)
    {
    do
    {
    diff=base-j;
    p=p+diff+i;
    printf("%d\t",p);
    j++;
    }while (j<=i-1);
    printf("%d\t",p);
    //getch();
    }
    printf("\n");
    }
    getch();
    }
    but this is not working

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Nothing's stopping you.
    I just cant do that
    please help me

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should look at the triangle you wish to get again. The method of getting from column x to column x+1 is way way way way easier than you seem to think it is, based on the code. (EDIT: Or maybe you are just trying to use p in a couple different and incompatible ways. It's not clear, because hey, you could also comment your code to say what you're trying to do.) Also you have a do-while loop inside a for loop, which is not wrong in and of itself, but in this case is just silly.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Sounds good, so go ahead and do it
    Quote Originally Posted by tabstop View Post
    You should look at the triangle you wish to get again. The method of getting from column x to column x+1 is way way way way easier than you seem to think it is, based on the code. (EDIT: Or maybe you are just trying to use p in a couple different and incompatible ways. It's not clear, because hey, you could also comment your code to say what you're trying to do.) Also you have a do-while loop inside a for loop, which is not wrong in and of itself, but in this case is just silly.
    Hey please can you write the code for me?
    I just can't get it.

    Yet I have developed the code but want to make some changes that it have a lot of variables, can you please cut down?

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i,j,c,b,p=0;
    clrscr();
    printf("enter the name");
    scanf("%d",&b);
    for(i=1;i<=b;i++)
    {
    printf(" %d",i);
    c=b-1;
    p=c+i;
    for(j=2;j<=i;j++)
    {
    printf(" %d",p);
    c=c-1;
    p=p+c;
    }
    p=0;
    printf("\n");
    }
    getch();
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bharat_kaushik View Post
    Hey please can you write the code for me?
    I just can't get it.
    Can, but won't.

    You can probably get away with one less variable in your code, but there's no reason for you to do so, as each variable that you have has a nice meaning -- of course it's a little bit of work to figure out the meanings with names like "c" and "p" and the like.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bharat_kaushik
    Hey please can you write the code for me?
    I just can't get it.

    Yet I have developed the code but want to make some changes that it have a lot of variables, can you please cut down?
    That is... weird. You say that you "just can't get it", yet you have written a program that appears to do what you wanted to do.

    My suggestions are:
    • Get rid of the unnecessary inclusion of <conio.h> and use of the non-standard clrscr() and getch().
    • Change the return type of main to int.
    • Instead of asking for a name, ask for something more sensible.
    • Indent your code.
    • Declare variables at the start of the first block in which they are used.
    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

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    okey ya now I got the solution for my problem, just wish if anyone have any other solution using arrays or something else then please write for me.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    You can probably get away with one less variable in your code, but there's no reason for you to do so, as each variable that you have has a nice meaning -- of course it's a little bit of work to figure out the meanings with names like "c" and "p" and the like.
    Personally, I feel that c really should be removed, with b renamed to num_rows and p renamed to sum.
    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

  12. #12
    Registered User xerxes212's Avatar
    Join Date
    Aug 2009
    Location
    Philippines
    Posts
    2

    Smile How did you do this?

    being just a newbie here. you are really good man. We haven't undergone looping yet in our programming. I may not be able to help you. I just want to express my amazement on your thread.

  13. #13
    Registered User manojthesingham's Avatar
    Join Date
    Sep 2009
    Location
    Coimbatore
    Posts
    2
    Code:
    /* 
        This type of printing can be done using simple mathematical induction
        r&c
        c =  |   0       1         2         3         4
    ______________________________
        r 1   |  1     
               |
          2   |  2    (2+4) 
               |  
          3   |  3    (3+4)  (7+3)
               |
          4   |  4    (4+4)  (8+3) (11+2)  
               | 
          5   |  5    (5+4)  (9+3)  (12+2)  (14+1)
         
           for(r = 1 to 4)
             c = 0;
             p = r;         
            while(c<r) // for upper triangle
             c++
             p = p + (5-c)         
    */
    
      #include<stdio.h>
       main()
       {
         int r,c,p;
         for(r=1;r<5;r++)
         {
            for(c=0,p=r;  c<r ; c++,p=p+(5-c))
              printf("%d\t",p);
            printf("\n");
         } 
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  2. Creating a UDF to create random numbers
    By rgmills in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2005, 11:22 PM
  3. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  5. strange numbers, while loops questionable?
    By exluddite in forum C++ Programming
    Replies: 8
    Last Post: 05-06-2004, 11:11 AM