【问题标题】:Why is my Mandelbrot-recursion only printing nonsense?为什么我的 Mandelbrot-recursion 只打印废话?
【发布时间】:2022-01-08 10:50:08
【问题描述】:
#include <stdio.h>
#include <math.h>
#include <complex.h>

#define N_MAX 12

int Mandelbrot_equation (double x, double y, int n, double Zn) {
  if (cabs(Zn) >= 2 || n >= N_MAX) return n;
  if (n == 0) Zn = 0;
  else {
    Zn = cpow(Zn, 2 + 0 * I) + x + I * y;
  }
  Mandelbrot_equation(x, y, n + 1, Zn);
}

int main(int argc, char const *argv[]) {
  for (double y = -2.0; y < 1.0; y += 0.1) {
    for (double x = -1.0; x < 1.0; x += 0.1) {
      if (Mandelbrot_equation(x,y,0,0) == N_MAX) printf(".");
      else{
        printf(" ");
      }
      if (x == 0.9) printf("\n");
    }
  }
  return 0;
}

在上面的代码中,我尝试实现 Mandelbrot 递归并将其打印到屏幕上。 问题是输出如下:https://i.stack.imgur.com/c0LPe.png

我是不是把递归弄错了,还是等式弄错了?

【问题讨论】:

  • 不要发布文字图片。将文本作为文本发布。阅读:How to Ask
  • 锌不复杂
  • 也许您可以添加预期输出的样子(?)
  • 你的编译器应该警告你你没有从Mandelbrot_equation()返回值。始终注意编译器消息。
  • 请问您为什么选择将其实现为递归函数而不是使用简单的循环?

标签: c recursion


【解决方案1】:

三个粗略的错误:

  1. Zn 参数应该很复杂:
int Mandelbrot_equation (double x, double y, int n, double Zn) {

应该是:

int Mandelbrot_equation (double x, double y, int n, complex Zn) {
  1. Mandelbrot_equation() 中缺少 return
  }
  Mandelbrot_equation(x, y, n + 1, Zn);
}

  }
  return Mandelbrot_equation(x, y, n + 1, Zn);
}
  1. 换行条件。

条件if (x == 0.9) printf("\n"); 永远不会被满足,因为数字0.9 不能用浮点运算来表示。应该使用整数或不等式运算符。但是,将换行打印机移动到外部y 循环的最佳方式。

替换:

for (double x = -1.0; x < 1.0; x += 0.1) {
      if (Mandelbrot_equation(x,y,0,0) == N_MAX) printf(".");
      else{
        printf(" ");
      }
      if (x == 0.9) printf("\n");
    }

for (double x = -1.0; x < 1.0; x += 0.1) {
      if (Mandelbrot_equation(x,y,0,0) == N_MAX) printf(".");
      else{
        printf(" ");
      }
      
    }
    printf("\n");

结果:

                     
                     
        .            
          .          
         .           
        ...          
        ...          
    ...........      
    ..........       
   ...........       
.. ............      
...............      
..............       
..............       
..............       
...............      
.. ............      
   ...........       
    ..........       
    ...........      
        ...          
        ...          
         .           

编辑

将范围替换为:

for (double y = -1.5; y < 1.5; y += 0.1) {
    for (double x = -2.0; x < 1.0; x += 0.05) {

打印# 而不是. 让它看起来更酷:

                                                            
                                    #                       
                                        #                   
                                     ##                     
                                   ######                   
                                   ######      #            
                            #####################           
                           #####################            
               #           ##################### #          
                ######    #######################           
                #################################           
            ####################################            
 ##############################################             
            ####################################            
                #################################           
                ######    #######################           
               #           ##################### #          
                           #####################            
                            #####################           
                                   ######      #            
                                   ######                   
                                     ##                     
                                        #                   
                                    #                       
                                                            

【讨论】:

  • 使用for (double x = -1.0; x &lt; 1.0; x += 0.1) 是不好的形式,因为C 标准和各种浮点格式和算术不能保证循环将有200 次或201 次迭代。另一种选择是for (double counter = 0; counter &lt; 200; ++counter) { double x = (counter-100)/10;… }
  • @EricPostpischil,是的,但这是一个较小的错误。原始程序有if (x == 0.9) printf("\n");,该条件“难以”满足。我只是尽量减少对原始代码的更改
猜你喜欢
  • 2012-09-11
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多