SlideShare a Scribd company logo
1 of 2
Download to read offline
Need to make a flowchart for the following code:
public static void main(String[] args) {
int num_cities;
double[][] city_coordinates;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of cities: ");
num_cities = in.nextInt();
city_coordinates = new double[num_cities][2];
System.out.print("Enter the coordinates of the cities: ");
for (int i = 0; i < city_coordinates.length; i++) {
city_coordinates[i][0] = in.nextDouble();
city_coordinates[i][1] = in.nextDouble();
}
// Get the city with the smallest distance to the all other cities
double min_distance = Double.MAX_VALUE;
int min_index = 0;
for (int i = 0; i < city_coordinates.length; i++) {
double distance = 0;
for (int j = 0; j < city_coordinates.length; j++) {
if (i != j) {
distance += Math.sqrt(Math.pow(city_coordinates[i][0] - city_coordinates[j][0], 2)
+ Math.pow(city_coordinates[i][1] - city_coordinates[j][1], 2));
}
}
if (distance < min_distance) {
min_distance = distance;
min_index = i;
}
}
// Get total distance to all other cities
double total_distance = 0;
for (int i = 0; i < city_coordinates.length; i++) {
if (i != min_index) {
total_distance += Math.sqrt(Math.pow(city_coordinates[min_index][0] - city_coordinates[i][0],
2)
+ Math.pow(city_coordinates[min_index][1] - city_coordinates[i][1], 2));
}
}
System.out.printf("The central city is at (%.1f, %.1f)n", city_coordinates[min_index][0],
city_coordinates[min_index][1]);
System.out.printf("The total distance to all other cities is %.2fn", total_distance);
}
}

More Related Content

Similar to Need to make a flowchart for the following codepublic static voi.pdf

FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
actocomputer
 
JAVAImplement the DFS algorithm. The implementation should be able.pdf
JAVAImplement the DFS algorithm. The implementation should be able.pdfJAVAImplement the DFS algorithm. The implementation should be able.pdf
JAVAImplement the DFS algorithm. The implementation should be able.pdf
ezycolours78
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
MaruMengesha
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
Katecate1
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
AAKASH KUMAR
 

Similar to Need to make a flowchart for the following codepublic static voi.pdf (20)

C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
 
JAVAImplement the DFS algorithm. The implementation should be able.pdf
JAVAImplement the DFS algorithm. The implementation should be able.pdfJAVAImplement the DFS algorithm. The implementation should be able.pdf
JAVAImplement the DFS algorithm. The implementation should be able.pdf
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_17-Feb-2021_L8-...
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
Railway reservation(c++ project)
Railway reservation(c++ project)Railway reservation(c++ project)
Railway reservation(c++ project)
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
Railway reservation(c++ project)
Railway reservation(c++ project)Railway reservation(c++ project)
Railway reservation(c++ project)
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
Nonlinear 2nd order analysis of 2 d fixed support beam with plastic hinge con...
Nonlinear 2nd order analysis of 2 d fixed support beam with plastic hinge con...Nonlinear 2nd order analysis of 2 d fixed support beam with plastic hinge con...
Nonlinear 2nd order analysis of 2 d fixed support beam with plastic hinge con...
 

More from faktdeal

Natalie and her friend Curtis Lesperance decide that they can benefit.pdf
Natalie and her friend Curtis Lesperance decide that they can benefit.pdfNatalie and her friend Curtis Lesperance decide that they can benefit.pdf
Natalie and her friend Curtis Lesperance decide that they can benefit.pdf
faktdeal
 
Mary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdf
Mary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdfMary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdf
Mary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdf
faktdeal
 

More from faktdeal (7)

On April 1, 2024, Washtenaw Corporation realizes that one of its main.pdf
On April 1, 2024, Washtenaw Corporation realizes that one of its main.pdfOn April 1, 2024, Washtenaw Corporation realizes that one of its main.pdf
On April 1, 2024, Washtenaw Corporation realizes that one of its main.pdf
 
On 112014, Otter Group purchased 60 (60000 shares) of Dolphin Ltd.pdf
On 112014, Otter Group purchased 60 (60000 shares) of Dolphin Ltd.pdfOn 112014, Otter Group purchased 60 (60000 shares) of Dolphin Ltd.pdf
On 112014, Otter Group purchased 60 (60000 shares) of Dolphin Ltd.pdf
 
New workbooks typically contain how many worksheets 0123.pdf
New workbooks typically contain how many worksheets 0123.pdfNew workbooks typically contain how many worksheets 0123.pdf
New workbooks typically contain how many worksheets 0123.pdf
 
Natalie and her friend Curtis Lesperance decide that they can benefit.pdf
Natalie and her friend Curtis Lesperance decide that they can benefit.pdfNatalie and her friend Curtis Lesperance decide that they can benefit.pdf
Natalie and her friend Curtis Lesperance decide that they can benefit.pdf
 
Note In this problem, you are going to explore some techniques in ev.pdf
Note In this problem, you are going to explore some techniques in ev.pdfNote In this problem, you are going to explore some techniques in ev.pdf
Note In this problem, you are going to explore some techniques in ev.pdf
 
On January 1, 2022, Oriole Company reported stockholders equity .pdf
On January 1, 2022, Oriole Company reported stockholders equity .pdfOn January 1, 2022, Oriole Company reported stockholders equity .pdf
On January 1, 2022, Oriole Company reported stockholders equity .pdf
 
Mary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdf
Mary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdfMary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdf
Mary Dvorak organized Mullin Enterprises, Inc., in January 2019. .pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 

Need to make a flowchart for the following codepublic static voi.pdf

  • 1. Need to make a flowchart for the following code: public static void main(String[] args) { int num_cities; double[][] city_coordinates; Scanner in = new Scanner(System.in); System.out.print("Enter the number of cities: "); num_cities = in.nextInt(); city_coordinates = new double[num_cities][2]; System.out.print("Enter the coordinates of the cities: "); for (int i = 0; i < city_coordinates.length; i++) { city_coordinates[i][0] = in.nextDouble(); city_coordinates[i][1] = in.nextDouble(); } // Get the city with the smallest distance to the all other cities double min_distance = Double.MAX_VALUE; int min_index = 0; for (int i = 0; i < city_coordinates.length; i++) { double distance = 0; for (int j = 0; j < city_coordinates.length; j++) { if (i != j) { distance += Math.sqrt(Math.pow(city_coordinates[i][0] - city_coordinates[j][0], 2) + Math.pow(city_coordinates[i][1] - city_coordinates[j][1], 2)); } } if (distance < min_distance) { min_distance = distance; min_index = i; } } // Get total distance to all other cities double total_distance = 0; for (int i = 0; i < city_coordinates.length; i++) { if (i != min_index) {
  • 2. total_distance += Math.sqrt(Math.pow(city_coordinates[min_index][0] - city_coordinates[i][0], 2) + Math.pow(city_coordinates[min_index][1] - city_coordinates[i][1], 2)); } } System.out.printf("The central city is at (%.1f, %.1f)n", city_coordinates[min_index][0], city_coordinates[min_index][1]); System.out.printf("The total distance to all other cities is %.2fn", total_distance); } }