Recursion function

Writso i am trying to make a recusive function to calculate the number of even functions in an array.
The function takes the array, starting point, ending point and an initial value of the sum =0
Any chance i can remove the sum variable and use only the other three?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  main()
Int sum=0
Int array []={1,2,3,4,5};
Int start= 0
Int end = 4
Evenfunction(array,start,end,sum);
// Function
Evenfunction (a,s,e,sum)
If s=e
Return sum
Else 
{If (a[start]%2==0)
Sum++:
Return evenfunction (a,s+1,e,sum)
Hello Echelon22,

You should compile you code before posting. I have 29 errors that needed fixed before I could even test the code.

From the top as your code is laid out:

You are missing the header files.

Since your function comes after main you will need a prototype for the function. Or move the function above main.

main () needs to be written as int main().

Key words like "int", "if", "else", "else if", "return" and others all start with a lower case letter.

You are missing the {}s that define the block for main and the function.

All through out the program most of the lines of code are missing the ending ";".

In the function definition everything between the ()s needs to have a type and the "a" used for the array will need []s.

In the function definition you use "sum" lower case letters, but at line 13 you use "Sum". The latter will be considered as an undefined variable and a compiler error. When you define a variable in the function parameter it is the same as defining that variable inside the function, so any use inside the function the variable names have to match spelling ans case of the letters.

Line 12 has an opening '{' which is unmatched and is not needed in the first place.

You may find these of some use:
http://www.cplusplus.com/doc/tutorial/functions/

http://www.cplusplus.com/doc/tutorial/control/

While you are reading notice how the programs are written.

For your question about passing "sum". Being passed by value this variable has no use as a function parameter, but is does have a use to capture the return value of the function. So basically "sum" is on the wrong end of the function call. It needs to catch the return value of the function call.

When I fixed the program it worked and gave me the answer of 2. I believe this is the correct answer. You have a program that can work you just need to understand the syntax of the language.

A few blank lines in your code would help to see what is missing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

main()

Int sum = 0
Int array[] = { 1,2,3,4,5 };
Int start = 0
Int end = 4

Evenfunction(array, start, end, sum);

// Function
Evenfunction(a, s, e, sum)

If s = e
Return sum
Else
{ If(a[start] % 2 == 0)
Sum++:

Return evenfunction(a,s + 1,e,sum)  // <--- Watch spelling of function name. Easy to miss.


Correct your code the best you can and post it to a new message. Also include any error messages from the compiler that you do not understand and we can work from there.

Hope that helps,

Andy
Topic archived. No new replies allowed.