gpt4 book ai didi

基于循环迭代器在 C/Fortran 中动态创建多个数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:53 25 4
gpt4 key购买 nike

我想根据 C 中循环的迭代创建多个数组。

例如:

int i, size;

for ( i =0; i< 10; i++)
{
size = i * 100
create_array(i,size); // This loop iterates 9 times creating 9 different arrays
}

void create_array(int name, int size)
{
double *array_name = (double*) malloc (size*sizeof(double));
// perform operations on array_name

}

因此我们最终得到 9 个数组,即 array_0、array_1、.... array_9。这可以用 C 或 Fortran(不是 C++)完成吗?

最佳答案

Fortran 示例:

program create_arrays


type ArrayHolder_type
integer, dimension (:), allocatable :: IntArray
end type ArrayHolder_type

type (ArrayHolder_type), dimension (:), allocatable :: Arrays

integer :: i, j

allocate ( Arrays (4) )

do i=1, 4
allocate ( Arrays (i) % IntArray (2*i) )
end do

do i=1, 4
do j=1, 2*i
Arrays (i) % IntArray (j) = i+j
end do
write (*, *)
write (*, *) i
write (*, *) Arrays (i) % IntArray
end do


end program create_arrays

关于基于循环迭代器在 C/Fortran 中动态创建多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17194773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com