The point is to print a binary tree such as:
-------x--------
---x-------x----
-x---x---x---x--
x-x-x-x-x-x-x-x-
xxxxxxxxxxxxxxxx
My code is:
#include <stdio.h>
#include <math.h>
#define LENGTH 16
void makeBranches(int left, int right, char a[][LENGTH], int);
void display(char a[][LENGTH], int);
void main(){
int i, lines;
double a;
a = log10(LENGTH*2)/log10(2);
lines = (int)a;
char array[lines][LENGTH];
makeBranches(0, LENGTH-1, array, 0);
display(array, lines);
}
void makeBranches(int left, int right, char a[][LENGTH], int line){
if(left >= right){
a[line][left] = 'X';
return;
} else{
a[line][(right+left)/2] = 'X';
makeBranches(left, (right+left)/2, a, line+1);
makeBranches((right+left)/2+1, right, a, line+1);
}
}
void display(char a[][LENGTH], int lines){
int i, j;
for(i = 0; i < lines; i++){
for(j = 0; j < LENGTH; j++){
if(a[i][j] == 'X')
printf("%c", a[i][j]);
else
printf("-");
}
printf("\n");
}
}
This works fine for LENGTH values of 4,8,16 but when you get to trying 32,64 etc. it has some stray X's. For example:
The LENGTH 32
---------------X----X-------X---
-------X---------------X--------
---X-------X-------X-------X----
-X---X---X---X---X---X---X---X--
X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The LENGTH 64
-------------------------------X--------------------------------
---------------X-------------------------------X----------------
-------X---------------X---------------X---------------X--------
---X-------X-------X-------X-------X-------X-------X-------X----
-X---X---X---X---X--XX---X--XX---X---X---X---X---X---X---X---X--
X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This has got to be a simple fix somewhere but I just cannot see it. Hopefully someone can.
Xs.array?