Write a program that: Define a structure \"city\" to house a cities name (name n
ID: 3819140 • Letter: W
Question
Write a program that: Define a structure "city" to house a cities name (name not to exceed 20 char), the city's population as integer and surface area in square miles as a double. Create an array big enough to hold information about 3 cities and initialize the information as below. Write a function called density that returns no value but accept an array of my cities and prints for every city the city name and the population density as a double presenting person/square mile in the format. Have main print the information as below Shangri-La 250000 78000.00 Summit 250000 8000.00 Luxor 134000 4000.00 Shangri-La 3.21 person per square miles Summit 31.25 person per square miles Luxor 33.50 person per square milesExplanation / Answer
#include<stdio.h>
struct city
{
char name[10]; //for city name
int population; //for city population
double sqmiles; //for city area
}mycity[3]; // for three cities
void density()
{
int i;
double dence;
for(i=0;i<3;i++)
{
dence=mycity[i].population/mycity[i].sqmiles; //calculating density of city
printf("%s %lf person per square mile ",mycity[i].name,dence); //printing city name and density
}
}
int main()
{
int i;
for(i=0;i<3;i++) //reading three cities data
{
printf("please enter a name of city%d: ",i+1);
scanf("%s",&mycity[i].name);
printf("please enter a population of city%d: ",i+1);
scanf("%d",&mycity[i].population);
printf("please enter a area of city%d: ",i+1);
scanf("%lf",&mycity[i].sqmiles);
}
printf(" ");
for(i=0;i<3;i++)
{
printf("%s %d %lf ",mycity[i].name,mycity[i].population,mycity[i].sqmiles); //printing three cities data
}
printf(" ");
density(); //call for density function
}
Output:-
please enter a name of city1: Shangri_la
please enter a population of city1: 250000
please enter a area of city1: 78000
please enter a name of city2: Summit
please enter a population of city2: 250000
please enter a area of city2: 8000
please enter a name of city3: Luxor
please enter a population of city3: 134000
please enter a area of city3: 4000
Shangri_la 250000 78000.000000
Summit 250000 8000.000000
Luxor 134000 4000.000000
Shangri_la 3.205128 person per square mile
Summit 31.250000 person per square mile
Luxor 33.500000 person per square mile
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.