【问题标题】:For loop and While loop to select values in CFor循环和While循环在C中选择值
【发布时间】:2016-05-03 14:25:06
【问题描述】:

有人可以帮我看看我用 C 写的代码吗?它可以生成数字,但是if条件看起来不对,但我不知道如何修复它。

我需要得到一些随机分布的值,这些值在一个特殊的区间内,所以我使用如下的 for 循环:

  n=10; mu=2; mu_plus=3; p=0.2;

  for(i=1; i<=n; i++)
    {
    x1 = gasdev(&idum);
    x2 = gasdev(&idum);
    z  = sqrt(1-p*p)*x1 + p*x2 + mu;
    z_p= x2 + mu_plus;

    if (z > 2.17 || z<-2.17) 
     {   
        Z[i]=z; 
        Z_plus[count]=z_p;
     }
  printf("%d %lf %lf\n", i, Z[i], Z_plus[i]);   
}

其中 gasdev() 是用于生成具有标准正态分布的随机值的函数,Z 和 Z_plus 是 1*n 向量。结果一团糟,所以我认为IF条件一定是错误的。任何人都可以帮助我吗?谢谢。

我也尝试了 While 循环。

   while(count < n)
    {
      x1 = gasdev(&idum);
      x2 = gasdev(&idum);
      z  = sqrt(1-p*p)*x1 + p*x2 + mu;
      z_p= x2 + mu_plus;

   if (z > 2.17 || z<-2.17) 
   {
       count++;         
       Z[count]=z; 
        Z_plus[count]=z_p;
    }
    printf("%d %lf\n",count, Z[count]);

    if (count >n) break;
  }

可以正常打印,但最后出现错误。

谢谢!

【问题讨论】:

  • 您是否要在+/-2.17 之间绑定z
  • 顺便说一句:(1-p)*(1+p) 的数值比1-p*p 更稳定。当|p| 靠近1 时,代码会得到更好的答案。
  • 没有。我尝试选择大于 2.17 或小于 -2.17 的值。
  • ZZ_plus 是如何声明的?在for 循环中,它们的大小至少需要为 11(或更可能,您想要for(i=0; i&lt;n; i++)。)此外,在for 循环中,icount 似乎可能混淆了.
  • for 循环的变体在if 条件未满时不会为对应的数组位置设置值,因此您必须使用while 方法。但是你应该只在设置数组值之后增加count,因为count 将是一个,否则在最后一次迭代中等于n

标签: c if-statement for-loop while-loop


【解决方案1】:

这里的错误是你总是递增 i 作为 for 循环的一部分,这样如果 z 超出括号集的范围,你就不会将任何值放入 Z 数组。您没有指定计数,所以应该是 i 还是不是?

  n=10; mu=2; mu_plus=3; p=0.2;

  for(i=1; i<=n; i++)
    {
      x1 = gasdev(&idum);
      x2 = gasdev(&idum);
      z  = sqrt(1-p*p)*x1 + p*x2 + mu;
      z_p= x2 + mu_plus;

      if (z > 2.17 || z<-2.17) 
       {   
          Z[i]=z; 
          Z_plus[count]=z_p; // Should this be Z_plus[i] ??
       }
      // Note that if outside of bracket no value put in Z[i]
      // This makes Z[i] and Z_plus[i] garbage
      printf("%d %lf %lf\n", i, Z[i], Z_plus[i]);   
    }

您的 while 循环也不正确,因为它在 n-1 时递增计数,并在您必须将其定义为大小为 n 的数组时尝试处理 Z[n](最大索引为 n-1 )。如果不在外面,打印也需要在里面。

   while(count < n)
    {
      x1 = gasdev(&idum);
      x2 = gasdev(&idum);
      z  = sqrt(1-p*p)*x1 + p*x2 + mu;
      z_p= x2 + mu_plus;

      if (z > 2.17 || z<-2.17) 
       {
         // This allows count == n which overflows the buffer.
         count++;         
         Z[count]=z; 
         Z_plus[count]=z_p;
       }

    // This should be inside the bracket not outside
    printf("%d %lf\n",count, Z[count]);

    // This is not needed since it will exit the while at count == n
    if (count >n) break;
  }

正确的代码是

   count = 0;
   while(count < n)
    {
      x1 = gasdev(&idum);
      x2 = gasdev(&idum);
      z  = sqrt(1-p*p)*x1 + p*x2 + mu;
      z_p= x2 + mu_plus;

      if (z > 2.17 || z<-2.17) 
       {
         Z[count]=z; 
         Z_plus[count]=z_p;
         printf("%d %lf\n",count, Z[count]);
         count++;         
       }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2020-03-30
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多