Multiplication of two 2x2 matrices in Fortran

Click For Summary
SUMMARY

The forum discussion addresses a Fortran program designed to multiply two 2x2 matrices using arrays and do loops. The code fails to produce correct results due to the uninitialized array C, which leads to garbage values being used in calculations. To resolve this issue, users must initialize the C array to zero before performing the multiplication. Proper indentation of the code is also recommended for improved readability.

PREREQUISITES
  • Fortran programming language knowledge
  • Understanding of arrays and loops in programming
  • Matrix multiplication concepts
  • Debugging techniques for initialization errors
NEXT STEPS
  • Learn about array initialization in Fortran
  • Explore advanced matrix operations in Fortran
  • Study debugging techniques for Fortran programs
  • Investigate best practices for code readability and indentation
USEFUL FOR

Students learning Fortran, educators teaching matrix operations, and developers seeking to improve their Fortran programming skills.

Parzeevahl
Messages
6
Reaction score
0
Homework Statement
Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.
Relevant Equations
arrays and do loops
I have tried to do this using arrays and do loops:
Fortran:
program matrixmul
    implicit none
    real A(2, 2), B (2, 2), C (2, 2)
    integer i, j, k
    write (*, *) 'Input: First matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) A (i, j)
    enddo
    enddo
    write (*, *) 'Input: Second matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) B (i, j)
    enddo
    enddo
    do i = 1, 2
    do j = 1, 2
    do k = 1, 2
    C (i, j) = C (i, j) + A (i, k) * B (k, j)
    enddo
    enddo
    enddo
    write (*, *) C
    end

So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
 
Physics news on Phys.org
Parzeevahl said:
Homework Statement: Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.
Homework Equations: arrays and do loops

I have tried to do this using arrays and do loops:
Fortran:
program matrixmul
    implicit none
    real A(2, 2), B (2, 2), C (2, 2)
    integer i, j, k
    write (*, *) 'Input: First matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) A (i, j)
    enddo
    enddo
    write (*, *) 'Input: Second matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) B (i, j)
    enddo
    enddo
    do i = 1, 2
    do j = 1, 2
    do k = 1, 2
    C (i, j) = C (i, j) + A (i, k) * B (k, j)
    enddo
    enddo
    enddo
    write (*, *) C
    end

So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
Out off topic, you should try to indent your code, it comes in handy for long programs.
 
  • Like
Likes   Reactions: berkeman
Parzeevahl said:
Fortran:
C (i, j) = C (i, j) + A (i, k) * B (k, j)
This line is very likely causing your error.
Before you start your do loops for multiplication, you should initialize the C array to all zero values. Otherwise each value of C(i, j) will be a garbage value, so each assignment statement in the innermost loop just adds a correct value to a garbage value, which results in a different garbage value.

Executing a statement like value = value + x is something you shouldn't do in any programming language if you haven't previously initialized value to some known value.

@archaic's suggestion of indenting each loop body is a good one. This makes your code easier for humans to read.
 
Last edited:
  • Like
Likes   Reactions: berkeman

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K