求每行和每列的总和

发布于 2025-01-12 06:10:53 字数 1080 浏览 0 评论 0原文

需要使用SUM()dim

sum()算法中的问题无法正确计算,我无法修复它,我需要有人帮助

    program main
  use environment

  implicit none
  
   character(*), parameter    :: input_file = "../data/input.txt", output_file = "output.txt"
   integer                    :: In = 0, Out = 0, rows = 0, columns = 0!, i = 0
   integer, allocatable       :: A(:,:)
   integer                    :: res_rows = 0, res_columns = 0 
         
   open (file=input_file, newunit=In)
    read(In, *) rows, columns
    allocate(A(rows, columns))
    read (In, *) A
   close (In)

   res_rows = sum(A(1:columns+1,1), dim=1)

   res_columns = sum(A(1:rows+1,1), dim=1)

   !outout data
   open (file=output_file, encoding=E_, newunit=Out, position='append')
      write(*,*)"rows:",res_rows
      write(*,*)"columns:",res_columns
   close (Out)

end program main

输入txt文件中的数据

4 3
  1  1  2 
  4  3  4 
  1  1  2 
  4  3  2 

将数据输出到txt文件

rows: 4 11 4 9
columns: 10 8 10

need to use SUM() and dim

the problem in the sum() algorithm does not calculate correctly, I can’t fix it, I need someone’s help

    program main
  use environment

  implicit none
  
   character(*), parameter    :: input_file = "../data/input.txt", output_file = "output.txt"
   integer                    :: In = 0, Out = 0, rows = 0, columns = 0!, i = 0
   integer, allocatable       :: A(:,:)
   integer                    :: res_rows = 0, res_columns = 0 
         
   open (file=input_file, newunit=In)
    read(In, *) rows, columns
    allocate(A(rows, columns))
    read (In, *) A
   close (In)

   res_rows = sum(A(1:columns+1,1), dim=1)

   res_columns = sum(A(1:rows+1,1), dim=1)

   !outout data
   open (file=output_file, encoding=E_, newunit=Out, position='append')
      write(*,*)"rows:",res_rows
      write(*,*)"columns:",res_columns
   close (Out)

end program main

input data from txt file

4 3
  1  1  2 
  4  3  4 
  1  1  2 
  4  3  2 

output data to txt file

rows: 4 11 4 9
columns: 10 8 10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2025-01-19 06:10:53

Fortran 是一种列主语言。您的 read(in,*) a 正在以错误的顺序填充矩阵。尝试写出矩阵 a 的第一行。您对 sum 内在函数的使用也是错误的。见下文。

program main

   implicit none
  
   character(*), parameter :: input_file = "a.dat"
   integer i, in, out, rows, columns
   integer, allocatable :: a(:,:)
   integer :: res_rows = 0, res_columns = 0 
         
   open(file=input_file, newunit=in, status='old')
   read(in, *) rows, columns
   allocate(a(rows, columns))
   do i = 1, rows
      read(in,*) a(i,:)
   end do
   close(in)

   print '(A,4(1X,I0))', 'Sum of each row:', sum(a,dim=2)
   do i = 1, rows
      print '(3I3,A,I0)', a(i,:),' = ', sum(a(i,:))
   end do
   print *

   print '(A,4(1X,I0))', 'Sum of each column:',  sum(a,dim=1)
   do i = 1, columns
      print '(4I3,A,I0)', a(:,i),' = ',sum(a(:,i))
   end do

end program main

Fortran is a column-major language. Your read(in,*) a is populating the matrix in the wrong order. Try writing out the first row of your matrix a. Your use of the sum intrinsic is also wrong. See below.

program main

   implicit none
  
   character(*), parameter :: input_file = "a.dat"
   integer i, in, out, rows, columns
   integer, allocatable :: a(:,:)
   integer :: res_rows = 0, res_columns = 0 
         
   open(file=input_file, newunit=in, status='old')
   read(in, *) rows, columns
   allocate(a(rows, columns))
   do i = 1, rows
      read(in,*) a(i,:)
   end do
   close(in)

   print '(A,4(1X,I0))', 'Sum of each row:', sum(a,dim=2)
   do i = 1, rows
      print '(3I3,A,I0)', a(i,:),' = ', sum(a(i,:))
   end do
   print *

   print '(A,4(1X,I0))', 'Sum of each column:',  sum(a,dim=1)
   do i = 1, columns
      print '(4I3,A,I0)', a(:,i),' = ',sum(a(:,i))
   end do

end program main

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文