Question

Write a recursive method “int sumPos(int a[], int n)” to calculate the sum of positive integers...

Write a recursive method “int sumPos(int a[], int n)” to calculate the sum of positive integers in an array of n integers, a[].

No global variables are allowed. No other method is allowed to be called by sumPos() except itself. (Hint: The integers in an array a[] of n integers are stored in a[0], a[1], ... a[n-1].)

Homework Answers

Answer #1

int sumPos(int a[],int n){
if(n<0)
return 0;//return 0 if array has no elements left
else if(a[n-1]>0)
return a[n-1]+sumPos(a,n-1);//if positive element add it and call function for other elements
else
return sumPos(a,n-1);//if negative number, do not add it
}

Explanation:

The above code uses recursion.

The n represents the number of elements.

So, the array addition od positive numbers starts from the last index of array.

If the element is positive then the element must be added and the recursive call must be made on the remaining elements.

If the element is negative then the element must not be added and the recursive call must be made on the remaining elements.

Know the answer?
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
Similar Questions
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters,...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters, each greater or equal to zero, and evaluates to their sum. In this problem, you must use the built-in functions "add1" and "sub1" and may not use the built-in functions "+" or "-". For example, (sum 2 3) should evaluate to 5. Note: (add1 5) evaluates to 6 and (sub1 4) evaluates to 3. Hint: like you saw in the "append" lecture, treat one...
IN JAVA: Write recursive method to return true if a given array has element equal to...
IN JAVA: Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp,...
Write a recursive method body for the method whose contract is given below. Note that n...
Write a recursive method body for the method whose contract is given below. Note that n is a restores-mode parameter. As an example, if n = 314, the method should return 1. You can only use the NaturalNumber kernel methods multiplyBy10, divideBy10, and isZero. /** * Reports the minimum (i.e., smallest) digit of n when it is * written in ordinary decimal notation. * ... * @ensures * minDigit = [the minimum digit of n] */ private static int minDigit(NaturalNumber...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Which of the following data structure can’t store non-homogeneous data elements? A) Arrays B) Linked Lists...
    asked 4 minutes ago
  • Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose...
    asked 5 minutes ago
  • 4. Prove explicitly that congruence modulo 4 is an equivalence relation. Then list the equivalence classes....
    asked 17 minutes ago
  • a.) The photoelectric effect is the basis of the spectroscopic technique known as photoelectron spectroscopy. An...
    asked 18 minutes ago
  • [The following information applies to the questions displayed below.] Washington Warehouse is a small retail business...
    asked 25 minutes ago
  • Given the following two sets of quotations by two currency dealers: Dealer A                               &n
    asked 27 minutes ago
  • The programming language for this exercise must be on C++. The exercise is the following: Design...
    asked 27 minutes ago
  • Continuity, differentiability questions: • Give an example of a function f: R → R that is...
    asked 27 minutes ago
  • What are some non-verbal differences in cultures, different countries, etc that can be mis-interpreted in a...
    asked 37 minutes ago
  • The probability that a person has a certain disease is 0.05 Medical diagnostic tests are available...
    asked 48 minutes ago
  • In operating system: What process resources are associated with each thread that it does not share...
    asked 49 minutes ago
  • Suppose that in a senior college class of 500 ​students, it is found that 212 smoke,...
    asked 55 minutes ago