Question

write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...

write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.

Homework Answers

Answer #1

C++ Recursive Solution:

#include <iostream>
using namespace std;

//Recursive function 

int sumAlternate(int n, int i){
    
    //if we reach to zero simply return 2 as we have to reached to zero only if n
    // is at 2. and on substracting 2 it will become zero.
    if(n == 0) return 2;
    
    // if n is less than 0 return zero as we donot want to add this value
    if(n< 0) return 0;
    
    //or simply do recursion sum with substracting n by n-2;
     return n + sumAlternate(n-2*i, i+1);
     
}

//Main Method
int main() {
    
    int n;
    cout<<"Enter Number:";
    //take input value n
    cin>>n;
    
    //call recursive sumAlternate method to generate sum_alternate.
    //here 'i' have taken 1 as initial value so as to make formula
    //and 'i' will increase by 1 continously
    // x= x + x-(2*1) + x-(2*2) + x-(2*3) + ....... 
   cout<<"Alternate Sum is: "<<sumAlternate(n, 1);
        return 0;
}

Output:1.

output 2.

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
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters,...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters, each greater or equal to zero, and evaluates to their sum. In this problem, you must use the built-in functions "add1" and "sub1" and may not use the built-in functions "+" or "-". For example, (sum 2 3) should evaluate to 5. Note: (add1 5) evaluates to 6 and (sub1 4) evaluates to 3. Hint: like you saw in the "append" lecture, treat one...
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list...
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because these are the only strings shorter than 3. Your solution must not be recursive. You will...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x)...
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
For C++: a) Write a function is_prime that takes a positive integer X and returns 1...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1 if X is a prime number, or 1 if X is not a prime number. b) write a program that takes a positive integer N and prints all prime numbers from 2 to N by calling your function is_prime from part a.
Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list,...
Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list, say L, and two positive integers, M and N. The function should return another list L’, which represents the sub-list of L containing all the elements from index M to index N in reverse order. For example, if the input list is [a 2 g 5 4 k] and M is 2 and N is 5, then the output list should be [4 5...
Python: Write a recursive function that takes in a number as a parameter and calculates the...
Python: Write a recursive function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter.
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As...
Write a function that takes as its parameter two integer variables and an output stream varlable....
Write a function that takes as its parameter two integer variables and an output stream varlable. The two integers correspond to the numerator, and denominatorThe function should check if the denominator is zero, then it should print out the message "Otherwise , it should calculate and print the quotient and the remainder to a file. c++
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT