pointers and functon

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.

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
46
47
48
49
50
51
52
53
 //header file

//include 

#include<iostream>

using namespace std;

//Function prototypes
int reverse_array (int *[], int); 

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(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 = new int[arraySize];
	int u =0;
	
	for (int i=arraySize - 1;i>=0; i--) 
      {
		  newArray[u] = integersArray[i];
		  u++;
	  }
	  return newArray;
  }

try
int *reverse_array (int *, int);

and

int *reverse_array(int *integersArray, int arraySize)


and
reverse_Array = reverse_array(integers, SIZE);

your problems are mostly syntax. See if these get you going.

and please don't name a function and a variable nearly the same thing. Its very hard to read and easy to confuse.
Last edited on
#include<iostream>

using namespace std;

//Function prototypes
int* reverse_array(int [], int);

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.
I wrote, and prefer, int * to int []. Both work, don't let this confuse you :)

Topic archived. No new replies allowed.