Question

In: Computer Science

Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to...

Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to n so that the first number is added, the second number is subtracted, the third number added, the fourth subtracted, and so on: 1-2+3-4+5-6+7… until you reach n. If n is 0 or less then return 0.

Solutions

Expert Solution

code in c++

#include <iostream>

using namespace std;

// creating function
int alter_sum(int n)
{
    // if n is less than or equal to 0 return 0
    if(n<=0) 
    return 0;

    int sum=0;           //initializing int sum to 0

    // calculating alternate sum
    for (int i = 1; i <= n; i++)
    {
        if(i%2!=0)           //if i is odd then add it to sum        
        sum+=i;
        else                 //if i is even then subtract it from sum
        sum-=i;
    }
    return sum;    
}

int main()
{
    // testing our function
    cout<<alter_sum(5);

}

ss for reference:


Related Solutions

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
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns...
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns their minimum. # Execute the function with n=100, a=1, b=9. 2- Replicate the function call f(100,1,9) 100 thousand times # and plot the empirical density of the minimum of 100 indep. Unif(1,9)'s 3-Use the sampling distribution from (b) to find 95% confidence # interval for the minimum of 100 independent Unif(1,9)'s. Please solve in R
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a function sum_int( n ) to compute the sum of the integers from 1 up...
Write a function sum_int( n ) to compute the sum of the integers from 1 up to and including n.
A triangular number is the sum of the n natural numbers from 1 to n. For...
A triangular number is the sum of the n natural numbers from 1 to n. For example: The triangular number for 3 is 1 + 2 + 3 = 6 The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper...
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper factors/divisors ofn; for example,the sum of factorsof 20= 10+5+4+2 = 21.Note that 1 is nota properfactor of any number.Sample command => output:(display (sumOfFactors20))=> 21Hint: Calla recursivefunction sum_helperfrom sumOfFactorsfunction as follows:(define (sumOfFactors n)(sum_helper n (-n 1)))Here sum_helperfunction takes two parameters: n, d and returns the sum of all factors of n whichare <= d; for e.g. (sum_helper 20 6) returnsall proper factors of 20 which...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT