Question

How can I make this into a recursive function (C++)?? //Write a function that, given positive...

How can I make this into a recursive function (C++)??

//Write a function that, given positive integers 1 <= n <= 10^4 and 1 <= s <= n as input, returns the sum of the last s elements in the sequence from 1 to n (inclusive).

unsigned long int suffix_sum(unsigned int n, unsigned int s){
unsigned long int sum = 0,r=(n-s)+1;
while (r<=n){
sum = sum + r;
r++;
}
return sum;
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
unsigned long int suffix_sum(unsigned int n, unsigned int s){
    if(s == 0){
        return 0;
    }
    else{
        return n + suffix_sum(n-1, s-1);
    }
}

Add a comment
Know the answer?
Add Answer to:
How can I make this into a recursive function (C++)?? //Write a function that, given positive...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am stuck on trying to get this recursive function's logic to make it work. //Write...

    I am stuck on trying to get this recursive function's logic to make it work. //Write a recursive function that returns true if the sequence of 0 < n integers in A is sorted in non-increasing order and false otherwise. iostream and cmath libraries only bool is_sorted(const int *A, unsigned int n){ if (n == 0) return false; if (A > A+1) return true; else return false; }

  • I can not get this recursive binary search to wok for an edge case and it...

    I can not get this recursive binary search to wok for an edge case and it works but not for all, If you could look at it and try to find what is wrong to make this work for all edge cases. //Write a function that will return true if an element k is found in an array of integers A and false otherwise. The input to your //function is the array, its length 0 < n <= 10000, and...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • Python Programming Write a recursive function that takes positive int n as its input and returns...

    Python Programming Write a recursive function that takes positive int n as its input and returns sum of the first n squares, i.e 12 + 22 +...+n?

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

  • 1. The famous Fibonacci sequence f1, f2, f3, . . . is defined as f1 =...

    1. The famous Fibonacci sequence f1, f2, f3, . . . is defined as f1 = 1, f2 = 1 fn = fn−1 + fn−2, for n > 2 So the sequence begins as 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .. Define a recursive function int fibonacci(int n) which returns the n-th Fibonacci number 2. Define recursive function my_sequence(n) which returns the n-th member of the sequence a1 = 3, a2 = 5, a3 =...

  • d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes...

    d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes as parame- ters any three integers and returns the largest of the three integers. Your output should have the same format and should work for any integers a user enters Desired output: Enter three integers: 6 15 8 The largest integer is: 15 7.3 Recursive Functions Write a program that uses a function sum(int) that takes as an argument a positive integer n and...

  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT