In: Computer Science
********************************************C PROGRAMMING*************************************************************************************
char lname[][10]={"Johnson","Williams","Ling","Albin","Anderson","Baca","Birner","Dominguez","Aimino", "Armstrong","Beard","Calderon","Carter","Chaname","Chaney"};
char fname[][10] ={"Fred","Betty","Hector","Ross","Jason","Elisa","Dalton","Javier","Ann","Addison","Cindy","Yamil","Thomas","Bryan","Kris"};
char middle[] = {'N','L','X','L','O','L','M','B','S','T','J','C','P','D','Z'};
char city[][10] = {"Lakeland","Orlando","Tampa","Lakeland","Tampa","Lakeland","Orlando","Orlando", "Lakeland","Lakeland","Orlando","Tampa","Tampa","Lakeland","Orlando"};
Create a report that lists all of the cities that patients live
in and provide a count for
each city of how many patients come from that city. Print this list
out in alphabetical
order based on city name.
Please comment me if anything is required.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
//all the data from the problem stored in the character
arrays
char
lname[][10]={"Johnson","Williams","Ling","Albin","Anderson","Baca","Birner","Dominguez","Aimino",
"Armstrong","Beard","Calderon","Carter","Chaname","Chaney"};
char fname[][10] ={"Fred","Betty","Hector","Ross","Jason","Elisa","Dalton","Javier","Ann","Addison","Cindy","Yamil","Thomas","Bryan","Kris"};
char middle[] = {'N','L','X','L','O','L','M','B','S','T','J','C','P','D','Z'};
char city[][10] =
{"Lakeland","Orlando","Tampa","Lakeland","Tampa","Lakeland","Orlando","Orlando",
"Lakeland","Lakeland","Orlando","Tampa","Tampa","Lakeland","Orlando"};
printf("Patients full name last name middle name and their city
details below\n*************************************\n");
//loop to print the patients first name, last name, middle name
and their city
for(int i=0;i<10;i++)
{
printf("The patient %s %s %c is lives in
%s\n",lname[i],fname[i],middle[i],city[i]);
}
//sorting the city array in alphabetical order using bubble
sort
char temp[100];
for (int j=0; j<10-1; j++)
{
for (int i=j+1; i<10; i++)
{
if (strcmp(city[j], city[i]) > 0) //comparing the cities
according to the alphabetical order
{
strcpy(temp, city[j]);
strcpy(city[j], city[i]);
strcpy(city[i], temp);
}
}
}
printf("\nBelow is the count for each city from each city how many
patients come from in ascending
order\n************************************\n");
int count = 0;
//counting the count of each city
for(int i=0;i<10;i++)
{
count = 1;
while(strcmp(city[i],city[i+1])==0)
{
count++;
i++;
}
//printing the frequency of each city
printf("The number of patients lives in city %s is %d
\n",city[i],count) ;
}
}
Output :