SlideShare a Scribd company logo
1 of 2
R. For some array of size, ROW by COLUMN, compute and display the average of the left-to-
right diagonal in java
Solution
import java.util.Scanner;
public class SumOfDiagonals {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row,column;
System.out.print("Enter row: ");
row = getInt(sc, "");
System.out.print("Enter column: ");
column = getInt(sc, "");
//Declare 2 Dimensional Arrays
int[][] matrix = new int[row][column];
int sum = 0;
// Input Matrix
System.out.println("Enter matrix ");
for (int i = 0; i < row; i++) {
System.out.println("Enter elements of " + i + " row");
for (int j = 0; j < column; j++) {
matrix[i][j] = getInt(sc, "");
}
}
// Compute Sum of main diagonal elements
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (i == j) {
sum += matrix[i][j];
}
}
}
int n;
if(row > column) n = column;
else n = row;
System.out.println("Sum of Diagonal Elements is " + sum);
System.out.println("Average of Diagonal Elements is " + sum/n);
}
public static int getInt(Scanner sc, String prompt) {
int integer = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextInt()) {
integer = sc.nextInt();
isValid = true;
} else {
System.err.println("Error! Invalid number. Try again.");
}
sc.nextLine();
}
return integer;
}
}

More Related Content

Similar to R- For some array of size- ROW by COLUMN- compute and display the aver.docx

ArrayOperations.java import java.util.Arrays; import java.util.pdf
ArrayOperations.java import java.util.Arrays; import java.util.pdfArrayOperations.java import java.util.Arrays; import java.util.pdf
ArrayOperations.java import java.util.Arrays; import java.util.pdf
ANANDSALESINDIA105
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
dorisc7
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
angelsfashion1
 
Hi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdfHi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdf
aniyathikitchen
 
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfHi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
anujsharmaanuj14
 

Similar to R- For some array of size- ROW by COLUMN- compute and display the aver.docx (9)

ArrayOperations.java import java.util.Arrays; import java.util.pdf
ArrayOperations.java import java.util.Arrays; import java.util.pdfArrayOperations.java import java.util.Arrays; import java.util.pdf
ArrayOperations.java import java.util.Arrays; import java.util.pdf
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
 
3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
 
Hi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdfHi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdf
 
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdfHi, Please find my codeimport java.util.Random;public class Pro.pdf
Hi, Please find my codeimport java.util.Random;public class Pro.pdf
 

More from diegor62

Introductory Aocounting u 2014 1- Managerial information- accounting m.docx
Introductory Aocounting u 2014 1- Managerial information- accounting m.docxIntroductory Aocounting u 2014 1- Managerial information- accounting m.docx
Introductory Aocounting u 2014 1- Managerial information- accounting m.docx
diegor62
 
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docxInterpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
diegor62
 
Internet network management framework as consisting of three major par.docx
Internet network management framework as consisting of three major par.docxInternet network management framework as consisting of three major par.docx
Internet network management framework as consisting of three major par.docx
diegor62
 
International business is conducted outside of the home country- Natio.docx
International business is conducted outside of the home country- Natio.docxInternational business is conducted outside of the home country- Natio.docx
International business is conducted outside of the home country- Natio.docx
diegor62
 
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docxIntermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
diegor62
 
Infrared radiation is also called IR- Describe how it is different fro (1).docx
Infrared radiation is also called IR- Describe how it is different fro (1).docxInfrared radiation is also called IR- Describe how it is different fro (1).docx
Infrared radiation is also called IR- Describe how it is different fro (1).docx
diegor62
 
Infrared radiation is also called IR- Describe how it is different fro.docx
Infrared radiation is also called IR- Describe how it is different fro.docxInfrared radiation is also called IR- Describe how it is different fro.docx
Infrared radiation is also called IR- Describe how it is different fro.docx
diegor62
 
Is my half equation and ionic equation from the answer key correct- I.docx
Is my half equation and ionic equation from the answer key correct- I.docxIs my half equation and ionic equation from the answer key correct- I.docx
Is my half equation and ionic equation from the answer key correct- I.docx
diegor62
 
Is it time for IPI to reconsider its salesforce organization by produc.docx
Is it time for IPI to reconsider its salesforce organization by produc.docxIs it time for IPI to reconsider its salesforce organization by produc.docx
Is it time for IPI to reconsider its salesforce organization by produc.docx
diegor62
 
INITIALS- 22- Which of the following represents a combustion reaction-.docx
INITIALS- 22- Which of the following represents a combustion reaction-.docxINITIALS- 22- Which of the following represents a combustion reaction-.docx
INITIALS- 22- Which of the following represents a combustion reaction-.docx
diegor62
 

More from diegor62 (20)

Introductory Aocounting u 2014 1- Managerial information- accounting m.docx
Introductory Aocounting u 2014 1- Managerial information- accounting m.docxIntroductory Aocounting u 2014 1- Managerial information- accounting m.docx
Introductory Aocounting u 2014 1- Managerial information- accounting m.docx
 
Introduction to Java Programming 10th ed --25-4 (Implement preorder t.docx
Introduction to Java Programming 10th ed  --25-4 (Implement preorder t.docxIntroduction to Java Programming 10th ed  --25-4 (Implement preorder t.docx
Introduction to Java Programming 10th ed --25-4 (Implement preorder t.docx
 
Indicate the functions of the agent process in a network operating sys.docx
Indicate the functions of the agent process in a network operating sys.docxIndicate the functions of the agent process in a network operating sys.docx
Indicate the functions of the agent process in a network operating sys.docx
 
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docxInterpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
Interpretation 102-4 in the AICPA Code of Professional Conduct calls f.docx
 
internet domain names are linked to trademark issues- Technology makes.docx
internet domain names are linked to trademark issues- Technology makes.docxinternet domain names are linked to trademark issues- Technology makes.docx
internet domain names are linked to trademark issues- Technology makes.docx
 
Internet network management framework as consisting of three major par.docx
Internet network management framework as consisting of three major par.docxInternet network management framework as consisting of three major par.docx
Internet network management framework as consisting of three major par.docx
 
Internet Programming ORM This question uses the following two model cl.docx
Internet Programming ORM This question uses the following two model cl.docxInternet Programming ORM This question uses the following two model cl.docx
Internet Programming ORM This question uses the following two model cl.docx
 
international ECO 305 course- how does culture embrace globalization a.docx
international ECO 305 course- how does culture embrace globalization a.docxinternational ECO 305 course- how does culture embrace globalization a.docx
international ECO 305 course- how does culture embrace globalization a.docx
 
International business is conducted outside of the home country- Natio.docx
International business is conducted outside of the home country- Natio.docxInternational business is conducted outside of the home country- Natio.docx
International business is conducted outside of the home country- Natio.docx
 
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docxIntermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
Intermolecular Interactions Crystalline Solids K14 of 19 Part A Classi.docx
 
Integrative Problems equire the integration of multiple concepts to fi.docx
Integrative Problems equire the integration of multiple concepts to fi.docxIntegrative Problems equire the integration of multiple concepts to fi.docx
Integrative Problems equire the integration of multiple concepts to fi.docx
 
Insight Communications Promotes employees with good technical skills i.docx
Insight Communications Promotes employees with good technical skills i.docxInsight Communications Promotes employees with good technical skills i.docx
Insight Communications Promotes employees with good technical skills i.docx
 
Infrared radiation is also called IR- Describe how it is different fro (1).docx
Infrared radiation is also called IR- Describe how it is different fro (1).docxInfrared radiation is also called IR- Describe how it is different fro (1).docx
Infrared radiation is also called IR- Describe how it is different fro (1).docx
 
Infrared radiation is also called IR- Describe how it is different fro.docx
Infrared radiation is also called IR- Describe how it is different fro.docxInfrared radiation is also called IR- Describe how it is different fro.docx
Infrared radiation is also called IR- Describe how it is different fro.docx
 
Initial Knowledge Check For each row in the table below- decide whethe.docx
Initial Knowledge Check For each row in the table below- decide whethe.docxInitial Knowledge Check For each row in the table below- decide whethe.docx
Initial Knowledge Check For each row in the table below- decide whethe.docx
 
Is the ability of the human ear better or worse than the sonic capabil.docx
Is the ability of the human ear better or worse than the sonic capabil.docxIs the ability of the human ear better or worse than the sonic capabil.docx
Is the ability of the human ear better or worse than the sonic capabil.docx
 
Is my half equation and ionic equation from the answer key correct- I.docx
Is my half equation and ionic equation from the answer key correct- I.docxIs my half equation and ionic equation from the answer key correct- I.docx
Is my half equation and ionic equation from the answer key correct- I.docx
 
Inorganic Chemistry - Wurtzite vs- Rock-Salt Structures Beryllium oxid.docx
Inorganic Chemistry - Wurtzite vs- Rock-Salt Structures Beryllium oxid.docxInorganic Chemistry - Wurtzite vs- Rock-Salt Structures Beryllium oxid.docx
Inorganic Chemistry - Wurtzite vs- Rock-Salt Structures Beryllium oxid.docx
 
Is it time for IPI to reconsider its salesforce organization by produc.docx
Is it time for IPI to reconsider its salesforce organization by produc.docxIs it time for IPI to reconsider its salesforce organization by produc.docx
Is it time for IPI to reconsider its salesforce organization by produc.docx
 
INITIALS- 22- Which of the following represents a combustion reaction-.docx
INITIALS- 22- Which of the following represents a combustion reaction-.docxINITIALS- 22- Which of the following represents a combustion reaction-.docx
INITIALS- 22- Which of the following represents a combustion reaction-.docx
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 

R- For some array of size- ROW by COLUMN- compute and display the aver.docx

  • 1. R. For some array of size, ROW by COLUMN, compute and display the average of the left-to- right diagonal in java Solution import java.util.Scanner; public class SumOfDiagonals { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int row,column; System.out.print("Enter row: "); row = getInt(sc, ""); System.out.print("Enter column: "); column = getInt(sc, ""); //Declare 2 Dimensional Arrays int[][] matrix = new int[row][column]; int sum = 0; // Input Matrix System.out.println("Enter matrix "); for (int i = 0; i < row; i++) { System.out.println("Enter elements of " + i + " row"); for (int j = 0; j < column; j++) { matrix[i][j] = getInt(sc, ""); } } // Compute Sum of main diagonal elements for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (i == j) { sum += matrix[i][j];
  • 2. } } } int n; if(row > column) n = column; else n = row; System.out.println("Sum of Diagonal Elements is " + sum); System.out.println("Average of Diagonal Elements is " + sum/n); } public static int getInt(Scanner sc, String prompt) { int integer = 0; boolean isValid = false; while (isValid == false) { System.out.print(prompt); if (sc.hasNextInt()) { integer = sc.nextInt(); isValid = true; } else { System.err.println("Error! Invalid number. Try again."); } sc.nextLine(); } return integer; } }