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
Write an algorithm for a function named finder that recursively goes through a list and returns...
Write an algorithm for a function named finder that recursively goes through a list and returns the index of the first occurrence of an item of interest. The function takes three parameters: 1. The first position in the list (or 0) 2. The list we want to iterate over 3. The item we want to find. If the item is not there in the list and we have iterated over the whole list, then the function should return 0.
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.
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
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 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[] =...
-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++) 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 a bash script to find all the files ending with .c recursively for every directory...
Write a bash script to find all the files ending with .c recursively for every directory in your current working directory, then copy each one to a folder called programs, need handle duplicates by appending the number to the end of the file (ex main.c main-1.c ) compile them and generate a report report should look like: main.c compiles prog2.c failed main-2.c compiles etc.
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should...
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT