I was given an assignment asking me to create a program that reads the number of apartment buildings and the number of people living in each apartment. Then we have to calculate the minimum, maximum and average number of residents the number of unoccupied buildings/dwellings. The last point of the assignment causes me a problem, as I can't figure out how to implement it. The most I could do is to make the program write which apartment is unoccupied, which is still insufficient. I will need a little help.
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Write an algorithm that reads the number of bytes in the apartment building and the numbers living in each apartment.
//Calculate and write down the average, maximum and minimum number of inhabitants, the number of unoccupied dwellings.
int n,i;
printf("Enter the number of apartments in the apartment building:\n");
scanf("%d",&n);
int p[n],s=0;
for(i=0;i<n;i )
{
printf("Enter the number of residents in %d. apartment:\n",i 1);
scanf("%d",&p[i]);
s =p[i];
}
int min=p[0];
for(i=1;i<n;i )
if(p[i]<min)
{
min=p[i];
}
printf("Minimum population: %d\n",min);
int max=p[0];
for(i=1;i<n;i )
if(p[i]>max)
{
max=p[i];
}
for(i=0;i<n;i )
if(p[i]==0)
{
printf("%d. apartment is unoccupied.\n",i 1);
}
printf("Maximum population: %d\n",max);
printf("Average population: %.2f\n",(float)s/n);
printf("Number of unoccupied apartment: %d\n");
return 0;
}
CodePudding user response:
This is straightforward enough. You just need to be able to increment a counter, whose initial value is zero, and report its value at the end.
int empty = 0;
for(i=0;i<n;i )
if(p[i]==0)
{
empty ;
}
printf("Maximum population: %d\n",max);
printf("Average population: %.2f\n",(float)s/n);
printf("Number of unoccupied apartment: %d\n", empty);