Source code Error

Hi all!
Why doesn't my code work? I want it to reverse the content of the passed array.
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
#include <iostream>

using namespace std;

void reverseArray(int array[]);

int main()
{
    int array[4] = {1, 2, 3, 4};
    
    reverseArray(array);
    
    for(int i = 0; i < 4; i++)
        cout << array[i] << " ";
        
    cin.get();
    return 0;
}

void reverseArray(int array[])
{
    int auxArray[4];
    
    for(int i = 0, int j = 4; i < 4; i++, j--)
        auxArray[j] = array[i];
        
    array = auxArray;
}


The compiler errors(Dev-C++):
 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Capitals.cpp In function `void reverseArray(int*)': 
24 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Capitals.cpp expected unqualified-id before "int" 
24 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Capitals.cpp expected `,' or `;' before "int" 
24 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Capitals.cpp `j' undeclared (first use this function) 
  (Each undeclared identifier is reported only once for each function it appears in.)


The compiler errors(Visual Studio 2008):
1>------ Build started: Project: For-loop, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\eigenaar\documents\visual studio 2008\projects\for-loop\for-loop\main.cpp(24) : error C2062: type 'int' unexpected
1>c:\users\eigenaar\documents\visual studio 2008\projects\for-loop\for-loop\main.cpp(24) : warning C4552: '<' : operator has no effect; expected operator with side-effect
1>c:\users\eigenaar\documents\visual studio 2008\projects\for-loop\for-loop\main.cpp(24) : error C2065: 'j' : undeclared identifier
1>c:\users\eigenaar\documents\visual studio 2008\projects\for-loop\for-loop\main.cpp(24) : error C2143: syntax error : missing ';' before ')'
1>c:\users\eigenaar\documents\visual studio 2008\projects\for-loop\for-loop\main.cpp(24) : error C2143: syntax error : missing ';' before ')'
1>Build log was saved at "file://c:\Users\eigenaar\Documents\Visual Studio 2008\Projects\For-loop\For-loop\Debug\BuildLog.htm"
1>For-loop - 4 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Any help? Thanks in advance!
Presumably because the i = 0 should have a terminator. Either way, that loop syntax is a little ugly. I think you could tidy that up with a bit of thought.
In the same way that this is bad:
int i = 0, int j = 4;
and this is not bad
int i = 0, j = 4;,

this is bad
for(int i = 0, int j = 4; i < 4; i++, j--)
and this is not bad
for(int i = 0, j = 4; i < 4; i++, j--)
1
2
3
4
5
6
7
8
9
void reverseArray(int array[])
{
    int auxArray[4];
    
    for(int i = 0, int j = 4; i < 4; i++, j--)  //error here
        auxArray[j] = array[i];
        
    array = auxArray; //Not a C++ error - but certainly a programmer error
}


int x =0, int j=4 ; //Error - Cannot declare variables in this manner.

ALSO j should start at 3 otherwise you will have an array out-of bounds access crash.
You meant
for(int i = 0, j = 3; i < 4; i++, j--)


Here is a operational problem :
array = auxArray; // This will not work the way you are thinking

Oh, of course, int i, int j is nonsense. I was still sleeping when posting this, I suppose :D But what do you mean by "operational problem"?
fransje wrote:
... But what do you mean by "operational problem"?


in the function void reverseArray(int array[]), the variable called array is
an int * and is local to the function.
So doing this array = auxArray; will not affect anything in the main function.
So the printout you get in the main functions will be 1 2 3 4 - NO reversal
Last edited on
Yeah, I noticed that. But how then do I have to pass that array by reference? (Because void reverseArray(int& array[]) doesn't work) Or is it better to use a function that returns an array?
When you've finished assembling the reverse, just copy it over array.

1
2
3
4
5
6
7
8
9
10
11
12
void reverseArray(int array[])
{
    int auxArray[4];
    
    for(int i = 0, j = 3; i < 4; i++, j--)  //error here
        auxArray[j] = array[i];
        
   // auxArray is now array, backwards.
   for(int i = 0; i < 4; ++i)
  { array[i] = auxArray[i];}

}
Thanks all for the help! I solved it by using a struct (sorry for all the 'array's :D):

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
#include <iostream>

using namespace std;

struct array
{
    int array[4];
};

void reverseArray(array& array);

int main()
{
    array theArray = {1, 2, 3, 4};
    for(int i = 0; i < 4; i++)
        cout << theArray.array[i] << " ";
    
    reverseArray(theArray);
    
    cout << "\n\n";
    for(int i = 0; i < 4; i++)
        cout << theArray.array[i] << " ";
    
    cin.get();
    return 0;
} 

void reverseArray(array& theArray)
{
    array auxArray;
    for(int i = 0; i < 4; i++)
        auxArray.array[i] = theArray.array[i];
    
    for(int i = 0, j = 3; i < 4; i++, j--)
        theArray.array[i] = auxArray.array[j];
}


I'll make a new post for the problem about passing arrays by reference, and how to return them. In any case: Thanks! :)

Topic archived. No new replies allowed.