代码使用goto返回不同的输出

问题描述 投票:2回答:1

我正在编写一个函数,它接受一个输入n并创建一个大小为(2n-1)^ 2的1D数组来模拟一个正方形。即对于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。我已经花了很多时间,但我不知道如何解决这个问题。任何帮助是极大的赞赏。

c function memory
1个回答
1
投票

您需要初始化您的数组。当调用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
© www.soinside.com 2019 - 2024. All rights reserved.