Help required

Discussion in 'C' started by internet_bug, Sep 16, 2008.

  1. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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

    and

    1

    1 1

    1 2 1

    1 3 3 1

    1 4 6 4 1 ​
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    For the first one, each line has some padding that relates to the line length so you'll need to know in advance how many lines you want to display. Line N starts with number N so that part's easy. Also each line counts up N numbers starting at N, then counts back down to N, so again that should be easy.

    Often it's easier to write a program that outputs just a single line first, then adapt that program to print multiple lines. So can you write a program that just outputs "34543", bearing in mind that you don't just want to do printf("34543") but to generate the output just from a single starting variable N.

    Once you've got that step sorted and working for any initial value of N, adapt the program to use a second variable MAX which would represent the total number of lines to be displayed, and generate the padding, which if the number of lines is 5 would contain 4 spaces for line 1, 3 spaces for line 2 and so on. Work out an expression involving MAX and N that gives you that number of spaces, then output those spaces in a loop that prints a single space on each iteration (or if you want to be clever just use printf to select the number of characters from "<lots of spaces>" where this string is >=MAX characters long).

    Then it's just a simple case of changing "int N=3;" to "for (N=1; N<=MAX; N++) { ... }".

    The second example can work along the same lines, if you can think of a generator function that will generate each number for a given line. Or just store the previous line; each number is the sum of the two numbers above it (4=1+3, 6=3+3 etc, and if there isn't a number, say to the top left of each 1 on the left, just assume there's a zero there).

    Again you may need to know how many lines you'll print in advance, if you use the storage method then that will determine how long your previous line array is. Use alternating odd and even storage, so you can drop the formula current_line[x]=previous_line[x-1]+previous_line[x+1] straight in. Each line N has N values on it, so the length of the storage is easily calculated from N (add 2 if you want to include the implied zeros so that you don't have to do any bounds checking for the above formula).

    So for example at line 4 of a 5 line display program, previous_line could contain the values
    0 0 0 1 0 2 0 1 0 0 0
    and from this we want to calculate the values:
    0 0 1 0 3 0 3 0 1 0 0

    The final line would add the first zero and the first 1 of these results, which is what I meant about not having to do bounds checking. If the first zero isn't there and the array starts at the second zero (i.e. 0 1 0 3 0 3 0 1 0) then current_line[0]=previous_line[-1]+previous_line[1] won't work, because you can't* refer to previous_line[-1].

    *You can use negative array indices in C, but only if you know what you're doing. int x[10]; int *y=&x[5]; Now y[-1] is perfectly valid and refers to x[4]. x[-1] is still invalid.
     
    Last edited: Sep 17, 2008
  3. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    thankz a millions mate by your help i finally created the programs
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice