Fortran学习笔记:03 数组(Array)
Fortran学习笔记目录
书接上回:Fortran学习笔记:02 流控制语句
数组(Array)
定义数组
一维数组
program main
implicit none
integer :: i
integer :: a(10)
do i=1,10,1
a(i)=i*i;
end do
do i=1,10,1
write(*,*) "i=",i,"i*i=",a(i)
end do
end
数组申明格式:
Datatype name(size)
注意,size必须为常数,数组索引默认从1开始
多维数组
最多可以声明7维数组,声明格式
integer :: a(D1,D2,D3,...,Dn) !n维数组
其他数组声明方式
没有特别赋值的情况下,数组的默认索引是从1开始的,例如
integer :: a(3) !包含a(1),a(2),a(3)
自定义范围声明
integer :: a(0:2) !一维数组包含a(0),a(1),a(2)
integer :: b(2:3,-1:1) !二维数组b(2~3,-1~1)都可以使用
赋初值
直接赋初值
integer :: a(row,col)=0
program main
implicit none
integer ,parameter:: row=10
integer ,parameter:: col=10
integer :: a(row,col)=0
integer :: i,j
do i=1,row,1
do j=1,col,1
write(*,*) a(i,j)
end do
end do
end
对数组的操作
Fortran 95拥有强大的数组操作方法,简化许多for循环操作
等号赋值
integer :: a(10),b(10)
!!数组赋值!!
a=10
!等效于
do i=1,10,1
a(i)=10
end do
!!数组复制!!
b=a
!等效于
do i=1,10,1
b(i)=a(i)
end do
矩阵基本运算
\(+,-,*,/\)基本四则运算与Matlab中点乘、点除方法相同,均为数组对应元素进行四则运算
数组操作 | 意义 |
---|---|
\(a=b+c\) | \(a(i,j)=b(i,j)+c(i,j)\)矩阵对应位置数值相加 |
\(a=b-c\) | \(a(i,j)=b(i,j)-c(i,j)\)矩阵对应位置数值相减 |
\(a=b*c\) | \(a(i,j)=b(i,j)*c(i,j)\)矩阵对应位置数值相乘 |
\(a=b/c\) | \(a(i,j)=b(i,j)/c(i,j)\)矩阵对应位置数值相除以 |
program main
implicit none
integer ,parameter:: row=10
integer ,parameter:: col=10
integer :: a(row,col)
real :: b(row,col),c(row,col)
integer :: i,j
a=10
b=.5
c=a*b
do i=1,row,1
do j=1,col,1
write(*,*) c(i,j)
end do
end do
end
矩阵切片运算
可以通过对矩阵中部分数据进行赋值操作,Matlab中矩阵切片方式继承了Fortran语言这一形式。
a(3,5)=5 !a(3),a(4),a(5)数值设置为5
a(3:)=5 !a(3)之后的数值设置为5
a(3:5)=(/3,4,5/) !a(3),a(4),a(5)数值分别设为3,4,5
a(1:3)=b(4:6) !a,b赋值
a(1:5:2)=3 !a(1),a(3),a(5)赋值为3,1~5增量为2
a(1:10)=a(10:1:-1) !翻转a
integer :: a(5),b(5,5)
a(:)=b(:,2) !b中第二列赋值给a
integer :: a(5,5),b(5,5,5)
a(:,:)=b(:,:,1)
注意在进行数组操作时,需要保证两边数组数目一致
WHERE 语句
where是Fortran95添加的功能,可以通过逻辑判断,对数组部分元素进行操作。例如以下例子,可以将a中<3的数组元素赋值给b
program main
implicit none
integer :: i
integer :: a(5)=(/(i,i=1,5)/)
integer :: b(5)=0
where(a<3)
b=a
end where
write(*,*) "a=",a
write(*,*) "b=",b
stop
end
!输出
a= 1 2 3 4 5
b= 1 2 0 0 0
where也可以作嵌套,elsewhere(逻辑判断)
program main
implicit none
integer :: i
integer :: a(5)=(/(i,i=1,5)/)
integer :: b(5)=0
where(a<3)
b=1
elsewhere(a>4)
b=2
elsewhere
b=3
end where
write(*,*) "a=",a
write(*,*) "b=",b
end
!输出
a= 1 2 3 4 5
b= 1 1 3 3 2
FORALL语句
forall是Fortran95中添加的功能
program main
implicit none
integer :: i
integer :: a(5)=0
forall(i=1:5:2) !1~5,增量为2
a(i)=5
end forall
write(*,*) "a=",a
forall(i=1:5)
a(i)=i
end forall
write(*,*) "a=",a
stop
end
!输出
a= 5 0 5 0 5
a= 1 2 3 4 5
对于二维数组,有相同的操作
program main
implicit none
integer :: i,j
integer :: a(5,5)=0
forall(i=1:5:1,j=1:5:1)
a(i,j)=i+j
end forall
do i=1,5,1
write(*,*) a(:,i)
end do
stop
end
!输出
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
还可以在forall中添加条件判断,语法为
forall(索引1,索引2,逻辑判断)
program main
implicit none
integer :: i,j
integer,parameter :: N = 5
integer :: a(N,N)=0
forall(i=1:N,j=1:N,i>j) a(i,j)=1 !上三角
forall(i=1:N,j=1:N,i==j) a(i,j)=2 !对角线
forall(i=1:N,j=1:N,i<j) a(i,j)=3 !下三角
write(*,"(5(5I5,/))") a
stop
end
!输出
2 1 1 1 1
3 2 1 1 1
3 3 2 1 1
3 3 3 2 1
3 3 3 3 2
forall可以嵌套多层,forall中可以使用where,但是where中不能使用forall,例如
program main
implicit none
integer :: i,j
integer,parameter :: N = 5
integer :: a(N,N)=0
forall(i=1:N)
forall(j=1:N)
a(i,j)=i+j
end forall
end forall
write(*,"(5(5I5,/))") a
forall(i=1:N)
where(a(i,:)/=6)
a(i,:)=0
end where
end forall
write(*,"(5(5I5,/))") a
stop
end
!输出
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
0 0 0 0 6
0 0 0 6 0
0 0 6 0 0
0 6 0 0 0
6 0 0 0 0
数组的存储规则
可变大小的数组
利用allocate与deallocate分配与释放内存
program main
implicit none
integer :: students
integer,allocatable :: a(:)
integer :: i
write(*,*) "How many students:"
read(*,*) students
allocate(a(students)) !分配内存
write(*,*) "size of array = ",size(a)
deallocate(a) !释放内存
stop
end
测试程序(测试当前计算机所能分配的最大内存)
program main
implicit none
integer :: size_N=0,error=0
integer,parameter :: one_mb=1024*1024
character,allocatable :: a(:)
do while(.true.)
size_N=size_N+one_mb
allocate(a(size_N),stat=error) !分配内存
if(error/=0) then
exit
end if
write(*,"('Allocate ',I10,'bytes')") size_N
write(*,"(F10.2,'MB used')") real(size_N)/one_mb
deallocate(a) !释放内存
end do
stop
end
数组应用实例
冒泡排序
program main
implicit none
integer :: a(5)=(/10,2,5,4,6/)
integer :: i,j,temp
write(*,"(5(I5))") a
do i=1,5
do j=i+1,5
if(a(i)>a(j)) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
end do
end do
write(*,"(5(I5))") a
stop
end program
!输出
10 2 5 4 6
2 4 5 6 10
Fortran学习笔记:03 数组(Array)的更多相关文章
- [学习笔记]JS 数组Array push相关问题
前言: 今天用写了一个二维数组,都赋值为零,然后更新其中一个值,结果和预期是不一样,会整列的相同位置都是同一个值. 1.用Chrome的控制台样例如下: arrs[2][2] =1的赋值,竟然是三个数 ...
- Java学习笔记 03 数组
一.数组的创建和使用 数组的创建和使用 >>创建方法1:先声明,再用new关键字分配内存(使用new关键字分配内存,整形数组中各个元素的初始值都为0) String str[]; str= ...
- PHP学习笔记之数组篇
摘要:其实PHP中的数组和JavaScript中的数组很相似,就是一系列键值对的集合.... 转载请注明来源:PHP学习笔记之数组篇 一.如何定义数组:在PHP中创建数组主要有两种方式,下面就让我 ...
- (目录)Fortran学习笔记:开坑!!!
前言:因为某些原因,需要使用Fortran编写程序,记录下Fortran语法学习过程中的部分笔记.在此开坑记录,立下Flag,"希望年末能够更新完" Fortran 学习笔记 陈橙 ...
- JavaScript学习笔记之数组(二)
JavaScript学习笔记之数组(二) 1.['1','2','3'].map(parseInt) 输出什么,为什么? ['1','2','3'].map(parseInt)//[1,NaN,NaN ...
- Fortran学习笔记4(循环语句)
Fortran学习笔记4 Fortran学习笔记4 逻辑运算 循环 Do语句 Do-While循环 循环控制 循环应用实例 逻辑运算 if命令需要和逻辑运算表达式搭配才能起到很好的效果.下面分别列出F ...
- SaToken学习笔记-03
SaToken学习笔记-03 如果排版有问题,请点击:传送门 核心思想 所谓权限验证,验证的核心就是一个账号是否拥有一个权限码 有,就让你通过.没有?那么禁止访问! 再往底了说,就是每个账号都会拥有一 ...
- Fortran学习笔记:01 基本格式与变量声明
Fortran学习笔记目录 01 基本格式与变量声明 格式 固定格式(Fixed Format):Fortran77 程序需要满足一种特定的格式要求,具体形式参考教材 自由格式(Free Format ...
- 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试
机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理.源码解析及测试 关键字:决策树.python.源码解析.测试作者:米仓山下时间:2018-10-2 ...
随机推荐
- JFrame显示刷新
1 import java.awt.BorderLayout; 2 import java.awt.Font; 3 import java.awt.event.ActionEvent; 4 impor ...
- Ecplise项目导入IDEA(纯小白名词解释)
1. Module 模块 一个大的项目不仅仅是只有Java的源文件,还有数据库,服务器,web等等文件一起使用,将类似于这样分类的文件定义为 module 例如 core Module(核心).web ...
- 使用junit进行最简单的单元测试
使用junit进行最简单的单元测试 使用工具: jdk IDEA Maven 第一步 创建一个Maven项目 第二步 导入junit依赖 <dependency> <groupId& ...
- SQL查询语句执行流程
msyql执行流程 你有个最简单的表,表里只有一个 ID 字段,在执行下面这个查询语句时:: select * from T where ID=10: 我们看到的只是输入一条语句,返回一个结果,却不知 ...
- Linux 网卡驱动sk_buff内核源码随笔
这几天在调试有关网卡驱动的东西,有很多地方不清楚.有关网卡驱动部分主要有两个很重要的结构体:struct net_device 和struct sk_buff. 驱动大部分都是围绕这两个东西进行操作的 ...
- 了解HTTP基本知识板块
一.HTTP 协议概述 HTTP协议采用了请求/响座模型. 客户端向服务器发送-个请求,请求头包含请求的方法.URT..协议版本.以以 及包含请求修饰符.客户信息和内容的类似于MIME的消息结构. 服 ...
- C# 获取应用程序内存
double usedMemory = 0; Process p = Process.GetProcesses().Where(x => x.ProcessName.Co ...
- bash-completion linux命令补全
1.有时候用docker run 或者kubectl 想tab补全的时候用不了 这个时候可以安装一个神奇的包bash-completion yum install bash-completion 2. ...
- 安卓gradle时报错"ERROR: Plugin with id 'com.android.application' not found."
在build.gradle中更改gradle插件版本号 buildscript { repositories { google() jcenter() } dependencies { //版本号请根 ...
- 2021ICPC网络赛第一场部分题解-The 2021 ICPC Asia Regionals Online Contest (I)
写在前面 本来应该6题的,结果不知道哪个铸币发了H的clar,当即把我们的思路转向三维几何上.当时我们还在想这三维计算几何的正确率有点太高了还在感叹ICPC选手的含金量,直到赛后我才知道这H题的铸币出 ...