the function doesn't return and also shows error reverse_Array = reverse_array(int*[],int). what else can i do for improvements. The question was to create reverse array using functions, pointers and array.
//header file
//include
#include<iostream>
usingnamespace std;
//Function prototypes
int reverse_array (int *[], int);
int main()
{
constint SIZE = 5; //const declaration
int integers[SIZE];
int *reverse_Array; // array pointer
cout<<"Enter the "<<SIZE<<" integers you would like to reverse:\n";
for( int i=0; i<SIZE; i++)
cin>>integers[i];
//Display the numbers
cout<<"elements in the orginal array:";
for( int i=0; i<SIZE; i++)
cout<<integers[i]<<" ";
reverse_Array = reverse_array(int*[],int);
//Display results
cout<<"Elements in the reversed array:";
for(int i=0; i<SIZE; i++)
cout<<reverse_Array[i]<<"";
}
int reverse_array(int integersArray[], int arraySize)
{
int *newArray;
newArray = newint[arraySize];
int u =0;
for (int i=arraySize - 1;i>=0; i--)
{
newArray[u] = integersArray[i];
u++;
}
return newArray;
}
int main()
{
const int SIZE = 5; //const declaration
int integers[SIZE];
int *reverse_Array; // array pointer
cout << "Enter the " << SIZE << " integers you would like to reverse:\n";
for (int i = 0; i < SIZE; i++)
cin >> integers[i];
//Display the numbers
cout << "elements in the orginal array:";
for (int i = 0; i < SIZE; i++)
cout << integers[i] << " ";
reverse_Array = reverse_array(integers, SIZE);
//Display results
cout << "Elements in the reversed array:";
for (int i = 0; i < SIZE; i++)
cout << reverse_Array[i] << "";
//system("pause");
}
int* reverse_array(int integersArray[], int arraySize)
{
int *newArray;
newArray = new int[arraySize];
int u = 0;
for (int i = arraySize - 1; i >= 0; i--)
{
newArray[u] = integersArray[i];
u++;
}
return newArray;
}
here is your code with corrctions to make it work. However, i think you need to improve on naming variables like jonnin said. Remember, syntax like int *[] is wrong as an array is already a memory address so you cant make it a pointer. just use int [] and you will be fine. Also, the return type cannot be just int if you want to return an array, use a pointer instead as it can hold the address of the array. Otherwise, happy programming, and ask again if the above code is problematic.