在 C 中获取子例程返回三个独立的随机数数组

发布于 2024-09-12 13:14:03 字数 500 浏览 17 评论 0原文

我目前有一个子例程的代码,用于返回指向数组的指针。该数组是一维蒙特卡罗积分的随机数列表。我现在正在尝试做一个多维等效项,它需要 3 个随机数数组,而不是为每个数组都有一个单独的子例程,而是尝试创建一个返回 3 × N + 1 数组的子例程。有人可以帮我编码吗?一位伙伴提到我需要一个双指针,但到目前为止大多数网络资源都没有帮助。这是我的单个数组代码:

double* rdm_Y(void)
{
   double* Random_number_list_Y = calloc(N + 1, sizeof(double));
   int i;
   sleep(1);
   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list_Y[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list_Y;
}

非常感谢! 杰克·梅德利

I currently have code for a subroutine to return a pointer to an array. This array is a list of random numbers for a one dimensional monte-carlo integral. I am now trying to do a multi dimensional equivalent which requires 3 arrays of random numbers and instead of having a separate subroutine for each I'm trying to make one which returns a 3 by N + 1 array. Could somebody please help me with the coding for this. A mate mentioned I would need a double pointer but most web sources have been unhelpful thus far. Here is my single array code:

double* rdm_Y(void)
{
   double* Random_number_list_Y = calloc(N + 1, sizeof(double));
   int i;
   sleep(1);
   srand(time(NULL));
   for (i = 1; i <= N; i++) {
      Random_number_list_Y[i] = (float) rand() / (float) RAND_MAX;
   }
   return Random_number_list_Y;
}

Many Thanks!
Jack Medley

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

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

发布评论

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

评论(3

魂归处 2024-09-19 13:14:03

动态分配 T 类型的二维数组(其中 T 可以是 intdouble 等)的一般模式是

#include <stdlib.h>

T **alloc(size_t rows, size_t columns)  
{
  T **arr = malloc(sizeof *arr, rows); // type of *arr is T *
  if (arr)
  {
    size_t i;
    for (i = 0; i < rows; i++)
    {    
      arr[i] = malloc(sizeof *arr[i], columns); // type of *arr[i] is T
      if (arr[i])
      {
        size_t j;
        for (j = 0; j < columns; j++)
        {
          arr[i][j] = initial_value_for_this_element;
        }
      }
    }
  }
  return arr;
}

The general pattern for dynamically allocating a 2D array of type T (where T can be int, double, etc.) is

#include <stdlib.h>

T **alloc(size_t rows, size_t columns)  
{
  T **arr = malloc(sizeof *arr, rows); // type of *arr is T *
  if (arr)
  {
    size_t i;
    for (i = 0; i < rows; i++)
    {    
      arr[i] = malloc(sizeof *arr[i], columns); // type of *arr[i] is T
      if (arr[i])
      {
        size_t j;
        for (j = 0; j < columns; j++)
        {
          arr[i][j] = initial_value_for_this_element;
        }
      }
    }
  }
  return arr;
}
习ぎ惯性依靠 2024-09-19 13:14:03

尝试:

struct res{
 double *arr1, *arr2, *arr3;
};
main(){
 struct res r;
 r.arr1 = rdm_Y();
 r.arr2 = rdm_Y();
 r.arr3 = rdm_Y();
 // in r you have 3 pointers to 3 separate arrays
}

或者类似的东西

Try:

struct res{
 double *arr1, *arr2, *arr3;
};
main(){
 struct res r;
 r.arr1 = rdm_Y();
 r.arr2 = rdm_Y();
 r.arr3 = rdm_Y();
 // in r you have 3 pointers to 3 separate arrays
}

or something like this

墨小沫ゞ 2024-09-19 13:14:03

我能想到的三种方法是:

  1. A *double 到一个大小为 3xN 的一维数组(你可以假装它是三个数组)
  2. A **double 到一个数组三个*double,每个都指向一个包含 N 的数组
  3. 一个包含三个 *double,每个都指向一个包含 N 的数组

如果您不喜欢假装方法 1,您可以再声明两个 *double 并将它们分别设置为返回值 + N 和 + 2N。另外,不要忘记 free(),您应该分别为每个方法执行 1、4 和 3 个 free()

The three methods I can think of are:

  1. A *double to a 1D array of size 3xN (you can just pretend it's three arrays)
  2. A **double to an array of three *doubles, each one pointing to an array of N
  3. A struct containing three *doubles, each one pointing to an array of N

If you don't like pretending for method 1 you can declare two more *doubles and set them to the return value + N and + 2N respectively. Also don't forget to free() you should have 1, 4, and 3 free()s to do for each of the methods respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文