首页 > 解决方案 > 使用 goto 返回不同输出的代码

问题描述

我正在编写一个函数,它接受一个输入 n 并创建一个大小为 (2n-1)^2 的一维数组来模拟一个正方形。即对于 n = 1 的输入,只有一个点,对于 n = 2 的输入,它看起来像

0 1 2
3 4 5
6 7 8

对于 n = 3 它看起来像

0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

其中每个数字都是一个点。

当检测到当前位置位于边缘并且该点试图移出正方形的网格时,该函数终止。

这样做的目的是模拟从 n=2^0 到 n=2^8 的不同大小的正方形访问了多少点,并返回访问的点占总点数的比例正方形。

该函数生成一个随机数并检查它与 4 的模数,如果返回 0,则位置向上移动 1,如果返回 1,则位置向右移动,2 向下移动,3 向左移动。

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

double two_d_random (int n) {
       int tot_points = (2 * n - 1)*(2 * n - 1);
       int value = (n*n)+((n-1)*(n-1))-1; //center
       int length = 2 * n - 1; //length of side
       int *array = (int *)malloc (sizeof (int) * tot_points);     
       int count = 0;
       array[value] = 1;

   while (1 == 1) {
          int r = rand () % 4;
          array[value] = 1;
          if (r == 0) {//UP
                 if ((value >= 0) && (value < length)) {
                       goto a;
                       }
                 else {
                     array[value] = 1;
                       value -= length;
                       }
                 }
          else if (r == 1) {//RIGHT
                 if ((value % length) == (2*n-2)){
                       goto a;
                       }
                 else {
                     array[value] = 1;
                       value += 1;
                       }
                 }
          else if (r == 2) {//DOWN
                 if ((value < tot_points) && (value >= (tot_points - length))) {
                       goto a;
                       }
                 else {
                     array[value] = 1;
                       value += length;
                       }
                 }
          else if (r == 3) {//LEFT
                 if (value % length == 0) {
                       goto a;
                       }
                 else {
                     array[value] = 1;
                       value -= 1;
                       }
                 }
          }

a:
   for (int i = 0; i < tot_points; i++) {
          if (array[i] == 1) {
                 count += 1;
                 }
          }

   free (array);
   return 1.0 * count / tot_points;


   }

int main ()
   {
   int trials = 1000;

   srand (12345);
   for (int n = 1; n <= 256; n *= 2)
          {
          double sum = 0.;
          for (int i = 0; i < trials; i++)
                 {
                 double p = two_d_random(n);
                 sum += p;
                 }
          printf ("%d %.3lf\n", n, sum / trials);
          }
   return 0;
   }

我目前的问题是,当我在我的机器上运行它时,我得到了一系列我没想到的值:

当前结果

但是,当一位同事在他们的机器上运行它时,他们会得到以下结果,这是我所期望的:

期望的结果

我意识到这是一个很大的问题。我也意识到我不应该使用 goto。但是,我已经为此花费了很多时间,但我不知道如何解决此问题。任何帮助是极大的赞赏。

标签: cfunctionmemory

解决方案


你需要初始化你的数组。调用malloc()它时只返回一块未初始化的内存。要么初始化它,要么使用它calloc()来获得预置零的内存。

double two_d_random( int n )
{
    int tot_points = ( 2 * n - 1 ) * ( 2 * n - 1 );
    int value = ( n * n ) + ( ( n - 1 ) * ( n - 1 ) ) - 1;      //center
    int length = 2 * n - 1;     //length of side
    int *array = (int *) malloc( sizeof( int ) * tot_points );
    int count = 0;

    // Initialise the array to zero
    for ( int i=0; i<tot_points; i++ )
    {
        array[i] = 0;
    }

    array[value] = 1;

    while ( 1 == 1 )
    {
        int r = rand() % 4;
        array[value] = 1;
        if ( r == 0 )

通过此修改,我得到的结果您所希望的报告类似:

1 1.000
2 0.367
4 0.221
8 0.154
16 0.122
32 0.101
64 0.085
128 0.077
256 0.071

推荐阅读