Question

c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...

c++

Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive.

Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float).

The main function should be responsible for all inputs and outputs. Your functions will only calculate and return the values and NOT print anything. N is provided by the user; user input must be asked for repeatedly until a non-negative integer is provided.

Homework Answers

Answer #1
#include <iostream>

using namespace std;

int calculateSum(int N){
    int sum = 0;
    for(int i = -1;i<=N;i++){
        sum += i;
    }
    return sum;
}

float calculateAverage(int sum, int n){
    return 1.0*sum/n;
}

int main(){
    int N;
    
    cout<<"Enter a number: ";
    cin>>N;
    
    while(N<=0){
        cout<<"Enter a number: ";
        cin>>N;
    }
    
    cout<<"Sum: "<<calculateSum(N)<<endl;
    cout<<"Average: "<<calculateAverage(calculateSum(N),N+2)<<endl;
    
    return 0;
}

 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the user. Step 2: Modify the previous program so that it asks the user how many numbers they have, inputs that many numbers, and finally outputs the average of their numbers. The messages to the user should be clear throughout.
In C++ 1) Write the function definition that has 1 pass by value integer parameter. The...
In C++ 1) Write the function definition that has 1 pass by value integer parameter. The function contains logic to return true if the parameter is even and false if it is not. 2 ) Write a main function that prompts the user for a value, calls the function that you created in step one with the value entered by the user, display "even" if the function return true and otherwise displays "odd"
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
**C code only In this part, you will write a simple user-defined function that prints a...
**C code only In this part, you will write a simple user-defined function that prints a message. This function does not require any parameters or return value. The purpose is simply to get you started writing functions.Open repl project Lab: User-Defined Functions 1. Write a program that calls a function. This function should do the following: 1.Ask the user their name 2. Print a "hello" message that includes the user's name Example output: Please enter your name: ​Mat Hello, Mat!...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT