Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Fortran Programming Language

printing matrix


Hi!
I'm real newby in fortran, and I can't get the print/Write routine...
I'm trying to print a matrix but I can't even get the row to print on
the same line.. my code result on a one long column..

       PROGRAM printMatrix

                real, dimension(16,16) :: a
                integer i,j
                open(unit=12,file="a.data")
                read(12,*) a
                do i= 1,16
                        do j=1,16
                                print *, a(j)
                        end do
                end do
       STOP
       END

how do I write row on the same line? is it possible to somehow keep same
format as it was in original file? (it's square matrix)
I would like to the same print out no matter how big matrices I would use.

Thank you in advance
L R

On May 25, 3:04 pm, Carramba <u@example.net> wrote:

> Hi!
> I'm real newby in fortran, and I can't get the print/Write routine...
> I'm trying to print a matrix but I can't even get the row to print on
> the same line.. my code result on a one long column..

>        PROGRAM printMatrix

>                 real, dimension(16,16) :: a
>                 integer i,j
>                 open(unit=12,file="a.data")
>                 read(12,*) a
>                 do i= 1,16

Try

                     write (*,"(1000(1x,f0.6))") a(i,:)

If you have questions about what the line above does, ask -- but first
read a book or tutorial on Fortran 90 or 95 (links are at
http://dmoz.org/Computers/Programming/Languages/Fortran/FAQs,_Help,_a...
), which will cover basic Fortran input and output. By default, the
output of each Fortran WRITE is sent to a new line, and to suppress
this behavior one can use ADVANCE="NO".

>                         do j=1,16

There should be two subscripts on "a" -- I don't see how the line
below compiled.

Carramba wrote:
> I'm real newby in fortran, and I can't get the print/Write routine...
> I'm trying to print a matrix but I can't even get the row to print on
> the same line.. my code result on a one long column..

(snip)

>               read(12,*) a
>               do i= 1,16
>                   do j=1,16
>                       print *, a(j)
>                   end do
>               end do

Normally each WRITE/PRINT starts a new line.

> how do I write row on the same line? is it possible to somehow keep same
> format as it was in original file? (it's square matrix)
> I would like to the same print out no matter how big matrices I would use.

It is not possible to format it the same as the input file, but
it is possible to print a square matrix.  As list directed that would be:

       do i=1,16
          print *,(a(i,j),j=1,16)
       enddo

That doesn't guarantee to put each row on the same line,
and it will also print it as the transpose of the matrix you
read in.

       read(12,*) a

Reads columnwise, that is, a(1,1) a(2,1) a(3,1) ... a(16,1) a(1,2) ...

       do i=1,16
          print *,(a(j,i),j=1,16)
       enddo

Will print elements in the same order as they were read in.

       do i=1,16
          write(*,'(1X,100G14.5)') (a(i,j),j=1,16)
       enddo

is more likely to print a square matrix, and will work even if you
increase the 16 up to 100.  Since most printers aren't that wide,
I usually don't try that.

       do i=1,16
          write(*,'(1X,100G14.5)') (a(i,j),j=1,8)
       enddo
       write(*,'(1X)')
       do i=1,16
          write(*,'(1X,100G14.5)') (a(i,j),j=9,16)
       enddo

Will print two groups of eight columns each.

Reverse the subscripts to (j,i) if you want them
to come out the other way around.

-- glen

Beliavsky skrev:

Thank you I got it now :)

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc