unknown error in basic recursion problem, code included.

Hello world, I am writing a recursive function called maxArray as a practice problem from my data structures book. It finds the greatest integer in an array by continuously dividing it into left and right halves until the array sizes are 1, and then compares each to the adjacent value and returns the greater back up the recursive chain. (For now I have the test array built into the main function, I'll read it from a file after I get it working) Unfortunately I have two errors that I am unable to solve:

RecursiveGreatestInt.cpp:23: error: invalid types `int[int]' for array subscript
RecursiveGreatestInt.cpp:45:2: warning: no newline at end of file

Apparently my "start" value I’m using as a base case return value is an "invalid type"
And...
I have no idea what it means by "no newline at end of file".

I am very new to programming and my only friend who can help is out seeing Watchmen. Any help would be gratefully appreciated. Thanks!

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;
int maxArray(int arg[], int start, int end);

int main()
{
    int low = 0;
    int high = 12;
    int array[] = {7, 11, 55, 32, 19, 15, 27, 79, 81, 2, 6, 55, 77};
    cout << high << endl;
    cout << maxArray(array, low, high) <<endl;
    return (0);
}

int maxArray(int arg, int start, int end)
{
    int size;
    size = (end-start)+1;     
    if (size == 1)
        return arg[start];
    else if(size%2 != 0)      
    {
        int mid;
        mid = (start+end)/2;  
        if (maxArray(arg, start, mid) > maxArray(arg, mid, end))
            return maxArray(arg, start, mid);
        else
            return maxArray(arg, mid, end);
    }
    else
    {
        int leftEnd;
        leftEnd = (start+end-1)/2;
        int rightStart;
        rightStart = (start+end+1)/2;         

        if (maxArray(arg, start, leftEnd) > maxArray(arg, rightStart, end))
            return maxArray(arg, start, leftEnd);
        else
            return maxArray(arg, rightStart, end);
    }
}


p.s. hopefully this will be the first post of many. Thanks again.
-L.P.
1. When you are declaring your function, you tell it to take an int array[], but in the definition, you only use an int array.

2. Try just adding an enter to the end of the file.
Thank you very much firedraco. I'm sorry I posted in two forums, I will not do that again.

The first answer makes sense, initially I had the brackets on every instance of arg, and then a friend told me to only use it in the prototype, which made my error count go way down, but obviously I also took it out of the declaration as well.

Just out of curiosity/to bolster my understanding of what is actually going on, why are the brackets unnecessary in the instances within the function declaration? To me it seems as if there should be no harm in keeping a reference to memory there...or is it precisely just that, that the location in memory is changing with each recursive call?

Thanks again,
LP
I think my wording was confusing, sorry I meant in declaration you used int arg[], but in the definition you just used int arg (they are different);

Basically, arrays are pointers the the first element of the array (look in the tut for details on that). Thus, int array[] is basically equal to int* array. Since your function is expecting an int* (and not just an int or something similar), if you just pass arg (which is an int*) you will be passing the correct data type.

I think I answered you correctly...tell me if I didn't please.
yep I gotcha. Thanks again.
- L.P.
I know this is all about use of recursion, but these bits here seems to be
a bit of overkill (or am I missing something):
1
2
3
4
  if (maxArray(arg, start, mid) > maxArray(arg, mid, end))
            return maxArray(arg, start, mid);
        else
            return maxArray(arg, mid, end);


and:
1
2
3
4
        if (maxArray(arg, start, leftEnd) > maxArray(arg, rightStart, end))
            return maxArray(arg, start, leftEnd);
        else
            return maxArray(arg, rightStart, end);

Topic archived. No new replies allowed.