Write a program that calls a function calculateSum to calculate the sum of N int
ID: 3801220 • Letter: W
Question
Write a program that calls a function calculateSum to calculate the sum of N integers. The function calculateSum has one parameter n of type integer and returns an integer which represents the sum from 0 to N, inclusive. After calculating the sum of this N numbers write a function that calculate the average. This function will have three parameters: the sum, the number of items and the average. This function should calculate and return the average through one of its parameters (passing parameters by reference). The main function should be responsible for all print-outs. In other words, your functions will only calculate and return the values and NOT print anything.Explanation / Answer
// note: sum is from 0 to n so total term is n+1 and sum is n*(n+1)/2 and avg=sum/(n+1)
#include <iostream>
using namespace std;
int calculateSum(int n)
{
int sum=0;
sum=(n*(n+1))/2;
return sum;
}
int average(int s,int n,int avg)
{
avg=s/(n+1);
return avg;
}
int main() {
int sum=0,avg=0,n;
cout<<"enter the value of n ";
cin >> n;
sum=calculateSum(n);
cout<<"sum is="<<sum<<" ";
avg=average(sum,n,avg);
cout<<"avg is="<<avg;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.