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