#include <iostream>
usingnamespace 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 ==========
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.
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
... 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
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?