Hi all, I am trying to reverse the numbers in an array using recursion, however, I am receiving an error that I can't figure out. If possible, I need some assistance on it.

Error:

line 27 incompatible types in assignment of `int' to `int'

intArray = reverseArray(intArray,low,high);

line 57 invalid conversion from `int*' to `int'

return intArray;

Example Output:

Original Array:
1 2 3 4 5

Reverse Array:
5 4 3 2 1

Program:

``````#include <iostream>
#include <string>
using namespace std;

int reverseArray(int intArray[], int low, int high);

const int length = 5;

int main()
{

int intArray[length] = {1,2,3,4,5};
int low = 1;
int high = 4;

cout << "Original Array: ";

for (int i=0; i<length; i++)
{
cout << intArray[i] << " ";
}

intArray = reverseArray(intArray,low,high);

cout << "Reverse Array: ";

for (int i=0; i<length; i++)
{
cout << intArray[i] << " ";
}

system("PAUSE");
return 0;
}

int reverseArray(int intArray[], int low, int high)
{

int swap;

for (high; high < --low; high++)
{
swap = intArray[high];
intArray[high] = intArray[low];
intArray[low] = swap;
}
return intArray;

}``````

``reverseArray(intArray,low,high);``

what is the value of low and high. U didn't initialized them.
Would u plz first go through your code and do a dry run using pen and paper.

## All 4 Replies

check the prototype of the function reverseArray()

I got my program to run however, I am not getting the reverse. Can someone give me a hint to what I am doing wrong? Appreciate it.

Output:

Original Array: 1 2 3 4 5

Reverse Array: 1 2 3 4 5

``````#include <iostream>

using namespace std;

const int length = 5;

void reverseArray(int *intArray, int low, int high);

int main()
{

static int intArray[] = {1,2,3,4,5};
int low;
int high;

cout<< "Original Array: ";

for (int i=0; i<length; i++)
{
cout<< intArray[i] << " ";
}

cout<<endl;

reverseArray(intArray,low,high);

cout << "Reverse Array: ";

for (int i=0; i<length; i++)
{
cout << intArray[i] << " ";
}
cout<<endl;

system("PAUSE");
return 0;
}

void reverseArray(int *intArray, int low, int high)
{

int swap;

for (high; high < --low; high++)
{
swap = intArray[high];
intArray[high] = intArray[low];
intArray[low] = swap;
}

}``````
``reverseArray(intArray,low,high);``

what is the value of low and high. U didn't initialized them.
Would u plz first go through your code and do a dry run using pen and paper.

sorry dud this is not recursion can you tell me how to do it using recursion.
the problem is you are only having one parameter in your function that is char array and the function in the end should return the reverse array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.