forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Write format

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Anonymous
Guest





PostPosted: Thu Oct 21, 2004 2:31 pm    Post subject: Write format Reply with quote

Hello,

I have an array with n² values (n=1,3,5,7). I would like to display it with n lines of n elements (I want to see a matrix in my console window).
At school, I have learnt the syntax :
Code:
WRITE (*,'(<n>I3)')
, with n an integer defined in the programm and put into hooks to specify to the compiler that it's an integer already defined.
How can I do with FTN 95 ?

Thank you for your answer.

That's my code :

Code:

PROGRAM carre_magique

IMPLICIT NONE

INTEGER :: n,p,i,j
INTEGER :: error,somme
INTEGER,ALLOCATABLE :: grand_carre(:,Smile
INTEGER,ALLOCATABLE :: c_haut(:,Smile,c_bas(:,Smile,c_droite(:,Smile,c_gauche(:,Smile,petit_carre(:,Smile

!entete
WRITE (*,*) "Construction d'un carre magique de taille impaire n=2p+1"
WRITE (*,"(A,$)") "Valeur de p (0<p<5) ? "

!on n'accepte que 1 <= p <= 4
DO
READ (*,*) p
IF ((p>0) .AND. (p<5)) EXIT
WRITE (*,*) "Attention : 0<p<5"
ENDDO

!ordre du carre magique
n = 2*p+1

!allocation des tableaux
ALLOCATE(grand_carre(3*n,3*n),STAT = error)
IF (error /= 0) STOP "Erreur d'allocation"
ALLOCATE(c_haut(n,n),STAT = error)
IF (error /= 0) STOP "Erreur d'allocation"
ALLOCATE(c_bas(n,n),STAT = error)
IF (error /= 0) STOP "Erreur d'allocation"
ALLOCATE(c_droite(n,n),STAT = error)
IF (error /= 0) STOP "Erreur d'allocation"
ALLOCATE(c_gauche(n,n),STAT = error)
IF (error /= 0) STOP "Erreur d'allocation"
ALLOCATE(petit_carre(n,n),STAT = error)
IF (error /= 0) STOP "Erreur d'allocation"

!initialisation du grand carre 3nx3n
grand_carre = 0

!remplissage de 1 ˆ n^2
DO i=1,n
DO j=1,n
grand_carre(p+i+j,p+n+1+i-j) = n*(i-1) + j
ENDDO
ENDDO

!extraction des 5 petits carres nxn
petit_carre = grand_carre(n+1:2*n,n+1:2*n)

c_haut = grand_carre(1:n,n+1:2*n)
c_bas = grand_carre(2*n+1:3*n,n+1:2*n)
c_gauche = grand_carre(n+1:2*n,1:n)
c_droite = grand_carre(n+1:2*n,2*n+1:3*n)

!cumul des 5 tableaux => carre magique final
petit_carre = petit_carre + c_haut + c_bas + c_gauche + c_droite

!affichage du resultat
WRITE (*,*)
WRITE (*,'(A,I2,A)') "Voici un carre magique d'ordre ",n," :"
WRITE (*,*)
WRITE (*,'(<n>I3)') petit_carre

!affichage de la constante prevue pour ce carre
WRITE (*,*)
WRITE (*,'(A,I4,A)') "Verification : la constante vaut",n*(n*n+1)/2
WRITE (*,*)

!sommes de controle : lignes, colonnes, diagonales
WRITE (*,*) "Les lignes ont pour somme :"

DO i=1,n
somme = 0
DO j=1,n
somme = somme + petit_carre(i,j)
ENDDO
WRITE (*,'(I4,$)') somme
ENDDO
WRITE (*,*)

WRITE (*,*) "Les colonnes ont pour somme :"

DO i=1,n
somme = 0
DO j=1,n
somme = somme + petit_carre(j,i)
ENDDO
WRITE (*,'(I4,$)') somme
ENDDO
WRITE (*,*)

WRITE (*,*) "Les diagonales ont pour somme :"

somme = 0
DO i=1,n
somme = somme + petit_carre(i,i)
ENDDO
WRITE (*,'(I4,$)') somme
somme = 0
DO i=1,n
somme = somme + petit_carre(n+1-i,i)
ENDDO
WRITE (*,'(I4)') somme

!liberation de la memoire
DEALLOCATE(grand_carre,petit_carre,c_haut,c_bas,c_gauche,c_droite)

!pause finale de la console
WRITE(*,*)
WRITE(*,*) "Appuyez sur <entree>"
READ (*,*)

END PROGRAM



TL
Back to top
Martin



Joined: 09 Sep 2004
Posts: 43

PostPosted: Tue Oct 26, 2004 7:35 am    Post subject: Write format Reply with quote

You can construct a format string at runtime using internal file I/O.

Here is an example:

program run_time_format
character*100 my_format
integer n
n = 2
write (my_format, "(A,I0,A)") "(", n, "I3)"
write (*, my_format) 43, 6
end
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group