Please write in C programming Define the following data type that contains US St
ID: 3739748 • Letter: P
Question
Please write in C programming
Define the following data type that contains US State information. typedef struct char State-Name [20] double Total-Area i double Water Area; double Land.Area; int Population; int Population-per-Land.Area Statei Develop a C program that reads a file called State Data.txt into a 10-element array of type State. The file State Data.txt contains the name of the state, the population in the state, the total area of the state in square miles, and the area that is covered in water (also in square miles). You will need to calculate population per land area (rounded to the nearest person per square mile). Note that the land area is the total area minus the area covered in water. The program then prints the states sorted by population per square mile. Your program should use the following functions void scan-states (Statex,FILE in) void print-states (Statex) File State Data.txt: 10450316 59425.15 1911.66 28449186 268596.46 7364.75 7026629 113990.30 396.22 21002678 65757.70 12132.94 Georgia Texas Arizona Florida California39849872 163694.74 7915.52 Pennsylvania 12819975 46054.35 1311.64 Illinoiis Michigar Virginia Washington7384721 71297.95 4842.43 12815607 57913.55 2394.62 9935116 96713.51 40174.61 8492783 42774.93 3284.84Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Defines a structure stateInfo
typedef struct stateInfo
{
// Data member declaration
// To store state name
char stateName[20];
// To store total area
float totalArea ;
// To store water ares
float waterArea ;
// To store land area
float landArea ;
// To store population
int Population ;
// To store population per land area
int populationPerLandArea ;
}State; // End of structure
// Function to read the file and store data in structure data member and return number of records
int readFile(State ss[])
{
//File pointer for StateData.txt file declared and open it in read mode
FILE *fRead = fopen("StateData.txt", "r");
// Initializes the record counter to zero
int counter = 0;
//Checks if file is unable to open then display error message
if (fRead== NULL)
{
puts("Could not open files");
exit(0);
}//End of if
//Extracts a character from StateData.txt file and stores it in structure data member
// and Loops till end of file
while (fscanf(fRead, "%s", ss[counter].stateName) != EOF)
{
// Reads data from file and stores it in structure data member
fscanf(fRead, "%d", &ss[counter].Population);
fscanf(fRead, "%e", &ss[counter].totalArea);
fscanf(fRead, "%e", &ss[counter].waterArea);
// Calculates the land area
ss[counter].landArea = ss[counter].totalArea - ss[counter].waterArea;
// Calculates the population per land area
ss[counter].populationPerLandArea = ss[counter].Population / ss[counter].landArea;
// Increase the counter by one
counter++;
}// End of while
// Close the file
fclose(fRead);
// Returns the record counter
return counter;
}// End of function
// Displays the state records
void display(State ss[], int len)
{
int x;
// Loops till end of the record
for(x = 0; x < len; x++)
{
// Display state information
printf(" State: %-10s Population: %d Total Area: %.2f Water Area: %.2f", ss[x].stateName, ss[x].Population, ss[x].totalArea, ss[x].waterArea);
printf(" Land Area: %.2f Population per land area: %d", ss[x].landArea, ss[x].populationPerLandArea);
}// End of for loop
}// End of function
// Function to sort the state record based on population per square mile using selection sort
void sortState(State ss[], int len)
{
int i, j;
// To store the minimum index position
int minIndex;
// Loops till end of state record minus one time
for (i = 0; i < len - 1; i++)
{
// Find the minimum element in unsorted array
minIndex = i;
// Loops till end of record
for (j = i + 1; j < len; j++)
// Checks if population per land area at j index position is less than the population per land area at minimum index position
if (ss[j].populationPerLandArea < ss[minIndex].populationPerLandArea)
// Update the minimum index position by j
minIndex = j;
// Swap the found minimum element with the first element
State temp = ss[minIndex];
ss[minIndex] = ss[i];
ss[i] = temp;
}// End of for lop
}// End of function
// Function to display people per square mile
void displayPeople(State ss[], int len)
{
int j;
// Loops till end of record
for (j = 0; j < len; j++)
printf(" %s has %d people per square mile of dry land", ss[j].stateName, ss[j].populationPerLandArea);
}// End of function
// main function definition
int main()
{
// Creates an array of states
State ss[50];
// To store number of records
int len;
// Calls the function to read the file and store the number of records returned
len = readFile(ss);
// Calls the function to display state information
printf(" Before sorting: ");
display(ss, len);
// Calls the function to sort the state information
sortState(ss, len);
printf(" After sorting: ");
// Calls the function to display state information
displayPeople(ss, len);
}// End of main function
Sample Run:
Before sorting:
State: Georgia Population: 10450316 Total Area: 59425.15 Water Area: 1911.66
Land Area: 57513.49 Population per land area: 181
State: Texas Population: 28449186 Total Area: 268596.47 Water Area: 7364.75
Land Area: 261231.72 Population per land area: 108
State: Arizona Population: 7026629 Total Area: 113990.30 Water Area: 396.22
Land Area: 113594.08 Population per land area: 61
State: Florida Population: 21002678 Total Area: 65757.70 Water Area: 12132.94
Land Area: 53624.76 Population per land area: 391
State: California Population: 39849872 Total Area: 163694.73 Water Area: 7915.52
Land Area: 155779.22 Population per land area: 255
State: Pennsylvania Population: 12819975 Total Area: 46054.35 Water Area: 1311.64
Land Area: 44742.71 Population per land area: 286
State: Illinois Population: 12815607 Total Area: 57913.55 Water Area: 2394.62
Land Area: 55518.93 Population per land area: 230
State: Michigan Population: 9935116 Total Area: 96713.51 Water Area: 40174.61
Land Area: 56538.90 Population per land area: 175
State: Virginia Population: 8492783 Total Area: 42774.93 Water Area: 3284.84
Land Area: 39490.09 Population per land area: 215
State: Washington Population: 7384721 Total Area: 71297.95 Water Area: 4842.43
Land Area: 66455.52 Population per land area: 111
After sorting:
Arizona has 61 people per square mile of dry land
Texas has 108 people per square mile of dry land
Washington has 111 people per square mile of dry land
Michigan has 175 people per square mile of dry land
Georgia has 181 people per square mile of dry land
Virginia has 215 people per square mile of dry land
Illinois has 230 people per square mile of dry land
California has 255 people per square mile of dry land
Pennsylvania has 286 people per square mile of dry land
Florida has 391 people per square mile of dry land