answersLogoWhite

0


Best Answer

You mean an iterative function, not a recursive one. A recursive function is one that calls itself with altered arguments, typically used to implement divide-and-conquer algorithms. Since the arguments remain the same and you're not dividing the array into smaller subsets, recursion is not necessary. An iterative loop is all you need:

typedef unsigned int UINT;

const UINT CountOddNumbers( const UINT* const A, const UINT size)

{

UINT result=0;

for(UINT i=0; i<size; ++i)

result += A[i]&1;

return(result);

}

Note that this function is not safe, since size is not guaranteed to reflect the actual size of A. In C++, it's better to use a vector object, since vectors encapsulate the actual size of the embedded array:

const UINT CountOddNumbers( const std::vector<UINT> A)

{

UINT result=0;

for(UINT i=0; i<A.size(); ++i)

result += A[i]&1;

return(result);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a recursive function CountOddNumbers that accepts as parameters an array of integers and the array size The function should count and return the odd numbers in the array of integer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a C program to find the GCD of two given integers using non recursive functions?

Use the following function: int gcd (int a, int b) { while (b != 0) { a %= b; a ^= b ^= a ^= b; } return a; } Note that a ^= b ^= a ^= b is an efficient method of swapping two values.


What is simulation recursion in C?

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }


What is a parameter passing?

Passing parameters probably means passing a parameter into a function.Basically, this happens when you call a function. You put the parameter into a function, then call it, and the function does something to the parameters you put into it. Here's an example: #include &lt;iostream&gt; using namespace std; int sum(int a, int b); //Declare the function, so the program knows that it exists int main() { int first, second, total; cout&lt;&lt;"Please insert two numbers."&lt;&lt;endl; cin&gt;&gt;first&gt;&gt;second; total=sum(first, second); /*Here we are passing the variables (parameters) "first" and "second" into the function "sum". This function then does something to them and outputs the value. In this case, it is stored in the variable "total"*/ cout&lt;&lt;"Their sum is "&lt;&lt;total&lt;&lt;"."&lt;&lt;endl; return 0; } //Here we define the function (tell the program what to do) int sum(int a, int b) { //We pass two integers into the function. Their values are stored in a and b. a and b can then be used by the function. sum=a+b; return sum;//Adds them, then returns the sum }


What is recursive algorithm?

Algorithm can be defined as an interpretable, finite set of instructions for dealing with contigencies and accompanying task that has recognizable end-points for given inputs. It is a tool for solving a well computational problem. A recursive algorithm is one which calls itself.


Write a program to find gcd using recursive method in java?

for two positive integers: public static int gcd(int i1, int i2) { // using Euclid's algorithm int a=i1, b=i2, temp; while (b!=0) { temp=b; b=a%temp; a=temp; } return a; }

Related questions

Write a c program to find GCD of two given integers by using both recursive n non recursive functions?

i love u darling


Give a recursive definition for the language L of positive integers divisible by 2 or 7?

What alphabet are you using, and how are the natural numbers represented in this alphabet?


What are integres?

The integral zeros of a function are integers for which the value of the function is zero, or where the graph of the function crosses the horizontal axis.


How do you write a C program to find the GCD of two given integers using non recursive functions?

Use the following function: int gcd (int a, int b) { while (b != 0) { a %= b; a ^= b ^= a ^= b; } return a; } Note that a ^= b ^= a ^= b is an efficient method of swapping two values.


What is simulation recursion in C?

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }


What are integral zeros?

The integral zeros of a function are integers for which the value of the function is zero, or where the graph of the function crosses the horizontal axis.


What is an arithmetic function?

An arithmetic function is any function which is defined for all positive integers, and has values which are either real or complex.


What is recursive methods?

A recursive method is a method that can invoke itself. The classical example is N factorial...int nfact (int N) {if (N == 2) return N else return N * nfact (N - 1);}The example suffers from truncation issues, because N Factorial gets very large, very quickly, with relatively small values of N, and ordinary integers do not support that. The answer, however, is sufficient for the question of "what is a recursive method?"


Is the cardinality of an infinitely countable set the same as the rational numbers?

Yes. There is an injective function from rational numbers to positive rational numbers*. Every positive rational number can be written in lowest terms as a/b, so there is an injective function from positive rationals to pairs of positive integers. The function f(a,b) = a^2 + 2ab + b^2 + a + 3b maps maps every pair of positive integers (a,b) to a unique integer. So there is an injective function from rationals to integers. Since every integer is rational, the identity function is an injective function from integers to rationals. Then By the Cantor-Schroder-Bernstein theorem, there is a bijective function from rationals to integers, so the rationals are countably infinite. *This is left as an exercise for the reader.


Why isn't 2 or any other even positive integer a trivial zero of the Riemann Zeta Function?

Coz the gamma function is singular for all negative integers. The factorial for negative integers is not defined.


What is the function whose domain is a set of consecutive integers there are finite and infinite?

sequence


What is a parameter passing?

Passing parameters probably means passing a parameter into a function.Basically, this happens when you call a function. You put the parameter into a function, then call it, and the function does something to the parameters you put into it. Here's an example: #include &lt;iostream&gt; using namespace std; int sum(int a, int b); //Declare the function, so the program knows that it exists int main() { int first, second, total; cout&lt;&lt;"Please insert two numbers."&lt;&lt;endl; cin&gt;&gt;first&gt;&gt;second; total=sum(first, second); /*Here we are passing the variables (parameters) "first" and "second" into the function "sum". This function then does something to them and outputs the value. In this case, it is stored in the variable "total"*/ cout&lt;&lt;"Their sum is "&lt;&lt;total&lt;&lt;"."&lt;&lt;endl; return 0; } //Here we define the function (tell the program what to do) int sum(int a, int b) { //We pass two integers into the function. Their values are stored in a and b. a and b can then be used by the function. sum=a+b; return sum;//Adds them, then returns the sum }