0
votes

can anyone tell me what is wrong in this code?

i am getting an error in the main function, when i am calling the 'file_write' method function inside the main function, it says 'y' is not declared in this scope, but actually it was an argument that i have passed it in the method function earlier.

is it a serious error?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

unsigned int width = 1500;
unsigned int height = 1500;
unsigned int max_iterations = 30000;
unsigned int **color = NULL;
double threshold = 4;
double min_re = -2.0;
double max_re = 1.0;
double min_im = -1.2;
double max_im = min_im+(max_re-min_re)*height/width;
double x_factor = (max_re-min_re)/(width-1);
double y_factor = (max_im-min_im)/(height-1);

int file_write(int x, int y, double min_re, double max_im, double x_factor, double y_factor)
{
    FILE *fractal = fopen("mandelbrot_imageSequential.ppm","w+");
    if(fractal != NULL)
    {
 fprintf(fractal,"P6\n");
     fprintf(fractal,"# %s\n", "Mandelbrot_imageSequential.ppm");
     fprintf(fractal,"%d %d\n", height, width);
     fprintf(fractal,"255\n");
 unsigned int R = 0, G = 0, B = 0;
 for(unsigned y = 0; y < height; ++y)
 {
            double c_im = max_im - y*y_factor;
    for(unsigned x = 0; x < width; ++x)
    {
            double c_re = min_re + x*x_factor;
            double Z_re = c_re, Z_im = c_im;
            bool image_inside = true;
         R = 0, G = 0, B = 0;
            for(unsigned n=0; n<max_iterations; ++n)
                 {
                    double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
                  if(Z_re2 + Z_im2 > threshold)
                       {
                          image_inside = false;
                G = n;
                if(G == 10)
                {
                    G = 25, R = 10, B = 5;
                }
                           break;
                          }
                          Z_im = 2 * Z_re * Z_im + c_im;
                          Z_re = Z_re2 - Z_im2 + c_re;
                 }
            if(image_inside)
        {
            putc(R, fractal);
            putc(G, fractal);
            putc(B, fractal);
        }
        else
        {
            putc(R, fractal);
            putc(G, fractal);
            putc(B, fractal);
        }
          }
}
fclose(fractal);
return 0;
}
}
int main(int argc, char *argv[])
{
if(argc != 9)
{
printf("There is an error in the input given.\n");
return 0;
}
else
{
    height = atoi(argv[1]);
    width = atoi(argv[2]);
max_iterations = atoi(argv[3]);
min_re = atof(argv[4]);
max_re = atof(argv[5]);
min_im = atof(argv[6]);
max_im = atof(argv[7]);
threshold = atoi(argv[8]);
}
color = (unsigned int**)malloc(height*sizeof(unsigned int*));
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tthreshold_value = %.2f\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,threshold);
int x;
for(x = 0; x < height; x++)
{
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
}
time_t ts,te;
time(&ts);
file_write(x,y,min_re,max_im,x_factor,y_factor);
time(&te);
double diff = difftime(te,ts);
printf("Total Time elapsed: %f\n",diff);
for(x = 0; x < height; x++)
{
   free(color[x]);
}
free(color);
return 0;
}
1
Depending on the conditions, this error may be the way the compiler is complaining about the fact you've a parameter named 'y' and the variable you use in your first for loop is also named 'y'. - Kaganar
I can see that there's a 'y' variable used in main(), but I don't see it declared anywhere. - SirDarius
actually i used x and y as a parameter in 'file_write function' does it make any change for using int variable x again in main function - visanio_learner
@SirDarius, but i declared 'y' in the 'file_write' function - visanio_learner
@visanio_learner the error occurs in 'main', as you said yourself, not in file_write... variables are local to the functions they're declared in. That's also true for parameters, they are only valid IN the function they're declared in, not around. - SirDarius

1 Answers

0
votes

When you call file_write(x,y,min_re,max_im,x_factor,y_factor);, the variable y isn't defined.

x contains a value that you don't want (height which is an odd value for x).

The solution is to remove the two parameters from the function call. The variables are local to file_write() (= they are never read before the value is overwritten) and need not be exposed in main()