* population_report * Purpose: * calculates g,tatg (total, mAx, mig and average)
ID: 3562550 • Letter: #
Question
* population_report * Purpose: * calculates g,tatg (total, mAx, mig and average) of * carbon footprint data of individuals in a population, * Parameters: * gIg - a pointer to an array of integers which contains the carbon footprint result for each individual in the population * pop_sz - the size of the array pointed to by ag * g=g - an array to store the results g,t=[SUM] is the total ? g=a[MAX] is the maximum ? gLAtg(MIN] is the minimum ? g=a(AVG] is the average * Preconditions: * pop_sz >= 1 * ag references at least pop_sz valid integers * Postconditions: The array pointed to by g=g, will be updated and will ? contain the carbon footprint g=g (total, mAx, mig and average) ? for the population ?g=,KgAgritpja by ag * Returns: none */ void population_report (int *cfp, int pop_sz , int stats[NUM_STATS]);Explanation / Answer
void population_report(int *cfp, int pop_sz,int stats[NUM_STATS])
{
stats[0] = 0;
stats[1] = cfp[0];
stats[2] = cfp[0];
for(int i=0; i<pop_sz; i++) {
if(cfp[i] > stats[1]) stats[1] = cfp[i];
if(cfp[i] < stats[2]) stats[2] = cfp[i];
stats[0] = stats[0] + cfp[i];
}
stats[3] = stats[0]/pop_sz;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.