Declaration of n-dimensional array [duplicate] - arrays

This question already has answers here:
Fortran: Choosing the rank of an allocatable array
(2 answers)
Dynamic array rank
(2 answers)
Set array's rank at runtime
(4 answers)
Closed 3 years ago.
Is there a way I can declare an array which will be able to allocate any number of dimensions (in any shape) later in the code? Something like a the classic deferred shape but bearing in mind I do not know the rank, shape or size during declaration. For example, something similar to
real, allocatable :: a(:)
integer, parameter :: sh = [4,2,2]
allocate(a(sh))
which does not throw an error (of course it does in this case).

Related

Printing 2D matrix on Fortran 90 [duplicate]

This question already has answers here:
Print a Fortran 2D array as a matrix
(3 answers)
Printing a Fortran array with an implicit loop
(3 answers)
Closed 6 months ago.
I am trying to print a 2D matrix of real numbers using fortran 90 using format descriptor F, and it keeps being printed as a single column. what should I do?
My code:
do i=1,N
do j=1,M
A(i,j) = 2.4
end do
end do
write(*,'(F6.2)')((A(i,j), j=1,M), i=1,N)

How do I initialise an entire matrix with 0? [duplicate]

This question already has answers here:
Initializing variable length array [duplicate]
(3 answers)
Closed 2 years ago.
I want to initialise my matrix to 0, but it does not work because the first part of the matrix is a variable.
int playerCards[playerNum][10] = {0};
I want to make so all the values in the matrix are zero. I have tried the line above, but it tells me that i cannot initialise a the array. For reference, playerNum is an integer between 2 and 6, chosen by the user.
You can use memset():
int playerCards[playerNum][10];
memset(playerCards, 0, sizeof playerCards);
sizeof works for VLAs: in this case playerCards is evaluated, which does not make a difference, but also means that the size is computed at runtime as sizeof(int) * playerNum * 10 using the value of playerNum at the time of instantiation of playerCards.

Fortran operations on multidimensional arrays - sum example [duplicate]

This question already has answers here:
Sum of a two dimensional array
(1 answer)
sum only on certain dimension
(1 answer)
Closed 4 years ago.
I am trying to figure how to do simple algebraic operations on a subset of elements in a multidimensional array and assign the result to another array with the same dimensions.
program hello
IMPLICIT NONE
REAL,DIMENSION(10,10,2,1) :: tmp1=2
REAL,DIMENSION(10,10,2,1) :: tmp2=1
INTEGER,DIMENSION(2) :: myind=(/1,2/)
This operations returns only one value
Print *, sum(tmp1(:,:,itree,:))
While this returns the correct results, however I would expect the same result than the second operation, maybe I am thinking in the R way.
Print *, tmp1(:,:,1,:)+tmp1(:,:,2,:)
end program Hello
How do I achieve the result of the second operation and assign it to another multidimensional array? Imagine my index is large so I can't type out each single term in the operation.

finding the size of an unknown array in C [duplicate]

This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
We have been given a large array of unknown size with elements given , is there any function or something other through which we can find the size of that array in language C
int a[]={4,6,4,26,3,2,5,7,3,7,3,2,5,4,6,3,7,232,6,32,6,3,7,3,6,2,5,7,3,6,3,6,36,3,67,23,6};
The size in bytes you can get by
sizeof(a);
The number of elements in that array you can get by
sizeof(a)/sizeof(a[0]);

Resizing a allocatable array [duplicate]

This question already has answers here:
Fortran increase dynamic array size in function
(3 answers)
Fortran array automatically growing when adding a value
(1 answer)
How to increase array size on-the-fly in Fortran?
(3 answers)
Closed 5 years ago.
I am trying to allocate an array and want to resize the array ie., grow the array size as and when required in the program.
My sample code looks like this
program main
implicit none
integer, allocatable, dimension(:)::test1
integer i, c1, c2
c1=10
c2=5
allocate(test1(1:c1))
! I basically want to do this
!allocate(test1(c1+1:c2))
end program main
How do I do this?

Resources