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

标签 c function memory

我正在编写一个函数,它接受一个输入 n 并创建一个大小为 (2n-1)^2 的一维数组来模拟一个正方形。 IE。对于 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;
   }

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

Current result

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

Desired result

我意识到这是一个很大的问题。我也意识到我不应该使用 goto。然而,我已经花了很多时间,但我不知道如何解决这个问题。非常感谢任何帮助。

最佳答案

您需要初始化您的数组。当调用 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

关于使用 goto 返回不同输出的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54859554/

相关文章:

c - 如何检查子字符串的实例是否在字符串的末尾并且仅一次(比如使用 strstr)?

c - 如何在 Windows 中退出阻塞的 connect() 调用?

c - _mm_ 类型函数的等效 C 代码

这可能是对齐内存问题吗?

c - 编写伪装为TTY的程序

javascript - 按钮不应该调用 php 函数

function - 通缉 : Matlab example of an anonymous function returning more than 1 output

c++ - 使用 C++ void 函数操作 vector 数组

php - PHP 是否根据其限制动态处理内存使用情况?

c - 检测写入字符串