SlideShare a Scribd company logo
1 of 2
A matrix with M rows and N columns is actually an array of length M, each entry of which is an
array of length N. In a two-dimensional Java array, we can use the code ali] to refer to the ith
row (which is a one-dimensional array) For example, a 3x3 matrix is represented by the
following array: int my Matrix 129 28, 27 116 15, 14), 13, 2, 1) Write the matrixAdd method.
This method takes two two-dimensional Java integer arrays as parameters, adds and returns a
new two-dimensional Java integer array. Note: you can assume that the printIntArray method is
given for you in this question and the two parameters arrays are of equal length
Solution
Answer:
import java.util.*;
class matrixAdd {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter value for rows in matrix for addition ");
int horizontals = s.nextInt();
System.out.println("Enter value for columns in matrix for addition");
int verticals = s.nextInt();
int[][] a = new int[horizontals][verticals];
int[][] b = new int[horizontals][verticals];
System.out.println("Enter the first matrix");
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the second matrix");
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
b[i][j] = s.nextInt();
}
}
int[][] c = new int[horizontals][verticals];
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The result of the two matrices after addition is");
for (int i = 0; i < horizontals; i++) {
for (int j = 0; j < verticals; j++) {
System.out.print(c[i][j] + " ");
}
}
}
}

More Related Content

Similar to A matrix with M rows and N columns is actually an array of length M- e.docx

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Basics of matlab
Basics of matlabBasics of matlab
Basics of matlab
Anil Maurya
 

Similar to A matrix with M rows and N columns is actually an array of length M- e.docx (20)

Structured Data Type Arrays
Structured Data Type ArraysStructured Data Type Arrays
Structured Data Type Arrays
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
WAP to add two given matrices in Java
WAP to add two given matrices in JavaWAP to add two given matrices in Java
WAP to add two given matrices in Java
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
 
1. Introduction.pptx
1. Introduction.pptx1. Introduction.pptx
1. Introduction.pptx
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 
Programming in Java: Arrays
Programming in Java: ArraysProgramming in Java: Arrays
Programming in Java: Arrays
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
Arrays
ArraysArrays
Arrays
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Section-7.4-PC.ppt
Section-7.4-PC.pptSection-7.4-PC.ppt
Section-7.4-PC.ppt
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Matrix
MatrixMatrix
Matrix
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Basics of matlab
Basics of matlabBasics of matlab
Basics of matlab
 

More from kaseya1

A number of monks live in a monastery and they do not communicate in a.docx
A number of monks live in a monastery and they do not communicate in a.docxA number of monks live in a monastery and they do not communicate in a.docx
A number of monks live in a monastery and they do not communicate in a.docx
kaseya1
 
A photographer works part-time in a shopping center- 2 hours per day-.docx
A photographer works part-time in a shopping center- 2 hours per day-.docxA photographer works part-time in a shopping center- 2 hours per day-.docx
A photographer works part-time in a shopping center- 2 hours per day-.docx
kaseya1
 

More from kaseya1 (15)

A number of monks live in a monastery and they do not communicate in a.docx
A number of monks live in a monastery and they do not communicate in a.docxA number of monks live in a monastery and they do not communicate in a.docx
A number of monks live in a monastery and they do not communicate in a.docx
 
A molecule or ion that donates the hydrogen in a hydrogen bond is a hy (1).docx
A molecule or ion that donates the hydrogen in a hydrogen bond is a hy (1).docxA molecule or ion that donates the hydrogen in a hydrogen bond is a hy (1).docx
A molecule or ion that donates the hydrogen in a hydrogen bond is a hy (1).docx
 
A person talking produces a sound level of 65 dB at a distance of 50 c.docx
A person talking produces a sound level of 65 dB at a distance of 50 c.docxA person talking produces a sound level of 65 dB at a distance of 50 c.docx
A person talking produces a sound level of 65 dB at a distance of 50 c.docx
 
A metal plate is illuminated with light of a certain frequency- Which.docx
A metal plate is illuminated with light of a certain frequency- Which.docxA metal plate is illuminated with light of a certain frequency- Which.docx
A metal plate is illuminated with light of a certain frequency- Which.docx
 
A Martian leaves Mars in a spaceship that is heading to Venus- On the.docx
A Martian leaves Mars in a spaceship that is heading to Venus- On the.docxA Martian leaves Mars in a spaceship that is heading to Venus- On the.docx
A Martian leaves Mars in a spaceship that is heading to Venus- On the.docx
 
a major change in our study of differing economic systems in the era o.docx
a major change in our study of differing economic systems in the era o.docxa major change in our study of differing economic systems in the era o.docx
a major change in our study of differing economic systems in the era o.docx
 
A leadership-motivation case is provided below- The case includes impo.docx
A leadership-motivation case is provided below- The case includes impo.docxA leadership-motivation case is provided below- The case includes impo.docx
A leadership-motivation case is provided below- The case includes impo.docx
 
A kettle is filled with tap water and placed on a stove- The stove ele.docx
A kettle is filled with tap water and placed on a stove- The stove ele.docxA kettle is filled with tap water and placed on a stove- The stove ele.docx
A kettle is filled with tap water and placed on a stove- The stove ele.docx
 
A photographer works part-time in a shopping center- 2 hours per day-.docx
A photographer works part-time in a shopping center- 2 hours per day-.docxA photographer works part-time in a shopping center- 2 hours per day-.docx
A photographer works part-time in a shopping center- 2 hours per day-.docx
 
Consider the following definitions for linked lists for problems 6 thr.docx
Consider the following definitions for linked lists for problems 6 thr.docxConsider the following definitions for linked lists for problems 6 thr.docx
Consider the following definitions for linked lists for problems 6 thr.docx
 
Consider the following chemical compounds- Classify each as either an.docx
Consider the following chemical compounds- Classify each as either an.docxConsider the following chemical compounds- Classify each as either an.docx
Consider the following chemical compounds- Classify each as either an.docx
 
Consider the plane R2 and consider any two lines that pass through the.docx
Consider the plane R2 and consider any two lines that pass through the.docxConsider the plane R2 and consider any two lines that pass through the.docx
Consider the plane R2 and consider any two lines that pass through the.docx
 
Consider the following statements about the investment in working capi.docx
Consider the following statements about the investment in working capi.docxConsider the following statements about the investment in working capi.docx
Consider the following statements about the investment in working capi.docx
 
Consider the following method definitions intended to find the smalles.docx
Consider the following method definitions intended to find the smalles.docxConsider the following method definitions intended to find the smalles.docx
Consider the following method definitions intended to find the smalles.docx
 
Consider relations R and S as following and answer question- R A B 1 2 (8).docx
Consider relations R and S as following and answer question- R A B 1 2 (8).docxConsider relations R and S as following and answer question- R A B 1 2 (8).docx
Consider relations R and S as following and answer question- R A B 1 2 (8).docx
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

A matrix with M rows and N columns is actually an array of length M- e.docx

  • 1. A matrix with M rows and N columns is actually an array of length M, each entry of which is an array of length N. In a two-dimensional Java array, we can use the code ali] to refer to the ith row (which is a one-dimensional array) For example, a 3x3 matrix is represented by the following array: int my Matrix 129 28, 27 116 15, 14), 13, 2, 1) Write the matrixAdd method. This method takes two two-dimensional Java integer arrays as parameters, adds and returns a new two-dimensional Java integer array. Note: you can assume that the printIntArray method is given for you in this question and the two parameters arrays are of equal length Solution Answer: import java.util.*; class matrixAdd { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter value for rows in matrix for addition "); int horizontals = s.nextInt(); System.out.println("Enter value for columns in matrix for addition"); int verticals = s.nextInt(); int[][] a = new int[horizontals][verticals]; int[][] b = new int[horizontals][verticals]; System.out.println("Enter the first matrix"); for (int i = 0; i < horizontals; i++) { for (int j = 0; j < verticals; j++) { a[i][j] = s.nextInt(); } } System.out.println("Enter the second matrix"); for (int i = 0; i < horizontals; i++) { for (int j = 0; j < verticals; j++) { b[i][j] = s.nextInt(); } }
  • 2. int[][] c = new int[horizontals][verticals]; for (int i = 0; i < horizontals; i++) { for (int j = 0; j < verticals; j++) { c[i][j] = a[i][j] + b[i][j]; } } System.out.println("The result of the two matrices after addition is"); for (int i = 0; i < horizontals; i++) { for (int j = 0; j < verticals; j++) { System.out.print(c[i][j] + " "); } } } }