获取子例程返回引用数组的指针,然后在 main 中取消引用它(在 C 中)

发布于 2024-09-11 19:42:14 字数 1451 浏览 10 评论 0原文

你好,我正在尝试编写基本的一维蒙特卡罗积分的代码。为此,我需要一个伪随机数列表,然后可以将其输入到函数中(存储在另一个子例程中)。我已经给了随机数列表一个指针,但是当我尝试在 main 中取消引用它时,我得到“错误:从类型 'double' 分配给类型 'double[11]' 时不兼容的类型”。谁能告诉我哪里出错了?我的代码可以在这里找到:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef RAND_MAX
#define RAND_MAX 2147483648
#endif
#define N 10

double function(double x);
double* rdm(void);

void main(void)
{
   double* Random_number_list;
   int i;
   double sum = 0.0, sum2 = 0.0, X[N+1],S, Random_number_list2[N + 1];
   double F[N+1], lower, upper, avg, avg2;
   printf("Lower Bound:  ");
   scanf("%lf", &lower);
   printf("Upper Bound:  ");
   scanf("%lf", &upper);

   Random_number_list2 = *Random_number_list;

   for (i = 0; i <= N; i++) {
      X[i] = ((upper - lower)*Random_number_list2[i]) + lower;
      F[i] = function(X[i]);
      sum = sum + F[i];
      sum2 = sum2 + (F[i] * F[i]);
   }
   avg = sum / N;
   avg2 = sum2 / N;
   S = (upper - lower) * (avg + sqrt((avg2 - (avg * avg)) / N));
   printf("The Monte Carlo approximation is %lf\n", S);  
}

double function(double x)
{
   double y;
   y = sin (x);
   return y;
}

double* rdm(void)
{
   double* Random_number_list = calloc(N + 1, sizeof(double));
   int i;

   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list;
}

非常感谢。杰克·梅德利

Hiya im trying to write a code for basic 1D Monte-Carlo integration. To do so I need a list of pseudo-random numbers which I can then input into a function (stored in another subroutine). I've given the list of random numbers a pointer but when I try to dereference it in main I get "error: incompatible types when assigning to type ‘double[11]’ from type ‘double’ " . Could anyone tell me where I'm going wrong? My code can be found here:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef RAND_MAX
#define RAND_MAX 2147483648
#endif
#define N 10

double function(double x);
double* rdm(void);

void main(void)
{
   double* Random_number_list;
   int i;
   double sum = 0.0, sum2 = 0.0, X[N+1],S, Random_number_list2[N + 1];
   double F[N+1], lower, upper, avg, avg2;
   printf("Lower Bound:  ");
   scanf("%lf", &lower);
   printf("Upper Bound:  ");
   scanf("%lf", &upper);

   Random_number_list2 = *Random_number_list;

   for (i = 0; i <= N; i++) {
      X[i] = ((upper - lower)*Random_number_list2[i]) + lower;
      F[i] = function(X[i]);
      sum = sum + F[i];
      sum2 = sum2 + (F[i] * F[i]);
   }
   avg = sum / N;
   avg2 = sum2 / N;
   S = (upper - lower) * (avg + sqrt((avg2 - (avg * avg)) / N));
   printf("The Monte Carlo approximation is %lf\n", S);  
}

double function(double x)
{
   double y;
   y = sin (x);
   return y;
}

double* rdm(void)
{
   double* Random_number_list = calloc(N + 1, sizeof(double));
   int i;

   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list;
}

Many Thanks. Jack Medley

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-09-18 19:42:14

给定:

 int * a;
 int b[5];

那么你可以写:

 a = b;  // copying a pointer, same as a = &b[0];

但是,你不能写:

 b = a;  // b's memory is allocated, you cannot swap it out.

这意味着,失去 Random_number_list2,并仅使用 Random_number_list

你想要:

 double* Random_number_list = rdm();

稍后

 // added space, so it's clearer you're multiplying and not dereferencing.
 X[i] = ((upper - lower) * Random_number_list[i]) + lower;

Given:

 int * a;
 int b[5];

then you can write:

 a = b;  // copying a pointer, same as a = &b[0];

However, you cannot write:

 b = a;  // b's memory is allocated, you cannot swap it out.

Which means, lose Random_number_list2, and work with just Random_number_list.

You want:

 double* Random_number_list = rdm();

and later

 // added space, so it's clearer you're multiplying and not dereferencing.
 X[i] = ((upper - lower) * Random_number_list[i]) + lower;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文