Output required

This is a discussion on Output required within the C Programming forums, part of the General Programming Boards category; Hello all, Well i wanted a output of a prorgram to look like this, i tried but can't figured out ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Exclamation Output required

    Hello all,

    Well i wanted a output of a prorgram to look like this, i tried but can't figured out the logic, can anyone plesae help me out here with this

    1
    232
    34543
    4567654
    567898765

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Effort required.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    yes mate i tried from my side, but needed some expert here

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    One way to tackle it is to think about the shape first. You know its a triangle so its going to need for loops, more than likely nested loops. Within each increment of the inner loop calculate/print the number.

    If you are having trouble, think about the pescudo code first, or plan it out on paper.
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    thankz for your suggesetion M8, i have figured it out that there will be nested looping in this, but can't get to the logic of looping

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    These types of things are interesting. Look at this table:

    (0 represents space or padding)

    000010000
    000232000
    003454300
    045676540
    567898765

    What do we need to know. We need to know how much padding or space to provide, how many true digits to print, and what those digits should be.

    1. The "row" is going to go from 1 to 5;
    2. The "pad" is going to go from 4 to 0;
    3. The maximum value in the row is equal to the total number of digits printed.

    Look at this table:

    row|pad|max
    1 4 1
    2 3 3
    3 2 5
    4 1 7
    5 0 9

    What can we learn:

    1. pad = 5 - row;
    2. max = (2 * row) -1;

    Here is how to do it, step by step.

    Code:
    /* Outer loop */
    for(row = 1; row <= 5; ++ row){
    
    /* The pad loop. notice that as row increments the number of spaces printed decrements */
    
      for(pad = row; pad < 5; ++ pad)
        printf(" ");
    
    /* Now we are going to print the digits. This requires two loops. One that increments to "max", and one decrements back to "row */
    
      max = (2 * row) -1;
      for(col = row; col <= max; ++ col)
        printf("%d",col);
      for(col = max - 1; col >= row; -- col)
        printf("%d",col);
    
    /* Finish with a newline */
      printf("\n");
    
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok... well think of the math involved a little more. Think about the fact that the first number is going to be centered and the space on each side is going to be equal (which isn't necessarily as relavant in this project since you only need to write spaces before your data, not the spaces afterward.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Smile

    Quote Originally Posted by rmetcalf View Post
    These types of things are interesting. Look at this table:

    (0 represents space or padding)

    000010000
    000232000
    003454300
    045676540
    567898765

    What do we need to know. We need to know how much padding or space to provide, how many true digits to print, and what those digits should be.

    1. The "row" is going to go from 1 to 5;
    2. The "pad" is going to go from 4 to 0;
    3. The maximum value in the row is equal to the total number of digits printed.

    Look at this table:

    row|pad|max
    1 4 1
    2 3 3
    3 2 5
    4 1 7
    5 0 9

    What can we learn:

    1. pad = 5 - row;
    2. max = (2 * row) -1;

    Here is how to do it, step by step.

    Code:
    /* Outer loop */
    for(row = 1; row <= 5; ++ row){
    
    /* The pad loop. notice that as row increments the number of spaces printed decrements */
    
      for(pad = row; pad < 5; ++ pad)
        printf(" ");
    
    /* Now we are going to print the digits. This requires two loops. One that increments to "max", and one decrements back to "row */
    
      max = (2 * row) -1;
      for(col = row; col <= max; ++ col)
        printf("%d",col);
      for(col = max - 1; col >= row; -- col)
        printf("%d",col);
    
    /* Finish with a newline */
      printf("\n");
    
    }
    tons of thankz M8, for explaining in such an easy manner, great work

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Smile

    Quote Originally Posted by master5001 View Post
    Ok... well think of the math involved a little more. Think about the fact that the first number is going to be centered and the space on each side is going to be equal (which isn't necessarily as relavant in this project since you only need to write spaces before your data, not the spaces afterward.
    great info M8, i will surely try this one and let u know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. output display in screen required
    By imagetvr in forum C++ Programming
    Replies: 9
    Last Post: 08-19-2008, 09:55 PM
  2. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  3. help with output
    By s_ny33 in forum C Programming
    Replies: 20
    Last Post: 09-19-2005, 11:51 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

Tags for this Thread


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