Question

In: Computer Science

please write in c++ 2. Write a function sumOfArray that recursively finds the sum of a...

please write in c++

2. Write a function sumOfArray that recursively finds the sum of a one-dimensional array. A sample run is below.

The elements of the array are: 0 8 -4 6 7

The sum of the array is: 17

Press any key to continue . . .

Solutions

Expert Solution

Recursion:

The recursive function calls to itself and a terminate or base condition is specified which breaks the infinite execution of the recursive call. When the function call to itself then it works like a continue statement and the control goes to the starting of the function with updated argument value.

The stack data structure is used in recursion for storing the intermediate value when each function call is made.

In a recursive function, two cases are mentioned:

  1. Base condition
  2. Recursive call

Base condition:

A terminate condition or base condition is specified which breaks the infinite execution of the recursive call.

Recursive Call:

The function will call to itself and this is known as a recursive call.

The required program source code is given below:

#include <iostream>

using namespace std;

//function to find the sum of the array recursively
int sumOfArray(int arr[], int size)
{
//base condition
if(size<1)
return 0;
else
{
//recursive call
return arr[size-1] + sumOfArray(arr, size-1);
}
}

int main()
{
//array declaration and initialization
int arr[] = {0, 8, -4, 6, 7};
  
//function calling and display result
cout<<sumOfArray(arr, 5);

return 0;
}

OUTPUT:

17


Related Solutions

Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
a) Write an function that finds the independent variable value at which a mathematical function is...
a) Write an function that finds the independent variable value at which a mathematical function is maximized over a specified interval. The function must accept a handle to a function, and evaluate that function at values ranging from x1 to x2 with an increment of dx. The value returned is the value of x at which the maximum value of f(x) occurs. Function syntax: xm = xmax(f,x1,x2,dx); As was the case in the previous problem, this function does not find...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
C++ Code each of the following functions RECURSIVELY  (each function should use only the parameter value, and...
C++ Code each of the following functions RECURSIVELY  (each function should use only the parameter value, and any local function variables you might need ..no variables are to be declared outside the functions). void upTo(int number) This function prints all values from 1 up to the number. upTo(5); should output: 1 2 3 4 5 HINT: the following functions involve separating a digit from a number. Use of the arithmetic operators % and / are helpful here. Remember a % 10...
C++ Add a function that finds & displays all the words that begin with the letter...
C++ Add a function that finds & displays all the words that begin with the letter 's'. The text file "dickens.txt" is as follows: It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was...
1.please explain in details the Lump-Sum principle 2. Cubb- Douglass Utility function
1.please explain in details the Lump-Sum principle 2. Cubb- Douglass Utility function
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT