158k views
3 votes
You are working for the census bureau and you need to write a program in C to do the following: a) count the number of people in each of the following age groups: 0 - 18 infant, 18 - 29 young, 29 - 50 middle aged, 50 - 69 old, 69 - and older, really old. Report the number of people in each age group. (use some random data for test). b) they need some statistics regarding the distribution of ages. Write a program to read in a number of ages. Compute the average age (mean) and the mode i.e. the age that occurs most frequently (be careful, some of the ages might be the same). Ages should be read in as integers, the average should be a real number. Calculate also the standard deviation using the formula:

User Senseiwu
by
4.3k points

1 Answer

4 votes

Answer:

See explaination

Step-by-step explanation:

#include<iostream>

#include<stdio.h>

#include<conio.h>

#include<math.h>

using namespace std;

void main()

{

int RandomData[]={10,5,20,45,20,66,25,31,20,70}; //Random Data

int infant=0,young=0,middle=0,old=0,older=0; //To store count

int n,freq,count=0,high=0; //to store mode and other

double stdDev,mean,sum=0; //to store stdDev mean and sum

for(int i=0;i<(sizeof(RandomData)/sizeof(RandomData[0]));i++) //loop to count

{

if(RandomData[i]>0&&RandomData[i]<=18)

infant++;

else if(RandomData[i]>18&&RandomData[i]<=29)

young++;

else if(RandomData[i]>29&&RandomData[i]<=50)

middle++;

else if(RandomData[i]>50&&RandomData[i]<=69)

old++;

else if(RandomData[i]>69)

older++;

}

double first=0,sec=0; //to store result of stdDev formula first and second part

for(int i=0;i<(sizeof(RandomData)/sizeof(RandomData[0]));i++) //loop to calculate mean mode and stdDev

{

n=RandomData[i];

count=0;

for(int j=0;j<(sizeof(RandomData)/sizeof(RandomData[0]));j++)

{

if(n==RandomData[j])

count++;

}

if(high<count)

{

freq=RandomData[i];

high=count;

}

first+=(pow((RandomData[i]),2)/(sizeof(RandomData)/sizeof(RandomData[0])));

sec+=RandomData[i]/(sizeof(RandomData)/sizeof(RandomData[0]));

sum+=RandomData[i];

}

mean=sum/(sizeof(RandomData)/sizeof(RandomData[0]));

stdDev=sqrt(first-pow((sec),2));

/*------Printing Result-----*/

cout<<"\\Count:\\Infant: "<<infant<<"\\Young: "<<young<<"\\Middle: "<<middle;

cout<<"\\Old: "<<old<<"\\Older: "<<older;

cout<<"\\\\Mean: "<<mean<<"\\Mode: "<<freq<<"\\Standard Deviation: "<<stdDev;

/*-----------XXXX-------------*/

getch();

}

User Cal
by
4.2k points