How to remove first element in multidimentional array?

Write a recursive, int -valued function named productOfOdds that accepts an integer array, and the number of?

  • Write a recursive, int -valued function named productOfOdds that accepts an integer array, and the number of elements in the array and returns the product of the odd-valued elements of the array. You may assume the array has at least one odd-valued element. The product of the odd-valued elements of an integer-valued array recursively may be calculated as follows: If the array has a single element and it is odd, return the value of that element; otherwise return 1. Otherwise, if the first element of the array is odd, return the product of that element and the result of finding the product of the odd elements of the rest of the array; if the first element is NOT odd, simply return the result of finding the product of the odd elements of the rest of the array my answer is(but wrong): void productOfOdds(int a[], int n) { if (n < 0) {return 0;} if (n > 1) {return a[n-1]} if(a[0]>1){ productOfOdds(a+1, n-1); return 1;} } with C++ please.

  • Answer:

    With such clear instructions, the function practically writes itself. You have a few problems, including: . your function's return value is not int . you're not checking for odd values . your function does not compute a sum It should look like this: int productOfOdds(const int *a, size_t n) {   if (n == 1) {     if (a[0] & 1) {       return a[0];     } else {       return 1;     }   }   if (a[0] & 1) {     return a[0] * productOfOdds(a + 1, n - 1);   } else {     return 0 + productOfOdds(a + 1, n - 1);   } }

abn at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

well, go ahead and do your homework. it will be a good execrise for you. I am sure recursion was covered in class. I hope you took good notes for this one. you can think of your instructor kind of like your engineering manager. he may even say that. if he does, good for him. he's giving you good instruction.

Jim

Show your code. = People will be able to help you get it right.

tbshmkr

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.