Writing numbers only each second and third line in Fortran - numbers

I am fighting with trying to get numbers 1 and 2 only on each second and third line in the second column.
I have a file tisk2 with
O
H
H
O
H
H
And I need to get to file tisk this:
O
H1
H2
O
H1
H2
I mean I want no number no text after O. I tried to write this code:
program cislo
implicit none
integer :: i, sum, j, k
character :: t*10, m*10
open(10,file="tisk2",status='old')
open(12,file="tisk",status='old')
do k=0,5
read(10,*) m
enddo
do j=1,3
do i = 0,0
t=" "
print*, t
exit
enddo
sum=0
do i=1,2
write(12,fmt='(a2,i2)') m, i
enddo
enddo
end program cislo
I can print only i and I get nothing, second line 1 and third line 2. Then alone print m. But when I want to put both to one file, it skips O and writes to file only H1, H2 and then again. Does anyone has an idea how to get what I need? Thank you for help.

Related

Calculator doesn't execute the operations

I am trying to build a calculator in abap. The requirements are:
Reads
two numbers (ex. 56.3 and 78.2)
a character from the following list: q, w, e, r, t
Displays the result of the operation specified by the character
Addition for character q
Subtraction for character w
Multiplication for character e
Division for character r
Exponent for character t
I have created a table with the operations that I am using in the calculator.
The problem is when I execute the program it only prints my last option (else) "the operation is not possible".
Here's the code I wrote:
REPORT Z_CALCULATOR_V2.
TABLES: ZOPERATII.
DATA result type p decimals 2.
DATA Q type c.
DATA W like Q.
DATA E like Q.
DATA R like Q.
DATA T like Q.
PARAMETERS Nr_1 type p decimals 2 OBLIGATORY.
PARAMETERS Nr_2 like Nr_1 OBLIGATORY.
PARAMETERS Operatie LIKE zoperatii-operatie OBLIGATORY.
if Operatie = Q.
result = Nr_1 + Nr_2.
elseif Operatie = W.
result = Nr_1 - Nr_2.
elseif Operatie = E.
elseif Operatie = R.
result = Nr_1 / Nr_2.
elseif Operatie = T.
result = Nr_1 ** Nr_2.
else.
WRITE 'The operation is not possible'.
endif.
write result.
if you change the data declarations to:
DATA Q type c value 'Q'.
DATA W type c value 'W'.
DATA E type c value 'E'.
DATA R type c value 'R'.
DATA T type c value 'T'.
the code should run as you expect. That said, you should read up on the documentation as suggested in the comments.

Validation Pattern MUMPS

I am working to create a program in M that reads an input of names in a certain format. Once the user places a null value it prints out all of the values. Where I am getting stuck is reading into a global variable. I look forward to any input I can receive.
n prompt,val,done
s prompt="Enter a name (LAST,FIRST MI): "
s val="" f in=1:1 s val=$O(^ZNAME(val)) q:val=""
f w !,prompt r val q:val="" d q:done
. i val'?1.A1",".1" "1.A.1(1" "1A) w !,"Invalid name" q
. s val=$GET(^ZNAME)
. s done=1
i val="" q
w !,"You entered: ",val
. s done=1
q
Your code looks strange. In first this line s val="" f in=1:1 s val=$O(^ZNAME(val)) q:val="" is useless, just order all values in first subscript of global. Then your code should fail with UNDEFINED error for done variable when entered val will passed your pattern. And finally it gets new value from global ^ZNAME, and does not matter what was entered, it will get one value for all times.
I don't completely understand what you want to do but this is what I built:
read names till you give an empty name
if the name follows a pattern add him to ^ZNAME
at the end print all names in ^ZNAME
Here you go:
readlist
N prompt,val
S prompt="Enter a name (LAST,FIRST MI): "
F W !,prompt R val Q:val="" D
. I val'?1.A1",".1" "1.A.1(1" "1A) W !,"Invalid name"
. E S ^ZNAME(val)=""
F S val=$O(^ZNAME(val)) Q:val="" D
. W !,"You entered: ",val
Q
Example (using GTM):
GTM>D readlist^ZZTEST
Enter a name (LAST,FIRST MI): first,last
Enter a name (LAST,FIRST MI): name,surname
Enter a name (LAST,FIRST MI):
You entered: first,last
You entered: name,surname
GTM>zwr ^ZNAME
^ZNAME("first,last")=""
^ZNAME("name,surname")=""

Finding the error in this code

I keep receiving error,';' unexpected in this piece of maple code. I have looked and looked and just can't seem to find where I'm going wrong. Can anyone spot it?
QSFactorization := proc (n::(And(posint, odd)), mult::nonnegint := 0, { mindeps::posint := 5, c := 1.5 })
local mfb, m, x, fb, nfb, r, M, d;
if isprime(n) then
return "(n)"
elif issqr(n) then
return "(isqrt(n))"*"(isqrt(n))"
elif n < 1000000 then
return ifactor(n)
end if;
if mult = 0 then
mfb := MultSelect(n, ':-c' = c)
else mfb := [mult, FactorBase(mult*n, c)]
end if;
m := mfb[1];
if 1 < m then
print('Using*multiplier; -1');
print(m)
end if;
x := m*n*print('Using*smoothness*bound; -1');
print(ceil(evalf(c*sqrt(exp(sqrt(ln(n)*ln(ln(n))))))));
fb := Array(mfb[2], datatype = integer[4]);
nfb := ArrayNumElems(fb);
print('Size*of*factor*base; -1');
print(nfb);
r := Relations(x, fb, ':-mindeps' = mindeps);
M := r[3]; print('Smooth*values*found; -1');
print(nfb+mindeps);
print('Solving*a*matrix*of*size; -1');
print(LinearAlgebra:-Dimension(M));
d := Dependencies(M);
print('Number*of*linear*dependencies*found; -1');
print(nops(d));
print('Factors; -1');
FindFactors(n, r, d)
end proc
I'd really appreciate any insight.
You basic problem is that you are using the wrong quotes inside your print statements. This is invalid,
print('Using*multiplier; -1');
You are using single right-quotes (tick), which in Maple is used for unevaluation. In this case the semicolons inside your print statements are syntax errors.
Use either double-quotes or single left-quotes instead. The former delimits a string, and the latter delimits a name. Eg,
print("Using*multiplier; -1");
print(`Using*multiplier; -1`);
If you choose to go with name-quotes then the print command will prettyprint the output in the Maple GUI with the name in an italic font by default, but you won't see the quotes in the output.
If you choose to go with string-quotes then the print command will show the quotes in the output, but will use an upright roman font by default.
Some other comments/answers (since deleted) on your post suggest that you are missing statement terminators (colon or semicolon) for these two statements,
print(m)
FindFactors(n, r, d)
That is not true. Those statements appear right before end if and end proc respectively, and as such statement terminators are optional for them. Personally I dislike coding Maple with such optional terminator instances left out, as it can lead to confusion when you add intermediate lines or pass the code to someone else, etc.

Emacs auctex inserts line breaks after inline math in an ugly way

I got problems with emacs/auctex fill paragraph which insert linebreaks after each $blabla$ block, resulting in a poorly readable code. For exemple, hitting M-q on the following long line:
a $1$ b $2$ c $3$ d $4$ e $5$ f $6$ g $7$ h $8$ i $9$ j $10$ k $11$ l $12$ m $13$ n $14$ o $15$
Gives:
a $1$
b $2$
c $3$
d $4$
e $5$
f $6$ g $7$ h $8$ i $9$ j $10$ k $11$ l $12$ m $13$ n $14$ o $15$
I would like to have something like:
a $1$ b $2$ c $3$ d $4$ e $5$ f $6$ g $7$ h $8$ i $9$ j $10$ k
$11$ l $12$ m $13$ n $14$ o $15
Note: I have the impression that fill-paragraph didn't have this ugly behavior when I didn't use auctex but the built-in latex mode...
Any ideas?
Many thanks!
This can be fixed by changing the AUCTeX user option LaTeX-fill-break-at-separators (see AUCTeX Manual [Filling]).
Hit M-x and type customize-group in the minibuffer, then type LaTeX.
Now you should be able to see the option LaTeX Fill Break At Separators in the newly created buffer. Click on it (expanding it) and uncheck the box next to Closing Inline Math Switches. Don't forget to save.

single output file containing multiple columns

I was trying to code this problem in fortran. The output file contains the result in two columns. I am having hard time to modify my code (below) to get a gnuplot-ready output file containing nt columns and nx lines. Can anybody help me? Thanks!
PROGRAM odlc
IMPLICIT NONE
INTEGER::i,it,nx,nt,k,ierr
DOUBLE PRECISION::dx,dt,c
DOUBLE PRECISION,DIMENSION(2000)::u,un
ierr=0
nx=20
nt=50
dt=0.01
c=1.0
dx=2./(nx-1.)
!initial condition
DO i = 1,nx
IF(i*dx>=0.5 .and. i*dx<=1) THEN
u(i) = 2
ELSE
u(i)=1
ENDIF
ENDDO
!Finite Difference
OPEN(UNIT=200,FILE='tab2.txt',STATUS='REPLACE',ACTION='WRITE',IOSTAT=ierr)
DO it=1,nt
DO k=1,nx
un(k)=u(k)
ENDDO
DO i=2,nx-1
u(i)=un(i)-c*dt/dx*(un(i)-un(i-1))
ENDDO
DO i=1,nx
WRITE(200,'(I7,F10.2)')i,u(i)
ENDDO
ENDDO
CLOSE(UNIT=200)
END
I'm not 100% sure that it is exactly what you want but here you go:
Declare
CHARACTER(len=32)::fmt
and initialize it like this
WRITE(fmt,*)'(',nx,'F10.2)'
This just creates a format string which writes a single line of nx reals.
Then replace
DO i=1,nx
WRITE(200,'(I7,F10.2)')i,u(i)
ENDDO
by
WRITE(200,fmt) (/ (u(i), i=1,nx) /)
Now in your tab2.txt file, you will get nt lines and nx columns.
After your update, here is the (slightly shortened) code that will do what you want:
PROGRAM odlc
IMPLICIT NONE
INTEGER::i,it
INTEGER,PARAMETER::nx=20,nt=50
DOUBLE PRECISION,PARAMETER::dx=2./(nx-1.),dt=0.01,c=1.0
DOUBLE PRECISION,DIMENSION(nt,nx)::u
CHARACTER(len=32)::fmt
WRITE(fmt,*)'(I3,',nt,'F5.2)'
!initial condition
DO i = 1,nx
IF(i*dx>=0.5 .and. i*dx<=1) THEN
u(1,i) = 2
ELSE
u(1,i)=1
ENDIF
ENDDO
!Finite Difference
DO it=2,nt
DO i=2,nx-1
u(it,i)=u(it-1,i)-c*dt/dx*(u(it-1,i)-u(it-1,i-1))
ENDDO
ENDDO
!Write to file
OPEN(UNIT=200,FILE='tab2.txt',STATUS='REPLACE',ACTION='WRITE')
DO i=1,nx
WRITE(200,fmt)REAL(i*(2./(nx-1.))),u(:,i)
ENDDO
CLOSE(UNIT=200)
END PROGRAM odlc