Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying a matriz of dynamic product

Status
Not open for further replies.

NTC0394

Programmer
Jun 19, 2013
21

I would like to modify the following program to fill the matriz on a sub-routine. But I would like to do it most general possible analising the possiblity that the matriz has no the same number of line and columns.

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


PROGRAM matriz_product_dynamic
! Example of dynamic dimensioning
! of bidimensional array

IMPLICIT none
INTEGER, ALLOCATABLE :: matriz ( :, : )
INTEGER :: siz_matriz, line, column
INTEGER :: situation_of_allocation

! Find the wanted size to a matriz
PRINT *, "what is the size of the matriz?"
READ *, siz_matriz

! allocate space enogh of matriz
ALLOCATE ( matriz(siz_matriz, &
siz_matriz), &
stat=situation_of_allocation )

! Variry if the space was allocated
IF(situation_of_allocation==0) THEN


! Fill the matriz
col: DO column = 1, siz_matriz
lin: DO line = 1, siz_matriz
matriz(line, column) = &
line * column

END DO lin
END DO col

! print in matriz one line at each time
DO line = 1, siz_matriz
PRINT *, (matriz( line, column ), &
column = 1, siz_matriz )

END DO

! release the memory when no longer necessary
DEALLOCATE ( matriz )

ELSE

PRINT *, " it was no possible allocate the memory for matriz "

END IF

END PROGRAM matriz_product_dynamic
 
When I tried modifying directly occorred a error. I need some tips, please.
 
If you want to allow for the possibility that the matrix is not square, then you will need to change this:

Code:
! allocate space enogh of matriz
ALLOCATE ( matriz(siz_matriz, &
siz_matriz), &
stat=situation_of_allocation )

to something like this:

Code:
ALLOCATE ( matrix(number_of_rows, number_of_columns)

This will require you to prompt the user for two numbers (instead of just siz_matriz) and change the bounds of your DO loops.
 
I want transforme this program into two: a subroutine and another program. About a subroutine, I want put the value inside it.
 
***I want put the value of variable* inside it
 
NTC0394 said:
About a subroutine, I want put the value inside it.
I don't really understand what you mean. Currently you fill the matrix like this
Code:
! Fill the matriz
 col: DO column = 1, siz_matriz
 lin: DO line = 1, siz_matriz
 matriz(line, column) = &
 line * column
i.e a[i,j] = i*j

What you want to do differently?
Do you need to read the matrix from a file, or from the user input, or what?
 
Maybe something like this?
Code:
[COLOR=#a020f0]program[/color] matrix
 [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:,:), [COLOR=#2e8b57][b]allocatable[/b][/color] :: A
 [COLOR=#2e8b57][b]integer[/b][/color] :: m, n

 [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]advance[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]'Number of rows    = '[/color]
 [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) m
 [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]advance[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]'Number of columns = '[/color]
 [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) n
 [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

 [COLOR=#804040][b]allocate[/b][/color](A(m,n))
 
 [COLOR=#a020f0]call[/color] read_matrix(A, [COLOR=#ff00ff]'A'[/color], m, n)
 [COLOR=#a020f0]call[/color] print_matrix(A, [COLOR=#ff00ff]'A'[/color], m, n)

 [COLOR=#804040][b]deallocate[/b][/color](A) 
[COLOR=#a020f0]end program[/color] matrix

[COLOR=#a020f0]subroutine[/color] read_matrix(matrix, matrix_name, m, n)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]  real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](m,n), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]inout[/b][/color]) :: matrix
  [COLOR=#2e8b57][b]character[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: matrix_name
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: m, n
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, j

  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], m
    [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]10[/color], [COLOR=#804040][b]advance[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'no'[/color]) matrix_name, i, j
      [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#804040][b]*[/b][/color]) matrix(i,j) 
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])  
  [COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color] (A, [COLOR=#ff00ff]'['[/color], I2[COLOR=#ff00ff].2[/color], [COLOR=#ff00ff]','[/color], I2[COLOR=#ff00ff].2[/color], [COLOR=#ff00ff]'] = '[/color])
[COLOR=#a020f0]end subroutine[/color] read_matrix

[COLOR=#a020f0]subroutine[/color] print_matrix(matrix, matrix_name, m, n)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color] :: matrix_name
[COLOR=#2e8b57][b]  real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](m,n), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: matrix
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: m, n
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, j

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]10[/color]) matrix_name
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], m
    [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'(f8.2)'[/color], [COLOR=#804040][b]advance[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'no'[/color]) matrix(i,j)
    [COLOR=#804040][b]end do[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
  [COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color] ([COLOR=#ff00ff]'Msatrix '[/color], A, [COLOR=#ff00ff]' = '[/color])
[COLOR=#a020f0]end subroutine[/color] print_matrix

Output:
Code:
$ gfortran matrix.f95 -o matrix

$ matrix
Number of rows    = 3
Number of columns = 2

A[01,01] = 11
A[01,02] = 12
A[02,01] = 21
A[02,02] = 22
A[03,01] = 31
A[03,02] = 32

Msatrix A = 
   11.00   12.00
   21.00   22.00
   31.00   32.00
 
Hi mikrom ! Thanks for your help

I don't really understand what you mean. Currently you fill the matrix like this
CODE

"! Fill the matriz
col: DO column = 1, siz_matrizut
lin: DO line = 1, siz_matriz
matriz(line, column) = &
line * column
i.e a[i,j] = i*j

What you want to do differently?
Do you need to read the matrix from a file, or from the user input, or what? "

I need read the matrix from the user input.
About the columns and lines, I need multiply line*column like the first program.
Your program you created is amazing but I wanted a programa as simple as possible, so I need a program without FORMAT and ADVANCED.
Is it possible? And how do this?

Thanks !
 
NTC0394 said:
I need read the matrix from the user input.
...
I need a program without FORMAT and ADVANCED.
The program I posted above reads from user input. You can replace all formats with * and delete advance='no'.
 
I need read the matrix from the user input.
...
I need multiply line*column like the first program.
These two sentences contradict each other...
Either you read the matrix elements from user input or you can generate them as a[sub]i,j[/sub] = i*j, but no both.

Or maybe you have to read 2 matrices A, B and create the matrix product C = A*B ?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top