Question
In C... This assignment will test your ability to write functions, pass parameters appropriately, and use multiple files including a header file. In order to
In C...
This assignment will test your ability to write functions, pass parameters appropriately, and use multiple files including a header file. In order to get full credit, you must demonstrate that you know when to pass parameters by copy versus as addresses. Do not pass parameters as addresses unless needed. You will write this assignment in (at least) 4 files with functions split between the various files. One file will be a header file which will contain all of your programs #include and #define statements and function prototypes. You will include your header file as the only #include statement in your other files. NOTE: the only arrays you should use in this program are your strings (storing city names and if desired, the name of the input file).
This program will compute city ratings based on various statistics. The input will come from adisk file. You will input one citys information at one time using fscanf. After input, you will call various functions to perform computations on that city and assign the city a rating. An output function will output some of the computed values including the rating for the city. Keep track of the number of cities evaluated and the sum of their ratings along with the highest rated city (both its rating and city name). After evaluating all of the cities in the file, the program will end byoutputting the number of cities evaluated, the average rating, and the most highly rated citys nameand rating.
Your main function will open an input file and loop on each city until the end of the file, calling the six functions described below:
-
input a citys name, population, square mileage, pollution, crime, expense, number ofhighways (all input values need to be returned to main so you would have to pass these parameters appropriately)
-
compute and return the citys population density (this function only returns one value so it can be returned through a return statement)
-
compute the citys pollution rating, traffic rating, crime per capita, expense per capita (this function returns these four rating values)
-
compute the citys livability (this function returns a single value)
-
update statistics of the total of all of the city livability values, the number of cities, and
whether this is the best city found so far (this function receives several values and updates
some of them: number of cities, total livability, most livable city and its value)
-
output a summary of this city (name, livability, population density)
After the loop terminates, main will then call a seventh function to output a summary of the numberof cities input, the average livability, and the citys name and rating found to be the most livable.Also close the input file. All output should go to the console window. For #3, this function may call other functions, but you MUST call a single function from main to handle the computations (that is, do not call separate functions of computing pollution rating, traffic rating, etc from main).
NOTE: in order to write an input function, you will need to pass your FILE variable to that function. The variable is an * so you would pass it as input(fp, ...); instead of passing it asinput(&fp, ...); and the formal parameter in your input function would be defined as FILE *fp and not FILE **fp. Since data like population and crime will be int values, you will have to pass them to your input function as addresses (&). Since they will be received as addresses in the input function, when you pass them on to fscanf you would not include the & (confusing,
huh?). The city name is a string so you do not use the & for it. Formulas to compute the various values are:
-
Population density = population / square mileage.
-
Pollution = pollution amount * population density / 1000
-
Traffic rating = population density * 1.7 / major highways
-
Crime per capita = crime * population density / 872.6
-
Expense per capita = expense * population density / 1217.1
-
Livability = 100 (Pollution + Traffic rating + Crime per capita + Expense per capita) /
13.81 Use #define in your header file to declare constants for the values above (1.7, 872.6, 1217.1, 13.81)
Organize your program in separate files as follows:
-
the main function in a file by itself
-
all input/output functions in a file
-
all computation functions in a file (including the update function)
-
a header file that includes all function prototypes, #include and #define statements (you
can place other items in your header file if you have other definitions)
If you are confused about how to write your code, see the example programs on the sample code website, particularly the payroll and craps examples.
NOTE: in Visual Studio after creating a project, in the right pane under Solution, expand this and you will find entries for Header Files and Source Files (also Resource Files). Right click on Header Files and select Add and then either New Item to create your header file or Existing Item to load an already written header file into the project. Similarly, do the same under Source Files for the 3 files that will contain code (the file with main, the I/O file and the computation file). Remember that your header file will contain only compiler directives (#include, #define), declarations if any (in this case, you will probably not have any) and function prototypes plus any comments. There will be no C code in your header file.
Run your program on the 2 data files on the website (cities1.txt, cities2.txt). Your output for cities1.txt will look something like this:
cities1.txt:
Akron 245000 738 206 189 587 2 Cincinnati 2000000 7243 195 293 591 9 Cleveland 2250000 7517 214 331 662 12 Columbus 2125000 5718 181 471 612 7 Dayton 1100000 3950 191 241 587 5 Toledo 362000 1111 168 211 568 3
cities2.txt:
Atlanta 4600000 10381 357 392 610 18 Boston 4350000 8371 401 385 983 21 Cincinnati 2000000 7243 195 293 591 9 Dallas 3750000 13713 287 301 498 12 Las_Vegas 1350000 8741 387 205 540 6 Los_Angeles 11250000 19873 683 403 983 31 Minneapolis 3500000 10811 208 353 581 8 New_York_City 9850000 11871 299 397 1011 27 Seattle 3250000 8713 487 308 753 12 Washington_DC 4875000 9871 357 410 871 20
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started