kotlin 第一个程序_Kotlin程序添加两个矩阵

kotlin 第一个程序

Given two matrices, we have to add them.

给定两个矩阵,我们必须将它们相加。

Example:

例:

    Input:
matrix 1:
[2, 3]
[4, 5]
[7, 1]
matrix 2:
[4, 6]
[9, 0]
[7, 6]
Output:
[6, 9]
[13, 5]
[14, 7]    

在Kotlin中添加两个矩阵的程序 (Program to add two matrices in Kotlin)

package com.includehelp
import java.util.*
// Main function, Entry Point of Program
fun main(args: Array<String>) {
//variable of rows and col
val rows: Int
val column: Int
//Input Stream
val scanner = Scanner(System.`in`)
//Input no of rows and column
print("Enter the number of rows and columns of matrix : ")
rows   = scanner.nextInt()
column = scanner.nextInt()
//Create Array
val matrixA     = Array(rows) { IntArray(column) }
val matrixB     = Array(rows) { IntArray(column) }
val matrixSum   = Array(rows) { IntArray(column) }
//Input Matrix
println("Enter the Elements of First Matrix ($rows X $column} ): ")
for(i in matrixA.indices){
for(j in matrixA[i].indices){
print("matrixA[$i][$j]: ")
matrixA[i][j]=scanner.nextInt()
}
}
//Input Matrix
println("Enter the Elements of Second Matrix ($rows X $column} ): ")
for(i in matrixB.indices){
for(j in matrixB[i].indices){
print("matrixB[$i][$j]: ")
matrixB[i][j]=scanner.nextInt()
}
}
//print Matrix A
println("Matrix A : ")
for(i in matrixA.indices){
println("${matrixA[i].contentToString()} ")
}
//print Matrix B
println("Matrix B : ")
for(i in matrixB.indices){
println("${matrixB[i].contentToString()} ")
}
//Perform Addition
for(i in matrixSum.indices){
for(j in matrixSum[i].indices){
matrixSum[i][j]=matrixA[i][j]+matrixB[i][j]
}
}
//Print Sum of Matrices
println("Sum of the Matrices:")
for(i in matrixSum.indices){
println("${matrixSum[i].contentToString()} ")
}
}

Output

输出量

Run 1:
Enter the number of rows and columns of matrix : 3
2
Enter the Elements of First Matrix (3 X 2} ):
matrixA[0][0]: 2
matrixA[0][1]: 3
matrixA[1][0]: 4
matrixA[1][1]: 5
matrixA[2][0]: 7
matrixA[2][1]: 1
Enter the Elements of Second Matrix (3 X 2} ):
matrixB[0][0]: 4
matrixB[0][1]: 6
matrixB[1][0]: 9
matrixB[1][1]: 0
matrixB[2][0]: 7
matrixB[2][1]: 6
Matrix A :
[2, 3]
[4, 5]
[7, 1]
Matrix B :
[4, 6]
[9, 0]
[7, 6]
Sum of the Matrices:
[6, 9]
[13, 5]
[14, 7]
---------------
Run 2:
Enter the number of rows and columns of matrix : 3
3
Enter the Elements of First Matrix (3 X 3} ):
matrixA[0][0]: 2
matrixA[0][1]: 3
matrixA[0][2]: 4
matrixA[1][0]: 5
matrixA[1][1]: 6
matrixA[1][2]: 7
matrixA[2][0]: 0
matrixA[2][1]: 9
matrixA[2][2]: -7
Enter the Elements of Second Matrix (3 X 3} ):
matrixB[0][0]: 4
matrixB[0][1]: 0
matrixB[0][2]: -6
matrixB[1][0]: -3
matrixB[1][1]: 2
matrixB[1][2]: 3
matrixB[2][0]: 4
matrixB[2][1]: 5
matrixB[2][2]: 6
Matrix A :
[2, 3, 4]
[5, 6, 7]
[0, 9, -7]
Matrix B :
[4, 0, -6]
[-3, 2, 3]
[4, 5, 6]
Sum of the Matrices:
[6, 3, -2]
[2, 8, 10]
[4, 14, -1]

翻译自: https://www.includehelp.com/kotlin/add-two-matrices.aspx

kotlin 第一个程序

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377355.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ubuntu 切换用户的命令[shell, linux]

使用ubuntu过程中免不了和shell(终端)打交道, 也不可避免在各种用户之间进行切换, 从而实现对各帐户的管理, 这个就涉及到了一个比较基础又很重要的工作,怎么样切换用户, 对于LINUX老鸟来说,这个根本不值不提的东东却让新手挠头不已, 现在给出普通用户和超级用户切换的命令(附图…

曲苑杂坛--修改数据库名和文件组名

/* 该脚本示例如何完整的修改一个数据库的名称. 数据库为原名称为DB_BEIJING&#xff0c;需要修改成DB_SHANGHAI nzperfect 2012.12.19 */--判断是否存在同名的数据库&#xff0c;以防止误删除 USE master GO IF EXISTS (SELECT name FROM sys.databases WHERE name NDB_BEIJI…

关于new handler与default、delete关键字

在https://blog.csdn.net/qq_42604176/article/details/111638568的operate_new源代码长啥样中谈到过new handler。 当operator new不能够分配出申请的内存时&#xff0c;会抛出bad_alloc 异常。有的编译器会返回0. 当定义成new(nothrow) Foo&#xff1b;就不会抛异常&#xff…

模式匹配运算符–Shell

转载&#xff1a;http://www.firefoxbug.net/?p722 Var/home/firefox/MyProgram/fire.login.name ${Variable#pattern}:如果模式匹配于变量值的开头处&#xff0c;则删除匹配的最短部分&#xff0c;并且返回剩下的部分 例子&#xff1a; [fire]$ echo ${Var#*/} [fire]$ home/…

河内塔问题_河内塔的Python程序

河内塔问题You are challenged for a challenge to find the number of moves required to move a stack of disks from one peg to another peg. Wait for a second, it sounds easy? Let’s find are what is going on and in this article, we are introducing a chapter o…

VC6、BC5、G2.9标准分配器一览

目录VC6标准分配器BC5标准分配器G2.9标准分配器VC6标准分配器 VCx中源码可以在电脑路径中找&#xff1a; [D:\Program Files\VisualStudio\Community\VC\Tools\MSVC\14.28.29333\include\xmemory] 不过太多了。大概在837行左右有关于allocator代码。还是先看侯捷PPT上的吧。 …

【转】shell 大括号、圆括号的使用

在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量&#xff0c;命令的用法&#xff0c;如下&#xff1a; PHP 代码:1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:string},${var:string},${var:?string} 5.$((exp)) 6.$(var%pattern),$(var%%pattern),$(va…

css clear属性_CSS中的clear属性

css clear属性CSS | 清除财产 (CSS | clear Property) We know so much about float property and how it is used for styling our web pages. If you do not remember the float property, lets help jog your memory. The float property is used to set the elements in a …

linux find prune排除某目录或文件

http://blog.csdn.net/ysdaniel/article/details/7995681 查找cache目录下不是html的文件 find ./cache ! -name *.html -type f列出当前目录下的目录名,排除includes目录,后面的-print不能少 find . -path ./includes -prune -o -type d -maxdepth 1 -print排除多个目录,”(“…

嵌入式指针embedded pointer的概念以及用法

目录前言概念用法参考前言 在针对一个class写出它的内存管理池以及总结出allocator类(三个版本)中内存管理池的第二个版本中涉及到了一个非常重要的概念&#xff1a;嵌入式指针。同时嵌入式指针也在G2.9版本的alloc中出现。现在整理一下网上的一些用法和概念 概念 嵌入式指针…

CLI配置和编址

实施基本编址方案&#xff1a; 在设计新网络或规划现有网络时&#xff0c;至少要绘制一幅指示物理连接的拓扑图&#xff0c;以及一张列出以下信息的地址表&#xff1a; l 设备名称 l 设计中用到的接口 l IP 地址和子网掩码 l 终端设备&#xff08;如 PC&#xff09;的默…

sql语句中的in用法示例_PHP中的循环语句和示例

sql语句中的in用法示例循环 (Loops) Imagine that we need a program that says "hello world" 100 times. Its quite stressful and boring to write the statement -- echo "hello world" — 100 times in PHP. This is where loop statement facilitate…

love2d教程30--文件系统

在游戏里少不了文件操作&#xff0c;在love2d里我们可以直接用lua自带的io函数&#xff0c;如果不熟悉可以先读一下我的lua文件读写。 相对lua&#xff0c;love2d提供了更多的函数&#xff0c; 方便我们操作文件。不过可能处于安全考虑&#xff0c;love2d只允许我们访问两个目录…

std::alloc具体细节

G2.9 std::alloc的缺点&#xff1a; 1、在alloc::deallocate中没有将拿到的内存资源还给操作系统&#xff0c;在多任务中将占用很大资源 2、alloc::deallocate函数没有检查传入的p指针的有效性。在这里它默认p为alloc::allocate取得。 如果p并非alloc::allocate取得&#xf…

修改函数的返回地址

这篇随笔源自今天看的这篇文章http://www.cnblogs.com/bluesea147/archive/2012/05/19/2508208.html 1. 如何修改函数返回地址 今天主要写测试程序思考和验证了一下这个问题&#xff0c;先看一下这个&#xff23;程序 1 #include <stdio.h>2 void foo(){3 int a,…

调试JavaScript代码

JavaScript调试代码 (JavaScript debugging the code) Debugging is the process of finding mistakes or bugs in the program. There are several ways one can debug their JavaScript code. This article will walk you through the strict mode in JavaScript and excepti…

Delphi运算符及优先级

单目运算符 (最高优先级) 取变量或函数的地址(返回一个指针) not 逻辑取反或按位取反 乘除及按位运算符 * 相乘或集合交集 / 浮点相除 div 整数相除 mod 取模 (整数相除的余数) as 程序运行阶段类型转换 (RTTI运算符) and 逻辑或按位求和 shl 按位左移 shr 按位右移 加减运算符…

NotifyMyFrontEnd 函数背后的数据缓冲区(二)

message level 函数pq_putmessage调用 low level 函数 pq_putbytes,pq_putbytes调用 internal_putbytes。 从internal_putbyes上来看&#xff0c;就可以发现其数据发送的机制:有一个小技巧&#xff0c;如果数据缓冲区满了&#xff0c;就发送&#xff0c;否则就先堆在那儿。如果…

从源码角度剖析VC6下的内存分配与切割的运作

目录前言1、heap初始化2、第一次分配内存&#xff0c;计算真正区块大小3、new_region管理中心4、__sbh_alloc_new_group()切割第一次分配好的内存5、开始切割内存前言 malloc与free带来的内存管理是应付小区块的&#xff0c;即SBH(small block heap)&#xff0c;这点也可以从源…

windows常见命令整理(持续更新)

windows常见命令整理 1. 文件1.1. 实时显示文件 logfile.txt 中新添加的内容&#xff08;类似于linux tail -f&#xff09; 2. 网络2.1. netstat 3. 进程和任务3.1. tasklist &#xff08;用于列出当前运行的进程及其详细信息&#xff09;3.2. wmic &#xff08;用于执行各种系…