In: Computer Science
*** C language ***
Make a list of all of the different zip codes
that the zombies live at and a count of how many
zombies live at that zip code. The list should be sorted based on
the zip code with the
smallest zip code printed first. Example:
Zip Code # Zombies
31234 2
//variables
int zip[] = {37643,31234,32785,32643,32785,32643,31234,31234,32643,32643,31234,32785,32785,32643,31234};
char zombie[]={'N','Y','Y','N','Y','Y','Y','N','Y','Y','N','Y','Y','Y','Y'};
Y = zombie N = Not zombie
int zPatient = 0;
int nonZpatient = 0;
for(int pt = 0; pt < (*(&zombie + 1) - zombie); pt++)
{
if(zombie[pt] == 'N')
{
nonZpatient++;
}
else if (zombie[pt] == 'Y')
{
zPatient++;
}
}
Below is the code for the above problem.;
#include <stdio.h>
//main method to initialize the program
int main()
{
//array of zipcode
int zip[] =
{37643,31234,32785,32643,32785,32643,31234,31234,32643,32643,31234,32785,32785,32643,31234};
//array of zombies status
char
zombie[]={'N','Y','Y','N','Y','Y','Y','N','Y','Y','N','Y','Y','Y','Y'};
//to count no.of zombies
int zPatient = 0;
int nonXpatient = 0;
int zipcode;
printf("Enter the zipcode you want to search for zombies\n");
scanf("%d",&zipcode);
//traverse the zipcodes
for(int i = 0;i<(sizeof(zip)/sizeof(int));i++)
{
if(zip[i]==zipcode && zombie[i]=='Y') //if there is
zombie
{
zPatient++;
}
if(zip[i]==zipcode && zombie[i]=='N') //if there is no
zombie
{
zPatient++;
}
}
printf("zipcode : %d\n",zipcode);
printf("zombies : %d\n",zPatient);
return 0;
}
OUTPUT: