Question: This project need to write in C include: complex.c complex.h main.c mandelbrot.c mandelbrot.h I already have complex.c main.c complex.h. I just need to define mandelbrot.c

This project need to write in C include: complex.c complex.h main.c mandelbrot.c mandelbrot.h

I already have complex.c main.c complex.h. I just need to define mandelbrot.c mandelbrot.h

This project is to practice multiple components compilation, recursion, and structure.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The Mandelbrot Set The Mandelbrot set is a set of all complex numbers c that satisfy the following condition: the absolute value of mandelbrot(n) remains bounded when n (a natural number 0, 1, 2, ...) tends to infinity, where

mandelbrot(n) = mandelbrot(n-1)*mandelbrot(n-1) + c; mandelbrot(0) = c.

Here the addition and multiplication are complex number addition and multiplication.

Specifically, for our project, if the absolute value of mandelbrot(15), |mandelbrot(15)|, is smaller than 10000 for a given c, then c is in the Mandelbrot set. If we consider a c as a 2D coordinates on a plane, the points in the rectangular area from (-2.0, -1.12) to (0.47, 1.12) will contain the Mandelbrot set. Our goal is to calculate an array of points that covers the Mandelbrot set in this area and display the array. If a point is in the Mandelbrot set, we display a 1 (or #); otherwise, we display a 0 (or an empty space). You may save the area in a file so the area may contain many more points beyond your display.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Implementation details

1) If you havent completed HW6, you will need to finish it without any problem before starting this project. We need to use the complex number addition, multiplication, and absolute value functions in this project.

2) Lets extend HW6 to include mandelbrot.c, which implement the recursive function "complex_t mandelbrot(int n)". In general, you should avoid using global variables, so the recursive function has to carry another input value, c, and mandelbrot(c, n) checks whether c belongs to the Mandelbrot set. In other words, your function should be mandelbrot(complex_t c, int n) instead. Optionally, for the purpose of learning, you may define a global variable c in main.c, and use extern in mandelbrot.h to make it visible in mandelbrot.c, so you can just use mandelbrot(n).

3) mandelbrot(n) uses complex number addition and multiplication, so you should include "complex.h" in mandelbrot.c to allow mandelbrot(n) to access complex functions. In the recursive function, |mandelbrot(n)| can be quite large beyond the computers representation. Therefore, in the recursion, if |mandelbrot(n-1)| is bigger than 10000, your recursion should consider that it is not bounded already, so you return mandelbrot(n) = (10000, 10000) instead of the calculated number to indicate that current c is not in the Mandelbrot set. Also, in the recursion, mandelbrot(n-1) is calculated twice, which is really wasting computing effort, so you should just calculate it once for efficiency.

4) In order for main.c to see the recursive function, you need to include "mandelbrot.h". Because you use complex numbers in main.c, you need to include "complex.h" in main.c as well. Because mandelbrot function uses structure, so you should have "complex.h" included ahead of "mandelbrot.h".

In main.c, for all point c in the area from (-2.0, -1.12) to (0.47, 1.12), we can check the corresponding mandelbrot(15) to see if it is in the Mandelbrot set. If it is in the set, which means that |mandelbrot(15)| is smaller than 10000, we print a '1' or '#'; otherwise, we print a '0' or ' '(an empty space). Therefore, we can actually print an image of the Mandelbrot set. For example, you can check 40*30 points with a double for-loop in a rectangular area. That is, we start in x direction from -2.0 with step size (0.47-(-2))/40 = 0.06175 to 0.47, and in y direction from -1.12 with step size (1.12-(-1.12))/30 = 0.077 to 1.12. You may notice that this arrangement will be an up-side-down image, but the image is symmetric, so we can ignore this problem. You can google images on Mandelbrot, and see the Mandelbrot set images with very fine points at the screen resolution with vivid colors.

If you want, as an option, which is not required for this project, you may save the image in a two dimensional array of characters first. Better yet, you can save this image in a file, so you can check out a much larger image in the file with many more points.

The final executable is called mandelbrot. Make sure your Makefile will compile and generate the three object files, and link them together with correct dependencies.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

complex.c

#include

#include "complex.h"

#include "mandelbrot.h"

#include

//Define a method "scan_complex()"

int scan_complex(complex_t *c) {

int status;

status = scanf("%lf%lf", &c->real, &c->imag);

if (status == 2)

status = 1;

else if (status != EOF)

status = 0;

return (status);

}

//method "print_complex()"

void print_complex(complex_t c) {

// variables

double a, b;

char sign;

a = c.real;

b = c.imag;

printf("(");

if (fabs(a) < .005 && fabs(b) < .005) {

printf("%.2f", 0.0);

}

else if (fabs(b) < .005) {

printf("%.2f", a);

}

else if (fabs(a) < .005){

printf("%.2fi", b);

}

else

{

if (b < 0)

sign = '-';

else

sign = '+';

printf("%.2f %c %.2fi", a, sign, fabs(b));

}

printf(")");

}

//Define a method "add_complex()"

complex_t add_complex(complex_t c1, complex_t c2) /* input - values to add */ {

complex_t csum;//Declare number

csum.real = c1.real + c2.real;

csum.imag = c1.imag + c2.imag;

return (csum);

}

//Define a method "subtract_complex()"

complex_t subtract_complex(complex_t c1, complex_t c2) {

complex_t cdiff;

cdiff.real = c1.real - c2.real;

cdiff.imag = c1.imag - c2.imag;

return (cdiff);

}

//method "multiply_complex()"

complex_t multiply_complex(complex_t c1, complex_t c2){

complex_t cmul;

cmul.real = (c1.real * c2.real)-(c1.imag*c2.imag);

cmul.imag=(c1.real*c2.imag)+(c1.imag*c2.real);

// printf("Function multilply_complex returning first argument "); //

return (cmul);

}

// method "divide_complex()"

complex_t divide_complex(complex_t c1, complex_t c2) /* input parameters */ {

complex_t cdiv;

cdiv.imag = ((c1.imag * c2.real) - (c1.real * c2.imag)) / ((c1.real * c2.real) + (c2.imag * c2.imag));

cdiv.real = ((c1.real * c2.real) + (c1.imag * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag));

// printf("Function divide_complex returning first argument "); //

return(cdiv);

}

//method "abs_complex()"

complex_t abs_complex(complex_t c) {

complex_t cabs;

cabs.real = sqrt((c.real * c.real) + (c.imag * c.imag)) ;

//* ((c.real * c.real) + (c.imag * c.imag));

cabs.imag = 0;

return (cabs);

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

complex.h

//Define a structure

typedef struct

{

//Declare member variables

double real;

double imag;

} complex_t;

int scan_complex(complex_t *c); //Define a method "scan_complex()"

void print_complex(complex_t c); //Define a method "print_complex()"

complex_t add_complex(complex_t c1, complex_t c2);//Define a method "add_complex()"

complex_t subtract_complex(complex_t c1, complex_t c2);//Define a method "subtract_complex()"

complex_t multiply_complex(complex_t c1, complex_t c2);//Define a method "multiply_complex()"

complex_t divide_complex(complex_t c1, complex_t c2);//Define a method "divide_complex()"

complex_t abs_complex(complex_t c);//Define a method "abs_complex()"

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

main.c

#include

#include "complex.h"

#include"mandelbrot.h"

int main(void) {

complex_t com1, com2;

printf("Enter the real and imaginary parts of a complex number ");

printf("separated by a space> ");

scan_complex(&com1);

printf("Enter a second complex number> ");

scan_complex(&com2);

printf(" ");

print_complex(com1);

printf(" + ");

print_complex(com2);

printf(" = ");

print_complex(add_complex(com1, com2));

printf(" ");

print_complex(com1);

printf(" - ");

print_complex(com2);

printf(" = ");

print_complex(subtract_complex(com1, com2));

printf(" ");

print_complex(com1);

printf(" * ");

print_complex(com2);

printf(" = ");

print_complex(multiply_complex(com1, com2));

printf(" ");

print_complex(com1);

printf(" / ");

print_complex(com2);

printf(" = ");

print_complex(divide_complex(com1, com2));

printf(" |");

print_complex(com1);

printf("| = ");

print_complex(abs_complex(com1));

printf(" ");

//system("pause");

return (0);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!