0

on the table, the n value should fit with the line of the value fibo with spaces.

printf("n | ");  

for (i = 1; i < = n ; i++)
{
    printf("%d", i);

    for (wert=0; wert < = (hochn(fibo(i)) - hochn(i)) ; wert++)
    {
        printf(" ");
    }
}

the other functions 'hochn' finds the value of the power of 10.

int hochn (int b)
{
    int tmp;

    for(tmp =0; b > 10 ; tmp++)
    {
        b = (b / 10); 
    }

    return tmp;
}

additionally, 'fibo' finds the value of fibonacci number.

the actual run of the program looks like:

n | 1 2 3 4 5 6 7  8  9  10  11 12  13  14  
--------------------------------------------
f | 1 1 2 3 5 8 13 21 34 55 89 144 233 377

why is there only 1 space for n=11 and returns to 2 spaces again in n = 12 ?

John Odom
  • 1,189
  • 2
  • 20
  • 35
TeaMan
  • 3
  • 1
  • 1
    Hello, welcome to StackOverflow! Have you tried debugging through your code first to see if you could find the answer? – John Odom Dec 24 '14 at 00:05
  • 3
    `b > 10` --> `b >= 10`. also `< =` --> `<=` – BLUEPIXY Dec 24 '14 at 00:34
  • Using `printf(" ");` instead of `putchar(' ');` is valid but a tad clumsy. The output is as it is because `hochn()` produces the wrong answer. For example: `0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 1, 12: 1, 13: 1, … 97: 1, 98: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 107: 1, 108: 1, 109: 1, 110: 2, 111: 2, …`. That's easily shown by running a simple test that counts from 0 to 120 or whatever. Fixing it so it works for 0 as well as larger numbers is subtle: `for (tmp = 1; (b /= 10) > 0; tmp++) ;` works. Learn how to test trivial code. – Jonathan Leffler Dec 24 '14 at 01:23

1 Answers1

0

The problem is that a simple test of the values returned by your hochn() function shows that it yields the wrong answers. Your version is hochn0() in the code below; a fixed version is hochn1(). I've included test code for the layout too. Note that your printing of the heading line puts spaces after the number, rather than before, which complicates things too, and leaves trailing spaces at the end of the line. You might also note that your problems start with 7 rather than 11.

In the code below, I made the functions static because the compiler options I use otherwise demand a declaration of the functions before they're defined.

#include <stdio.h>

static int hochn0(int b)
{
    int tmp;

    for (tmp = 0; b > 10; tmp++)
        b = (b / 10);

    return tmp;
}

/* hochn1() - return int(log10(b)) - 1, and 0 for b <= 0 */
static int hochn1(int b)
{
    int tmp;

    for (tmp = 0; (b /= 10) > 0; tmp++)
        ;

    return tmp;
}

static int fibo(int n)
{
    int f0 = 0;
    int f1 = 1;
    for (int i = 0; i < n - 1; i++)
    {
        int fn = f0 + f1;
        f0 = f1;
        f1 = fn;
    }
    return f1;
}

int main(void)
{
    for (int i = 0; i < 120; i++)
    {
        int h0 = hochn0(i);
        int h1 = hochn1(i);
        if (h0 != h1)
            printf("%3d: hochn0 = %d, hochn1 = %d\n", i, hochn0(i), hochn1(i));
    }

    int n = 14;

    /* Broken - print spaces after number */
    printf("n | ");
    for (int i = 1; i <= n; i++)
    {
        printf("%d", i);
        for (int wert = 0; wert <= (hochn0(fibo(i)) - hochn0(i)); wert++)
            putchar(' ');
    }
    putchar('\n');

    /* Fixed - print spaces before number */
    printf("n |");
    for (int i = 1; i <= n; i++)
    {
        for (int wert = 0; wert <= (hochn1(fibo(i)) - hochn1(i)); wert++)
            putchar(' ');
        printf("%d", i);
    }
    putchar('\n');

    /* Succinct fixed */
    int count = 0;
    count += printf("n |");
    for (int i = 1; i <= n; i++)
        count += printf("%*s%d", (hochn1(fibo(i)) - hochn1(i)) + 1, " ", i);
    putchar('\n');

    for (int i = 0; i < count; i++)
        putchar('-');
    putchar('\n');

    /* Result line */
    printf("f |");
    for (int i = 1; i <= n; i++)
        printf(" %d", fibo(i));
    putchar('\n');

    return 0;
}

Example output:

The first section identifies numbers where hochn0() and hochn1() produce different answers. The last few lines show how the output appears, in the original and a couple of revisions.

 10: hochn0 = 0, hochn1 = 1
100: hochn0 = 1, hochn1 = 2
101: hochn0 = 1, hochn1 = 2
102: hochn0 = 1, hochn1 = 2
103: hochn0 = 1, hochn1 = 2
104: hochn0 = 1, hochn1 = 2
105: hochn0 = 1, hochn1 = 2
106: hochn0 = 1, hochn1 = 2
107: hochn0 = 1, hochn1 = 2
108: hochn0 = 1, hochn1 = 2
109: hochn0 = 1, hochn1 = 2
n | 1 2 3 4 5 6 7  8  9  10  11 12  13  14  
n | 1 2 3 4 5 6  7  8  9 10 11  12  13  14
n | 1 2 3 4 5 6  7  8  9 10 11  12  13  14
------------------------------------------
f | 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278