Question

How to write a recursive function in C++ that gives the sum of up to 10...

How to write a recursive function in C++ that gives the sum of up to 10 integers from an array. If there are more than 10 integers entered by user, it takes only the first 10. If less than 10, it continues with the calculation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int getSum(int arr[], int size, int index){
    if(index<size && index<10){
        return arr[index]+getSum(arr,size,index+1);
    }
    else{
        return 0;
    }
}

int main() {
    int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12};
    cout<<getSum(arr,5,0)<<endl;
    cout<<getSum(arr,12,0)<<endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
How to write a recursive function in C++ that gives the sum of up to 10...
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
  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Write a recursive function sum-odds that takes a non-empty list of integers

    in python Part I: Sum of Odd Integers Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up...

    IN MATLAB RECURSIVE FUNCTION 1. Sum of Factorials. The Recursive Function: A series that sums up the factorial of the natural numbers from 1 to N can be expressed as The recursive algorithm: N-1 N-2 N-3 Write independent matlab code to solve the above problem with the following methods: 1. 2. 3. A monolithic program (with no functions) A standard (non-recursive) user defined function (an a program to call it) A recursive function (an a program to call it) Test...

  • 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...

  • 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...

  • You must use C++ to answer this problem. "Write a recursive function squaresSum that takes a...

    You must use C++ to answer this problem. "Write a recursive function squaresSum that takes a positive integer parameter, num, and returns the sum of squares of all integers from 1-num."

  • Write a recursive function that accumulates the sum of values in an n element array and...

    Write a recursive function that accumulates the sum of values in an n element array and prints out the contents of the array and the final sum and average. In c language

  • 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; }

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