1. Write a program that calculates and prints the average of several integers. A
ID: 3544756 • Letter: 1
Question
1. Write a program that calculates and prints the average of several integers. Assume the last value read with scanf is the sentinel 9999. A typical input sequence might be
10 8 11 7 9 9999
Indicating that the average of all the values preceding 9999 is to be calculated.
2. Write a function integerPower ( base, exponent) that returns the value of
base^(exponent)
For example, integerPower( 3,4) = 3*3*3*3. Assume that exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use
Explanation / Answer
#include <stdio.h>
int main() {
int scan,count=0;
double avg=0,sum=0;
scanf("%d",&scan);
while(scan!=9999)
{
count++;
sum=sum+scan;
scanf("%d",&scan);
}
if(count!=0)
avg=sum/count;
printf("The average is %lf ",avg);
return 0;
}
2)
int integerPower(int base,int exponent)
{
int power=1;
for(int i=1;i<=exponent;i++)
{
power=power*base;
}
return power;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.