Write to screen in the same line fortran - fortran

I am looking to output some numbers to the screen in Intel Fortran, By default Fortran writes a new line everytime I output something to the screen. Is there anyway to avoid this?
Here is a sample of my code:
PROGRAM sample
integer :: ind
do ind = 1,20
write(*,*) ind
!Some other stuff here
end do
END PROGRAM
I tried to use the no-advance option but it did not work:
PROGRAM sample
integer :: ind
do ind = 1,20
write(*,*,advance='no') ind
!Some other stuff here
end do
END PROGRAM
Ideally what I want is to output the following to the screen:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
All in the same line. Is there ant way of doing this in fortran?

This does not work with the format "*" (list directed). But it is OK with an explicit format :
program test
implicit none
integer :: ind
do ind = 1,20
write(*,'(1x,i0)',advance='no') ind
!Some other stuff here
end do
end program
Result :
[coul#localhost ~]$ gfortran test.f90
[coul#localhost ~]$ ./a.out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20[coul#localhost ~]$

Related

How to set different (if) parameters for the same number in a list?

I don't even know how to format this paragraph right haha
Hi there I'm completely new to Python.
I was wondering if I have a list
how do I take the output of a first if command and use on the next
Example
L= [23, 91, 0, -11, 4, 23, 49]
for i in L:
if(i > 10):
print(i * 30)
example
I also want to check if the output of this number is an even number if so (+6) -- for the output of that
if number is not equal to -11 I want to add 10.
So I got 23 > 10= (23*30 = 690) // 690 is an even number ( 690 + 6=696) // it not equal to -11 so (696+10= 706).
How do I do this for every number on the list? Excuse my lack of knowledge it's literally my first python class and my first exercises.
Is that what you want?
L= [23, 91, 0, -11, 4, 23, 49]
for i in L:
if i > 10:
num = i * 30
if num % 2 == 0:
num += 6
if i != -11:
num += 10
else:
pass
print(num)
output:
706
2746
706
1486

GDB and Fortran modules

I have the following Fortran 95 code:
MODULE ISSUE
IMPLICIT NONE
CONTAINS
SUBROUTINE PROBLEM(A)
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(:,:), INTENT(INOUT) :: A
INTEGER :: i, n
n = SIZE(A, 2)
DO i = 1, n
PRINT *, A(i, 1:n)
ENDDO
END SUBROUTINE PROBLEM
END MODULE ISSUE
PROGRAM TEST
USE ISSUE
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(5, 5) :: A
A = TRANSPOSE(RESHAPE((/ 1, 2, 3, 4, 5, &
6, 7, 8, 9, 0, &
1, 2, 3, 4, 5, &
6, 7, 8, 9, 0, &
1, 2, 3, 4, 5/), SHAPE(A)))
CALL PROBLEM(A)
END PROGRAM TEST
And now I compile it using
gfortran -g -O0 problem.f95 -o problem
Then I run the program using GDB 7.7.1 and I set a breakpoint to line 11, (DO i = 1, n). After that, I print the first element of the matrix A. The output that I get is:
(gdb) break 11
Breakpoint 1 at 0x4008c5: file problem.f95, line 11.
(gdb) run
Starting program: /path_to_problem_folder/problem
Breakpoint 1, issue::problem (a=...) at problem.f95:11
warning: Source file is more recent than executable.
11 DO i = 1, n
(gdb) p A(1, 1)
$1 = 6.9533558074105031e-310
I expect the output 1, but 6.9533558074105031e-310 is printed by gdb. Note that the subroutine PROBLEM prints all values correctly.
Why this happens and how can I print the value A(1, 1) correctly using GDB?
I updated my GDB version from 7.7.1 to 7.11.1 and the problem disappeared.

Assignment of Allocatables of Different Shapes in Fortran [duplicate]

This question already has an answer here:
Allocatable array valued function. gfortran vs ifort
(1 answer)
Closed 5 years ago.
Please look at the following code:
program test
implicit none
integer, allocatable :: v1(:, :)
integer, allocatable :: v2(:, :)
allocate(v1(2, 4))
allocate(v2(2, 3))
v1(:, :) = reshape([11, 12, 13, 14, 15, 16, 17, 18], [2, 4])
v2(:, :) = reshape([21, 22, 23, 24, 25, 26], [2, 3])
print *, v1
print *, 'shape(v1): ', shape(v1)
print *
print *, v2
print *, 'shape(v2): ', shape(v2)
print *
v2 = v1
print *, v1
print *, 'shape(v1): ', shape(v1)
print *
print *, v2
print *, 'shape(v2): ', shape(v2)
print *
deallocate(v1)
deallocate(v2)
end program test
When I compile it with gfortran, I get the following output:
11 12 13 14 15 16 17 18
shape(v1): 2 4
21 22 23 24 25 26
shape(v2): 2 3
11 12 13 14 15 16 17 18
shape(v1): 2 4
11 12 13 14 15 16 17 18
shape(v2): 2 4
When I compile it with ifort, I get the following output:
11 12 13 14 15 16 17 18
shape(v1): 2 4
21 22 23 24 25 26
shape(v2): 2 3
11 12 13 14 15 16 17 18
shape(v1): 2 4
11 12 13 14 15 16
shape(v2): 2 3
which one is reliable? is there a bug in ifort or in gfortran?
gfortran version 4.8.1
ifort version 14.0.0
By default, ifort before version 17 does not use Fortran 2003 semantics for reallocating an allocatable type on the left side of an assignment. The ifort 15 manual has this to say (for the default norealloc-lhs assumption):
The compiler uses Standard Fortran rules when interpreting assignment statements. The left-hand side is assumed to be allocated with the correct shape to hold the right-hand side. If it is not, incorrect behavior will occur.
To allow the left side of the assignment to be reallocated to the proper shape, compile with the option -assume realloc-lhs. Alternatively you can compile with -standard-semantics to make all assumptions default to compliance with the Fortran 2003 standard, with some Fortran 2008 features.

Avoiding IF in a simple mapping function

I'm trying to avoid an IF in the following mapping function:
X Y
1 11
2 10
3 9
4 8
5 7
6 6
7 5
8 4
9 3
10 2
11 1
12 12
It's basically Y = (12 - X), except when X = 12. In this case, Y = 12.
The Y vector is the reverse of the X vector shifted by one position. Is there a way to write this function using min and max or something like this in order to avoid a conditional?
I'm not attached to any programming language here
y = 12 - x%12;
works for all x from 1 to 12 inclusive. % is the C-style modulus operator, giving the remainder from dividing x by 12. That's zero if x is 12, and x for 1 to 11.
Ruby answer:
x = (1..12).to_a
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = x.map{|n| 12 - n % 12}
#=> [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 12]
This can be extended to work for any length by using n.max instead of 12.

Powerball number generator

To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770.
The output needs to be in the form:
Official (but fruitless) Powerball number generator
How many sets of numbers? 3
Your numbers: 3 12 14 26 47 Powerball: 2
Your numbers: 1 4 31 34 51 Powerball: 17
Your numbers: 10 12 49 50 53 Powerball: 35
import random
#Powerball
print "Offical Powerball number generaor"
x = int(raw_input("How many sets of numbers? "))
z = range(1,42)
z1 = random.choice(z)
def list1():
l1=[]
n=1
while n<=5:
y = range(1,53)
y1 = random.choice(y)
l1.append(y1)
n +=1
print sorted(l1)
i=1
while i<=x:
# print "Your numbers: " + list1() + "Powerball: "+ str(z1)
print list1()
raw_input("Press<enter>")
My code's output goes on a infinite loop. I have to kill it. And the message is:
None
[2, 7, 22, 33, 42]
None
[15, 19, 19, 26, 48]
None
[1, 5, 7, 26, 41]
None
[7, 42, 42, 42, 51]
None
..... etc ....
while i<=x: - you never increment i, so it is stuck in your last loop...
To avoid such things and remove the noise of i+=1 lines in your code I suggest using for loops for i in range(x) and for n in range(5).
Better yet, the following expression can replace list1:
[random.choice(range(1,53)) for x in xrange(5)]
At least, that does the same as your code. But what you probably really want (to avoid the same ball being chosen twice) is:
random.sample( range(1,53), 5 )