Make a program that will produce basic statistics from an imput based on the hap
ID: 3567975 • Letter: M
Question
Make a program that will produce basic statistics from an imput based on the happiness of citizens for the Census Bureau. The Census will be conducted on how happy people are, with people at the top having a happiness of 90-99, and a low happiness of 0-9. The census requires a histographic output stating the number of people in each happiness level, and the report must include another output stating the percentage of people that have at least that level of happiness(e.g. "0-9-->100%, 31-40--->67%").
Sample Input:( 45,65,89,21,97,42)
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main()
{
int arr[] = { 45,65,89,21,97,42 }; //array provided
int n = 6; //size of the array
int i;
int hist[10] = {0}; //histogram data at each index
float happy[10] = {0.0}; //happiness percentage at each level
for(i=0; i<n; i++) //this loop produces the histogram data
{
if(arr[i] >= 0 && arr[i] <=9)
hist[0]++;
else if(arr[i] >= 10 && arr[i] <=19)
hist[1]++;
else if(arr[i] >= 20 && arr[i] <=29)
hist[2]++;
else if(arr[i] >= 30 && arr[i] <=39)
hist[3]++;
else if(arr[i] >= 40 && arr[i] <=49)
hist[4]++;
else if(arr[i] >= 50 && arr[i] <=59)
hist[5]++;
else if(arr[i] >= 60 && arr[i] <=69)
hist[6]++;
else if(arr[i] >= 70 && arr[i] <=79)
hist[7]++;
else if(arr[i] >= 80 && arr[i] <=89)
hist[8]++;
else
hist[9]++;
}
int sum=0, per;
for( i=9; i>=0; i--) //calculates the happiness percentage
{
sum += hist[i];
happy[i] = (((float)sum)/((float)n))*100.0;
}
for( i=0; i<10; i++) //displays the data
{
printf("%d - %d : %d people with %.2f % ", (i*10), (i*10) + 9, hist[i], happy[i]);
}
return 0;
}
-------------------
output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.