Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help required (http://www.go4expert.com/forums/help-required-t13944/)

internet_bug 16Sep2008 22:44

Help 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 1

1 2 1

1 3 3 1

1 4 6 4 1

xpi0t0s 17Sep2008 13:40

Re: Help required
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.

internet_bug 19Sep2008 08:42

Re: Help required
thankz a millions mate by your help i finally created the programs

All times are GMT +5.5. The time now is 11:10.