In: Computer Science
Nobody wants a nuclear power plant in their neighborhood. Unfortunately,
the governor must locate a nuclear power plant somewhere inside a 25 square
mile area containing four cities. The governor, being primarily concerned
with reelection, desires to locate the plant in the location which will
cause the least amount of unhappiness among the constituents. In this lab,
you will write a program to assist the governor in this difficult decision.
METHOD:
^^^^^^^
Assume each city is located in an integer-pair (x, y) valued location
within a 25 x 25 square mile area. For example, a city can be located at
(4, 19), but not at (4.5, 19.8). In other words, there are no real valued
coordinate for a location of a city.
Your program will consider each integer-pair valued location within the area
as a possible site for the nuclear plant, starting at (1, 1) and continuing
across each row until the final site (25, 25) is reached.
At each possible location for the nuclear plant, your program will compute
the average unhappiness if the plant were located there. The following two
rules will be used to compute the unhappiness for a city.
1) If the plant is within two miles or less of a city, the
unhappiness is infinite (that is, assign a very large number
to the unhappiness for that city).
2) Otherwise, the unhappiness is equal to the population of the
city divided by the distance of the plant from the city.
The average unhappiness equals:
Avg. Unhappiness = Sum of the unhappiness of 4 cities /
The total state's population.
Your program should select the site at which the average unhappiness is
smallest.
INPUT:
^^^^^^
The user should be prompted to enter the x and y coordinates and the
population for each of the four cities from the keyboard. Each coordinate
should be checked to ensure it is between 1 and 25. If not, the user should
be prompted to enter a value which is in the correct range. The population
should be entered in thousands of people. For example, if the population
is 10,000, the user should input 10.
OUTPUT:
^^^^^^^
The program should print a message indication the coordinates where
the plant should be located. Following that message, the user should be
prompted to enter a 1 to view a map of the scenario or a 0 to exit the
program.
An Example Scenario:
Enter the x and y for City 1 : 1 1
Enter the Population for City 1: 10
Enter the x and y for City 2 : 1 25
Enter the Population for City 2: 10
Enter the x and y for City 3 : 25 1
Enter the Population for City 3: 10
Enter the x and y for City 4 : 25 25
Enter the Population for City 4: 10
**********
Locate the Plant At: 13 13
Enter 1 to view a Map of the scenario, or 0 to exit: 1
MAP OF SCENARIO
---------------
C2<><><><><><><><><><><><><><><><><><><><><><><>C4
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><>PP<><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><>
C1<><><><><><><><><><><><><><><><><><><><><><><>C3
Note: The previous scenario is just an example, you need to create your own
test cases to make sure that your program produces the correct results.
The code is well explained in the comments. The screenshots of the codes are here for better understanding.
The output of the above code is:
I am attaching the code with comments for understanding So that manipulation of code could e possible for further requirements.
#include<bits/stdc++.h>
using namespace std;
int main() {
int arr[25][25] ={0}; //declaring an array where
i,j is the location of the city
int n;
cout<<"Enter the number of
cities"<<endl;
cin>>n; // where n is number of cities
for(int i=0; i<n; i++)
{
int x,y,z;
cout<<"Enter the x and y for
City "<<i+1<<": "<<endl;
cin>>x>>y;
cout<<"Enter the
Population for City "<<i+1<<": "<<endl;
cin>>z;
if((x < 1) || (x > 25) || (y < 1) || (y > 25)) // if
the city is not in the range then input again
{
cout<<"Enter a value which is
in the correct range"<<endl;
i--;
}
else arr[x-1][y-1]=z; // storing
the population at the location
}
int x1,y1;
cout<<"Locate the Plant At: "<<endl;
cin>>x1>>y1;
while((x1 < 1) || (x1 > 25) || (y1 < 1) || (y1 > 25))
// if the plant is not in the range then input again
{
cout<<"Enter a value which is
in the correct range"<<endl;
cout<<"Locate the Plant At:
"<<endl;
cin>>x1>>y1;
}
double un = 0; // declaring the variable which will tell about
total unhapppiness in te city
int flag = 0; // for checking if the city is within 2 miles
int sum = 0; // sum is storingt the total population of the
state
for(int i=0;i<25;i++)
{
for(int j=0;j<25;j++)
{
if(arr[i][j] > 0)
{
int v =
(i+1-x1)*(i+1-x1);
int b =
(j+1-y1)*(j+1-y1);
cout<<v<<" "<<b<<endl;
double dis =
sqrt(v+b); // finding the distance
if(dis > 2)
un = un + (arr[i][j]/dis);
else flag =
1;
cout<<un<<endl;
sum = sum +
arr[i][j]; // sum is storingt the total population of the
state
}
}
}
if(flag == 1) un = 1000000.00/sum;
else un = un / sum;
cout<<"Avg. Unhappiness: "<<un<<endl;
// printing the map
cout<<"Enter 1 to view a Map of the scenario, or 0 to exit:
"<<endl;
int m;
cin>>m;
if(m == 1)
{
for(int i=0;i<25;i++)
{
for(int
j=0;j<25;j++)
{
if(arr[i][j] > 0)
{
cout<<"City";
}
else if(i == x1-1 && j== y1-1)
cout<<"PP";
else cout<<"<>";
}
cout<<endl;
}
}
return 0;
}