I'm using a double for-loop in order to check every point (coordinate pair) in a rectangular area from (-2.0, -1.12) to (0.47, 1.12) to see whether it belongs to the Mandelbrot set. If it does, I want to print a 1. Likewise, if it does not, I want to print a 0. The basic idea is to print, line by line, an array of characters that displays a simplified Mandelbrot set.
This is my main function:
#include <stdio.h>
#include "complex.h"
#include "mandelbrot.h"
#define STEP_X 0.06175
#define STEP_Y 0.07466
int main(void){
int i = 0;
char arr[50];
complex_t c, abs, max;
max.real = 10000;
max.imag = 0;
for (c.imag = -1.12; c.imag <= 1.12; c.imag += STEP_Y){
for (c.real = -2.0; c.real <= 0.47; c.real += STEP_X){
abs = abs_complex(mandelbrot(c,15));
if (abs.real < max.real){
arr[i] = 1;
i++;
}
else{
arr[i] = 0;
i++;
}
}
printf("%s", arr);
i = 0;
}
}
The program compiles just fine, but does not produce an output. I know I must not be printing the array the right way, but for the life of me I can not figure out how to do it.
Any feedback, hints, or tips would be greatly appreciated.
Thanks in advance!
The problems you are having are two-fold. (1) you are copying decimal values to arr
(e.g. 0
and 1
) instead of ASCII characters ('0'
and '1'
). Decimal 0
and 1
are non-printable. Ironically decimal 0
is the nul-terminating character, so if if (abs.real >= max.real)
for i == 0
arr
holds the empty-string.
Second you call printf
without having insured the final character is the nul-terminating character. (you can do this by default by initializing char arr[MAXC] = "";
and insuring your loop is limited to i + 1 < 50 && c.real <= 0.47
or you can simply affirmatively terminate arr
with arr[i] = 0;
before calling i = 0;
(or move your declaration of i
inside the first for
loop and initialize).
This is untested (I don't have your local headers), but it looks like you intended:
#include <stdio.h>
#include "complex.h"
#include "mandelbrot.h"
#define MAXC 50
#define STEP_X 0.06175
#define STEP_Y 0.07466
int main(void){
complex_t c, abs, max;
max.real = 10000;
max.imag = 0;
for (c.imag = -1.12; c.imag <= 1.12; c.imag += STEP_Y) {
int i = 0; /* declare/initialize i & arr here */
char arr[MAXC] = ""; /* set to all zero */
for (c.real = -2.0;
i + 1 < MAXC && c.real <= 0.47; /* limit to 49 chars max */
c.real += STEP_X) {
abs = abs_complex (mandelbrot (c,15));
if (abs.real < max.real)
arr[i++] = '1'; /* assign character '1' */
else
arr[i++] = '0'; /* assign character '0' */
}
arr[i] = 0; /* nul-terminate line */
printf ("%s\n", arr); /* output line */
}
return 0;
}
Give it a try and let me know if you have further questions.