Zack
Zack

Reputation: 13

Recursive Binary Tree

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.

Upvotes: 0

Views: 476

Answers (2)

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23342

Your array is a local variable in main, and you never initialize most of its values. Local variables that are not explicitly initialized start out containing "random garbage" -- which is not really "random", but is "garbage" enough that you can't be sure some of the positions do not accidentally contain the value 'X' initially.

Start by looping over the entire array and initializing all positions to something known.

(Nitpicking language lawyers will know that "random garbage" is not the technically correct term for what happens with unitialized locals, but it is close enough to work as a mental model in practical programming).

Upvotes: 2

Brendan Long
Brendan Long

Reputation: 54312

char array[lines][LENGTH];

This creates an empty array, where every value is whatever is currently in memory (which is sometimes 0, but not guaranteed to be). This means that sometimes, at random, the memory will already contain an "X" byte. You can solve this by initializing the array to be full of 0's (that's the null character, not '0'):

memset(array, 0, LENGTH * lines);

Or:

for(size_t i = 0; i < lines; i++){
    for(size_t j = 0; j < LENGTH; j++){
        a[i][j] = 0;
    }
}

Upvotes: 3

Related Questions