Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need some help for C programming Relevant Programming Concepts: Structures Defin

ID: 3807476 • Letter: N

Question

Need some help for C programming

Relevant Programming Concepts: Structures Define the following data type that describes a city typedef struct char name[20]; char state[21; double population; int rank; double percent; city t, Develop a C program that reads a file called cities.txt into a 10-element array of type city t. It then prints those cities with population growth larger than 10% on screen. Your program should use the following functions: void scan city city t *x, FILE in); void print city city t x), File cities txt 1 New York NY 8143197 9.4 2 Los Angeles CA 3844829 6.0 3 Chicago IL 2842518 4.0 4 Houston TX 2016582 19.8 5 Philadelphia PA 1463281 -4.3 6 Phoenix AZ 1461575 34.3 7 San Antonio TX 1256509 22.3 8 San Diego CA 1255540 10.2 9 Dallas TX 121382 18.0 10 San Jose CA 912332 14.4

Explanation / Answer

Cities.txt File contents

1 New_York NY 8143197 9.4
2 Los_Angeles CA 3844829 6.0
3 Chhicago IL 2842518 4.0
4 Houston TX 2016582 19.8
5 Philadelphia PA 1463281 -4.3
6 Phoenix AZ 1461575 34.3
7 San_Antonio TX 1256509 22.3
8 San_Diego CA 1255540 10.2
9 Dallas TX 121382 18.0
10 San_Jose CA 912332 14.4

Program

#include<stdio.h>

#include<stdlib.h>
//Structure definition
typedef struct
{
char name[20];
char state[2];
double population;
int rank;
double percent;
}city_t;//End of structure

//To read file Cities.txt
void scan_city(city_t *x, FILE *in)
{
int len = 0;
//Checks if file pointer contains null then show error and exit
if (in == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}//End of if
//Loops till end of file
while(!feof(in))
{
//Reads data from file and stores in structure objects
fscanf(in, "%d %s %s %lf %lf", &x[len].rank, x[len].name, x[len].state, &x[len].population, &x[len].percent);
//Increase the counter
len++;
}//End of while

// Close input file
close(in);

}//End of function
//Print the city having percentage population is greater than 10
void print_city(city_t *x)
{
int c;
//Loops till 10 number of records
for(c = 0; c < 10; c++)
{
//Checks city percentage is greater than 10
if(x[c].percent > 10)
{
//Displays city information
printf(" Position: %d", x[c].rank);
printf(" Name: %s", x[c].name);
printf(" State: %s", x[c].state);
printf(" Population: %.2lf", x[c].population);
printf(" Percentage: %.2lf", x[c].percent);
}//End of if condition
}//End of for loop
}//End of function
//Swap operation
void swap(city_t *x, city_t *y)
{
city_t t;
t = *x;
*x = *y;
*y = t;
}//End of function
//Sorts the city based on population Descending order
void selection(city_t x[])
{
int r,c;
int min;

//Loops till 10
for(r = 0; r < 10; r++)
{
//Start search from currently unsorted
min = r;
for(c = r; c < 10; c++)
{
//If found a smaller element
if(x[c].population > x[min].population)
//Move it to the front
min = c;
}//End of inner loop
//Calls the swap function
swap(&x[r], &x[min]);
}//End of outer loop
//Print the sorted list
for(c = 0; c < 10; c++)
{
printf(" Position: %d", x[c].rank);
printf(" Name: %s", x[c].name);
printf(" State: %s", x[c].state);
printf(" Population: %.2lf", x[c].population);
printf(" Percentage: %.2lf", x[c].percent);
}//End of for loop
}//End of function
//Main function
int main()
{
city_t ct[10];
//Opens Cities.txt in read mode
FILE *fp = fopen("Cities.txt", "r");
scan_city(ct, fp);
print_city(ct);
selection(ct);
}//End of main function

Output:

Position: 4
Name: Houston
State: TX
Population: 2016582.00
Percentage: 19.80
Position: 6
Name: Phoenix
State: AZ
Population: 1461575.00
Percentage: 34.30
Position: 7
Name: San_Antonio
State: TX
Population: 1256509.00
Percentage: 22.30
Position: 8
Name: San_Diego
State: CA
Population: 1255540.00
Percentage: 10.20
Position: 9
Name: Dallas
State: TX
Population: 121382.00
Percentage: 18.00
Position: 10
Name: San_Jose
State: CA
Population: 912332.00
Percentage: 14.40
Position: 1
Name: New_York
State: NY
Population: 8143197.00
Percentage: 9.40
Position: 2
Name: Los_Angeles
State: CA
Population: 3844829.00
Percentage: 6.00
Position: 3
Name: Chhicago
State: IL
Population: 2842518.00
Percentage: 4.00
Position: 4
Name: Houston
State: TX
Population: 2016582.00
Percentage: 19.80
Position: 5
Name: Philadelphia
State: PA
Population: 1463281.00
Percentage: -4.30
Position: 6
Name: Phoenix
State: AZ
Population: 1461575.00
Percentage: 34.30
Position: 7
Name: San_Antonio
State: TX
Population: 1256509.00
Percentage: 22.30
Position: 8
Name: San_Diego
State: CA
Population: 1255540.00
Percentage: 10.20
Position: 10
Name: San_Jose
State: CA
Population: 912332.00
Percentage: 14.40
Position: 9
Name: Dallas
State: TX
Population: 121382.00
Percentage: 18.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote