I want to extend the following program using mandelbrot. Here are the requirments: Let’s extend HW6 to include mandelbrot.c, which implement the recursive function "complex_t mandelbrot(int n)". In...

I want to extend the following program using mandelbrot. Here are the requirments:

Let’s 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 whethercbelongs 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 variablecin 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 computer’s 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 currentcis 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 pointcin 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.06175to 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.










#include #include #include
struct complex{int real, img;};
// to read complex number
void scan_complex(struct complex *a){//reading inputprintf("Enter a and b where a + ib is the complex number.");printf("\na = ");scanf("%d", &a->real);printf("b = ");scanf("%d", &a->img);
}
//method to print complex number
void print_complex(struct complex *a)
{
printf(" %d + %di", a->real, a->img);
}
//method to add two complex numbers
struct complex * add_complex(struct complex *a,struct complex *b)
{
struct complex *c = (struct complex *)malloc(sizeof(struct complex));
c->real = a->real + b->real;
c->img = a->img + b->img;
return c;
}
//method to multiply two complex numbers
void multiply_complex(struct complex *a,struct complex *b)
{
struct complex c;
//multiplying
c.real = a->real*b->real - a->img*b->img;
c.img = a->img*b->real + a->real*b->img;
if (c.img >= 0)
printf("Multiplication of the complex numbers = %d + %di", c.real, c.img);
else
printf("Multiplication of the complex numbers = %d %di", c.real, c.img);
}
//method to find addition of c , n times
struct complex* f(int n,struct complex *c)
{
if(n==0)
return c;
return add_complex(c,f(n-1,c));
}
//to find absolute
float abs_complex(struct complex c)
{
return sqrt(c.real*c.real + c.img *c.img);
}
int main()
{
//testing
struct complex a, b;
scan_complex(&a);
scan_complex(&b);
printf("absolute of : ");
print_complex(&a);
printf("%f\n",abs_complex(a));
printf("\n");
print_complex(&a);
printf(" + ");
print_complex(&b);
printf(" = ");
struct complex *c =add_complex(&a,&b);
print_complex(c);
printf("\n");
multiply_complex(&a,&b);
printf("\n");
struct complex *d = f(3,&a);
print_complex(d);
printf("\n");
return 0;
}

Apr 17, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here