How to array the numbers like a bowtie shape in c - c

I'm trying to arrange numbers in a nxn(n is odd number) matrix with bow tie shapes. (like fig.)
Trying to 5x5 matrix set coordinates but no result.
my code:
bowtie {
int a[5][5] = {{
0,
},
{
0,
},
{
0,
},
{
0,
},
{
0,
}};
int i, j;
int num = 1;
for (i = 0; i < 5; i++) {
if (i <= 2) // y>=0 - coordinate(2d)
{
for (j = i; j <= 2; j++) // x<=0, y>=0 Quadrant 2
{
a[i][j] = num;
num++;
}
for (j = 4 - i; j > i; j++) // Quadrant 1
{
a[i][j] = num;
num++;
}
} else // y<0
for (j = 4 - i; j <= 4 - i; j++) // Quadrant 3
{
a[i][j] = num;
num++;
}
for (j = i; j >= i; j++) // Quadrant 2
{
a[i][j] = num;
num++;
}
}
for (i = 0; i < 5; j++) {
for (j = 0; j < 5; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}

The loop
for (j = 4 - i; j > i; j++) // Quadrant 1
is wrong because j starts at 4 (i valuing 0) being the last valid index then never stop to grow producing an undefined behavior when you go out of the array
The loop
for (j = 4 - i; j <= 4 - i; j++) // Quadrant 3
is strange because the last possible value is the first one, so this is not a loop but just its body executed with j = 4 - i
The loop
for (j = i; j >= i; j++) // Quadrant 2
is like the first and makes j incompatible with the array dimensions
A proposal where the size in given in argument and can be odd or even :
#include <stdio.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int a[n][n];
int v = 0; /* the value 1.. to put in the cells */
int empty; /* the empty height */
int i,j;
/* first half and may be center */
empty = -1;
for (j = 0; j <= (n-1)/2; ++j) {
empty += 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = ++v;
}
if ((n & 1) == 0)
empty += 1;
/* second half */
for (; j < n; ++j) {
empty -= 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = ++v;
}
/* show result */
for (i = 0; i != n; ++i) {
for (j = 0; j != n; ++j) {
if (a[i][j] == 0)
fputs(" ", stdout); /* witdh = 4 compatible with a size up to 43 */
else
printf("% 4d", a[i][j]); /* width = 4 compatible with a size up to 43 */
}
putchar('\n');
}
}
}
return 0;
}
Compilation and executions :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall b.c
pi#raspberrypi:/tmp $ ./a.out 1
1
pi#raspberrypi:/tmp $ ./a.out 2
1 3
2 4
pi#raspberrypi:/tmp $ ./a.out 3
1 5
2 4 6
3 7
pi#raspberrypi:/tmp $ ./a.out 4
1 9
2 5 7 10
3 6 8 11
4 12
pi#raspberrypi:/tmp $ ./a.out 5
1 13
2 6 10 14
3 7 9 11 15
4 8 12 16
5 17
pi#raspberrypi:/tmp $ ./a.out 6
1 19
2 7 15 20
3 8 11 13 16 21
4 9 12 14 17 22
5 10 18 23
6 24
pi#raspberrypi:/tmp $ ./a.out 7
1 25
2 8 20 26
3 9 13 17 21 27
4 10 14 16 18 22 28
5 11 15 19 23 29
6 12 24 30
7 31
If you do not accept even size
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1) || ((n & 1) == 0))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int a[n][n];
int v = 1; /* the value 1.. to put in the cells */
int empty; /* the empty height */
/* first half more center */
empty = -1;
for (int j = 0; j <= n/2; ++j) {
int i;
empty += 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = v++;
}
/* second half */
for (int j = n/2 + 1; j < n; ++j) {
int i;
empty -= 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = v++;
}
/* show result */
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if (a[i][j] == 0)
fputs(" ", stdout);
else
printf("% 4d", a[i][j]);
}
putchar('\n');
}
}
}
return 0;
}
Compilation and executions :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall o.c
pi#raspberrypi:/tmp $ ./a.out
Usage ./a.out <size>
pi#raspberrypi:/tmp $ ./a.out 1
1
pi#raspberrypi:/tmp $ ./a.out 2
invalid size 2
pi#raspberrypi:/tmp $ ./a.out 3
1 5
2 4 6
3 7
pi#raspberrypi:/tmp $ ./a.out 5
1 13
2 6 10 14
3 7 9 11 15
4 8 12 16
5 17
pi#raspberrypi:/tmp $ ./a.out 17
1 145
2 18 130 146
3 19 33 117 131 147
4 20 34 46 106 118 132 148
5 21 35 47 57 97 107 119 133 149
6 22 36 48 58 66 90 98 108 120 134 150
7 23 37 49 59 67 73 85 91 99 109 121 135 151
8 24 38 50 60 68 74 78 82 86 92 100 110 122 136 152
9 25 39 51 61 69 75 79 81 83 87 93 101 111 123 137 153
10 26 40 52 62 70 76 80 84 88 94 102 112 124 138 154
11 27 41 53 63 71 77 89 95 103 113 125 139 155
12 28 42 54 64 72 96 104 114 126 140 156
13 29 43 55 65 105 115 127 141 157
14 30 44 56 116 128 142 158
15 31 45 129 143 159
16 32 144 160
17 161
pi#raspberrypi:/tmp $
From you remark
'int a[n][n];' has problem.
probably you compiled in C++ rather than C, but it is easy to change that :
replace int a[n][n]; by int * a = malloc(n*n*sizeof(int));
replace each form a[x][y] by a[(x)*n+y]
add a free(a); at the end
For instance if I do that on the proposal only accepting odd size :
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1) || ((n & 1) == 0))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int * a = malloc(n*n*sizeof(int));
int v = 1; /* the value 1.. to put in the cells */
int empty; /* the empty height */
/* first half more center */
empty = -1;
for (int j = 0; j <= n/2; ++j) {
int i;
empty += 1;
for (i = 0; i != empty; ++i)
a[i*n+j] = a[(n - i - 1)*n+j] = 0;
for (int k = n - empty*2; k; --k)
a[i++*n+j] = v++;
}
/* second half */
for (int j = n/2 + 1; j < n; ++j) {
int i;
empty -= 1;
for (i = 0; i != empty; ++i)
a[i*n+j] = a[(n - i - 1)*n+j] = 0;
for (int k = n - empty*2; k; --k)
a[i++*n+j] = v++;
}
/* show result */
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if (a[i*n+j] == 0)
fputs(" ", stdout);
else
printf("% 4d", a[i*n+j]);
}
putchar('\n');
}
free(a);
}
}
return 0;
}

Related

BINGO GAME in C , MATRIX WITH 3 Dimensions?

How can I generate some random cards with numbers, but I need manipulate them as a 3 dimenson matrix . [players][n][n] .. n = The dimenson
My program generates only one card, how can I generate more cards? The index of the cards will be set in the variable players( jogadores in the program) that is the first dimension of the Matrix
#include <stdio.h>
#include <stdlib.h>
int n, soma;
int jogadores;
int menu;
int
main()
{
srand(123);
printf("Número de jogadores: \n");
do {
scanf("%d", &jogadores);
} while (jogadores < 2 || jogadores > 10);
printf("Numero de jogadores salvo \n Escolha a dimensao das cartelas: \n");
do {
scanf("%d", &n);
} while (n < 2 || n > 9);
printf("\n Dimensao das cartelas salva \n ");
//printf(" %d %d \n ",jogadores,n);
int value = 10 * n;
// I need to add the dimensiona _jogadores_ ==> cartela [jogadores][n][n]
int cartela[n][n];
// Loop number of players
// for (int q = 0; q <=jogadores;q++)
// {
// Loop lines of the card
for (int i = 0; i <= n; i++) {
// Loop rows of the card
for (int j = 0; j < n; j++) {
do {
soma = 0;
// Colocar a dimensão jogadores
cartela[i][j] = rand() % value;
for (int l = 0; l < n; l++) {
for (int c = 0; c < n; c++) {
if (cartela[i][j] == cartela[l][c] && (i != l && j != c)) {
soma++;
}
}
}
} while (soma != 0);
}
}
// for (int j = 0; j < jogadores; j++)
// {
for (int l = 0; l < n; l++) {
for (int c = 0; c < n; c++) {
printf("\t %d", cartela[l][c]);
}
printf("\n");
}
// }
//while(1)
//{
//}
return 0;
}
That's my code, and like I said before, the biggest question is how can i use the 3 dimension matrix to get more cards.
A few things ...
The main issue is that you should break down the main into some separate functions.
That way, you can create a function that creates a card. Then, you can call it N times for the number of players.
The card creation you have could take a long time and is very slow. Better to fill the array linearly and then randomly swap cells. This guarantees uniqueness but is much faster.
Here's a refactored version:
#include <stdio.h>
#include <stdlib.h>
int n, soma;
int jogadores;
int menu;
void
card_print(int n,int card[n][n],const char *reason)
{
printf("\n");
printf("%s:\n",reason);
for (int irow = 0; irow < n; ++irow) {
for (int icol = 0; icol < n; ++icol)
printf(" %2d",card[irow][icol]);
printf("\n");
}
}
void
card_init(int n,int card[n][n])
{
int cardno = 0;
for (int irow = 0; irow < n; ++irow) {
for (int icol = 0; icol < n; ++icol)
card[irow][icol] = cardno++;
}
// randomly swap cells in card
int passmax = n * n;
for (int passno = 0; passno < passmax; ++passno) {
int irow = rand() % n;
int jrow = rand() % n;
int icol = rand() % n;
int jcol = rand() % n;
int tmp = card[irow][icol];
card[irow][icol] = card[jrow][jcol];
card[jrow][jcol] = tmp;
}
}
int
main()
{
srand(123);
printf("Número de jogadores: \n");
do {
scanf("%d", &jogadores);
} while (jogadores < 2 || jogadores > 10);
printf("Numero de jogadores salvo \n Escolha a dimensao das cartelas: \n");
do {
scanf("%d", &n);
} while (n < 2 || n > 9);
printf("\n Dimensao das cartelas salva \n ");
//printf(" %d %d \n ",jogadores,n);
//int value = 10 * n;
// I need to add the dimensiona _jogadores_ ==> cartela [jogadores][n][n]
int cartela[n][n];
card_init(n,cartela);
card_print(n,cartela,"Single");
int players[jogadores][n][n];
for (int iplayer = 0; iplayer < jogadores; ++iplayer) {
char msg[100];
card_init(n,players[iplayer]);
sprintf(msg,"Player %d",iplayer);
card_print(n,players[iplayer],msg);
}
return 0;
}
Here's the program output for 3 players and card dimension 7:
Número de jogadores:
Numero de jogadores salvo
Escolha a dimensao das cartelas:
Dimensao das cartelas salva
Single:
13 34 19 44 15 5 47
7 25 32 48 16 28 0
9 11 2 17 18 4 42
36 41 12 23 8 39 6
24 38 31 29 27 20 26
1 21 37 3 14 10 22
46 43 35 45 40 33 30
Player 0:
7 30 17 23 11 2 15
10 38 0 21 34 25 3
6 40 12 46 18 22 41
47 26 19 36 13 16 31
33 28 1 35 27 14 4
20 39 5 8 42 29 32
9 43 44 45 24 37 48
Player 1:
0 8 22 18 2 5 21
20 29 16 10 7 42 24
14 1 37 17 3 13 32
31 6 23 39 15 11 45
28 9 43 25 19 4 36
35 12 47 38 40 30 44
41 33 48 27 46 34 26
Player 2:
40 44 18 3 28 5 43
13 39 42 16 17 4 35
27 23 14 45 37 41 30
21 22 15 9 0 26 47
46 12 11 31 2 7 20
8 29 48 38 33 32 1
24 6 25 36 19 34 10

Sum of primes: what is the problem in result?

The code works fine but why are my answers wrong in the int result?
in output:
3
10
2 3 5 7: 17 //correct
30
2 3 5 7 11 13 17 19 23 29: 146 //incorrect
50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47: 474 //incorrect
Here is the code:
#include <stdio.h>
int main() {
int y, n, i, fact, j, result = 0;
scanf("%d", &y);
for (int x = 1; x <= y; x++) {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fact = 0;
for (j = 1; j <= n; j++) {
if (i % j == 0)
fact++;
}
if (fact == 2) {
result += i;
printf("%d ", i);
}
}
printf(": %d\n", result); //Not Getting correct answer please HELP!
}
return 0;
}
You forgot to initialize result before each calculation.
for(int x=1;x<=y;x++){
scanf("%d", &n);
result = 0; // add this for initialization
for (i = 1; i <= n; i++) {
/* ... */
}
/* ... */
}
The variable result is initialized only once
int y, n, i, fact, j, result = 0;
So it will accumulate values calculated in the loop
for(int x=1;x<=y;x++){
//...
}
Move the declaration of the variable result in the body of the loop
for(int x=1;x<=y;x++){
int result = 0;
//...
}
To avoid such an error you should declare variables in the minimum scope where they are used.
Also this loop
for (j = 1; j <= n; j++)
{
if (i % j == 0)
fact++;
}
does not make a great sense. Change the condition in the loop the following way
for (j = 1; j <= i; j++)
{
if (i % j == 0)
fact++;
}
substituting the variable n for the variable i.
Also you should use an unsigned integer type instead of the signed integer type int because prime numbers are defined for natural numbers.
The program can look for example the following way
#include <stdio.h>
int main(void)
{
unsigned int n = 0;
scanf( "%u", &n );
while ( n-- )
{
unsigned int max_value = 0;
scanf( "%u", &max_value );
unsigned long long int sum = 0;
for ( unsigned int i = 1; i <= max_value; i++ )
{
size_t count = 0;
for ( unsigned int j = 1; j <= i; j++ )
{
if ( i % j == 0 ) ++count;
}
if ( count == 2 )
{
printf( "%u ", i );
sum += i;
}
}
printf( ": %llu\n", sum );
}
return 0;
}
If to enter
3
10
20
100
then the output will be
2 3 5 7 : 17
2 3 5 7 11 13 17 19 : 77
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 : 1060

Bubble Sort Algorithm in C – Is this it?

I'm currently a CS50x student.
I've been toying around with the search and sorting algorithms. Trying to understand them in code. Now, on the topic of bubble sort: I created what I think is a bubble sort algorithm. But I couldn't quite fit in the idea of the swap count that needs to be set to non-zero value. My algorithm (below) does sort all numbers. Where would I fit in the swap idea though? If anyone could be so kind to explain I'd really appreciate it.
#import <stdio.h>
#import <cs50.h>
int main(void)
{
// Creating an unsorted array
int count = 10;
int array[count];
for (int z = 0; z < count; z++)
scanf("%i", &array[z]);
// Bubble Sort
int buffer;
for (int b = 0; b < count; b++)
{
int a = 0;
while (a < count)
{
if (array[a] > array[a+1])
{
buffer = array[a];
array[a] = array[a+1];
array[a+1] = buffer;
}
a++;
}
}
printf("Sorted: ");
for (int b = 0; b < count; b++)
printf("%i ", array[b]);
printf("\n");
}
The directive is #include, not #import. The idea of counting swaps is to break the outer loop if nothing is out of sequence (because there were no swaps needed in the inner loop). This code implements that:
#include <stdio.h>
int main(void)
{
// Creating an unsorted array
int count = 10;
int array[count];
for (int z = 0; z < count; z++)
scanf("%i", &array[z]);
putchar('\n');
printf("%8s:", "Unsorted");
for (int b = 0; b < count; b++)
printf(" %i", array[b]);
printf("\n");
// Bubble Sort
for (int b = 0; b < count; b++)
{
int a = 0;
int num_swaps = 0;
while (a < count - 1)
{
if (array[a] > array[a+1])
{
int buffer = array[a];
array[a] = array[a+1];
array[a+1] = buffer;
num_swaps++;
}
a++;
}
if (num_swaps == 0)
break;
}
printf("%8s:", "Sorted");
for (int b = 0; b < count; b++)
printf(" %i", array[b]);
printf("\n");
return 0;
}
Sample runs (source bs97.c compiled to bs97; home-brew random number generator random — the options used generate 10 numbers between 10 and 99 inclusive):
$ random -n 10 10 99 | bs97
Unsorted: 68 47 85 39 52 54 31 81 19 59
Sorted: 19 31 39 47 52 54 59 68 81 85
$ random -n 10 10 99 | bs97
Unsorted: 75 85 36 11 35 87 59 63 26 36
Sorted: 11 26 35 36 36 59 63 75 85 87
$ random -n 10 10 99 | bs97
Unsorted: 90 27 64 90 76 79 52 46 98 99
Sorted: 27 46 52 64 76 79 90 90 98 99
$ random -n 10 10 99 | bs97
Unsorted: 53 60 87 89 38 68 73 10 69 84
Sorted: 10 38 53 60 68 69 73 84 87 89
$
Note that the code avoids trailing blanks in the output.
You could also define int num_swaps = 1; outside the sorting for loop and test it in the main loop condition:
for (int b = 0; b < count - 1 && num_swaps > 0; b++)
{
num_swaps = 0;
and remove the if (num_swaps == 0) break; from the end of the loop. The inner loop could be a for loop too. And the first cycle of the outer loop moves the largest value to the end of the array, so you can shorten the inner cycle so it has less work to do. The printing code should be factored into a function, too.
#include <stdio.h>
static void dump_array(const char *tag, int size, int data[size])
{
printf("%8s (%d):", tag, size);
for (int i = 0; i < size; i++)
printf(" %i", data[i]);
printf("\n");
}
int main(void)
{
// Creating an unsorted array
int count = 10;
int array[count];
for (int z = 0; z < count; z++)
{
if (scanf("%i", &array[z]) != 1)
{
fprintf(stderr, "Failed to read number %d\n", z + 1);
return 1;
}
}
putchar('\n');
dump_array("Unsorted", count, array);
// Bubble Sort
int num_swaps = 1;
for (int b = 0; b < count - 1 && num_swaps > 0; b++)
{
num_swaps = 0;
for (int a = 0; a < count - b - 1; a++)
{
if (array[a] > array[a + 1])
{
int buffer = array[a];
array[a] = array[a + 1];
array[a + 1] = buffer;
num_swaps++;
}
}
}
dump_array("Sorted", count, array);
return 0;
}
Sample output:
$ random -n 10 10 99 | bs97
Unsorted (10): 31 82 81 40 12 17 70 44 90 12
Sorted (10): 12 12 17 31 40 44 70 81 82 90
$

To find and assign primes to an array

Hi I have made a simple program to print primes between 1 and 100 but I cannot figure a way to assign these values to an array of size 25 as we all know there are 25 primes between 1 and 100:
#include <stdio.h>
int main() {
int i, k;
for (i = 3; i < 100; i = i + 2) {
for (k = 2; k < i; k++) {
if (i % k == 0)
break;
}
if (i == k)
printf("%d\n", i);
}
}
Just create an array at the top, write to it, and then read out of it after you've found all your primes. Note that this could definitely be done more efficiently, but given the number of calculations you're doing, that's beside the point.
Code
#include<stdio.h>
int main() {
int primes[25];
int i, k;
int j = 0;
// find primes
for(i = 2; i < 100; i++) {
for (k = 2; k < i; k++) {
if (i % k == 0) {
break;
}
}
if (i == k) {
primes[j] = i;
j++;
}
}
// print primes
for (j = 0; j < 25; j++) {
printf("%d\n", primes[j]);
}
return 0;
}
Also note that 2 is prime, so you'll want to make sure that that's included in your output.
Output
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Make an array before you begin, then create a variable that increments while each prime is found. It might look something like this:
#include <stdio.h>
int main() {
int primes[25];
primes[0] = 2;
int count = 1;
for (int i = 3; i < 100; i += 2) {
int k;
for (k = 2; k < i; k++) {
if (i % k == 0) break;
}
if(i == k) {
primes[count] = i;
count++;
}
}
}
Warning: this is a humorous answer, not to be taken seriously.
Just like we all know there are 25 primes between 1 and 100, we might as well know the magic value to avoid using an array at all:
#include <stdio.h>
int main() {
long long magic = 150964650272183;
printf("2");
for (int i = 3; magic; magic >>= 1, i += 2)
magic & 1 && printf(" %d", i);
printf("\n");
return 0;
}
Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Spacing fibonacci chart corresponding to the length of fibonacci numbers in C

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 ?
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

Resources