Question

In: Computer Science

Using C++: Write a function that recursively calculates the sum of an array. The function should...

Using C++: Write a function that recursively calculates the sum of an array. The function should have 2 parameters, the array and an integer showing the number of elements (assume they are all integers) in the array. The function will use a recursive call to sum the value of all elements.

You must prompt for a series of integers to be entered in the array. As your program reads the numbers in, increment a count so it knows how many elements the array has. Use a symbol for users to indicate when they are done with entering numbers (choose any letter you think is proper, but you need to print it on screen at the beginning to let users know which one to enter). Then your program will print the sum of the array elements on the screen.

Solutions

Expert Solution

#include<iostream>
#include <stdlib.h>

using namespace std;

int sumArray(int arr[], int size) {
   int sum = 0;
   if(size > 0)
       sum += arr[size-1]+sumArray(arr,size-1);
   else
       return sum;
}

int main() {

   int randArray[500];
   int num = 0;
   int count = 0;
   cout << "Enter a number: (Enter any non-numeric character to quit)" <<endl;
   while(cin >> num) {
       randArray[count] = num;
       count++;
       cout << "Enter a number: (Enter any non-numeric character to quit)" <<endl;
   };

   int sum = sumArray(randArray, count);
   cout <<"Sum: " << sum << endl;

}

------------output-----------------
Enter a number: (Enter any non-numeric character to quit)
1
Enter a number: (Enter any non-numeric character to quit)
2
Enter a number: (Enter any non-numeric character to quit)
-1
Enter a number: (Enter any non-numeric character to quit)
-2
Enter a number: (Enter any non-numeric character to quit)
q
Sum: 0


Related Solutions

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 . . .
Write a program in C++ that calculates the sum of two fractions. The program should ask...
Write a program in C++ that calculates the sum of two fractions. The program should ask for the numerators and denominators of two fractions and then output the sum of the two fractions. You will need to write four functions for this program, one to read the inputted data, one to calculate the sum of the two fractions, one to find the Greatest Common Divider (GCD) between the numerator and denominator and a function that will display the end result....
Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float)...
Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float) and pow (int) > = 1, and it returns the result root raised to power pow. The recursion of the power function is defined as: Base case: rootpow = root, if pow = 1 Recursive case: rootpow = root* rootpow-1 , otherwise. b) Develop a greatestC( ) function that determines the greatest common divisor of two integer numbers. Test greatestC() in a simple main...
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
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.
Make a function that calculates the summation of even numbers in the range. The function sum...
Make a function that calculates the summation of even numbers in the range. The function sum takes the two integer parameters and they are used as the range. The function uses default parameters for the range. When we call this function with one argument, it will be used as a starting point and the end of the range will be 100. Also, the function is called without any argument, the default range (0,100) will be used. We will use default...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
1D Array Function Write a function in MATLAB to convert miles to feet. This function should...
1D Array Function Write a function in MATLAB to convert miles to feet. This function should work for one number, or an array of numbers to be converted.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT