任何人都可以告诉我这段代码有什么问题吗?
我在主要功能中出错, 当我在main函数中调用'file_write'方法函数时,它 说'y'没有在这个范围内声明,但实际上它是我的论据 已经在方法函数中传递了它。
这是一个严重的错误吗?
#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;
}
答案 0 :(得分:0)
当您致电file_write(x,y,min_re,max_im,x_factor,y_factor);
时,未定义变量y
。
x
包含您不想要的值(height
,这是x
的奇数值。)
解决方案是从函数调用中删除这两个参数。变量是file_write()
的本地变量(=在覆盖值之前永远不会读取它们),不需要在main()
中公开