In: Computer Science
Design a program which a census the numbers and percentages of houses with particular numbers of occupants in a road. The output must appear in a table like the one below:
Output
Occupants 0 1 2 3 4 5 6 >6 No.
houses 2 3 7 9 6 4 2 2
Percentage 5.7% 8.6% 20.0% 25.7% 17.1% 11.4% 5.7% 5.7%
Houses with more than 6 occupants are considered to be overcrowded, and are to be output under a single column (>6). The data input part of the program must request the number of houses for each occupancy category. For example, the program should ask similar questions:
Provide the number of houses with 0 occupancy: 2
Provide the number of houses with 1 occupancy: 3
Provide the number of houses with 2 occupancy: 7
Provide the number of houses with 3 occupancy: 9
Provide the number of houses with 4 occupancy: 6
Your program should ask the user 8 times about houses with occupancy of 0 to 7 to count the numbers of houses with particular numbers of occupants. Each count should be stored in a separate variable. For each question, the user of the program should input numbers of houses with a particular occupancy value. After the program ask for the number of houses with occupants > 6, the program should output a table like the one above, and then exits. The table above is a result from inputting a value for the number of houses with different occupancy settings in a street with total of 35 houses. The last raw of the table reflects the percentage of each houses of different occupancy level given the total number of houses in the street. For example, the percentage of houses with 0 occupancy is 5.7%. This is because (2/35) * 100 is 5.7 %
Program written in c++ programming language
program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//initializing the variable
float percent[20],percentage,num;
int i,occup[20];
//using loop to enter number of houses with respectuve
occupancy
for(i=0;i<=7;i++){
printf("Provide the number of houses with %d occupancy:",i);
scanf("%d",&occup[i]);
num=occup[i];
percentage=(num/35) * 100; // calculating the percetage
percent[i]=percentage;
}
printf("Occupants 0 1 2 3 4 5 6 >6 No" );
printf("\nhouses ");
for(i=0;i<=7;i++){
printf("%d ",occup[i]); // printing number of houses
}
printf("\nPercentage ");
for(i=0;i<=7;i++){
printf("%0.1f%% ", percent[i]); // printing percetnage
}
}
output:
Provide the number of houses with 6 occupancy:2
Provide the number of houses with 7 occupancy:2
Occupants 0 1 2 3 4 5 6 >6 No
houses 2 3 7 9 6 4 2 2
Percentage 5.7% 8.6% 20.0% 25.7% 17.1% 11.4% 5.7% 5.7%
Refer this screen shoot of the program for better understanding
output: