Copying an array and then reversing the numbers
Nov 6, 2014 at 3:50pm UTC
Good morning everyone. I am working on a program that will let the user input 10 numbers into an array then copy that array and hopefully output the copied array in reverse. So far, I have the code to build both arrays and output both but, the second one is not yet reversed. Just wondering if someone could point me in the right direction.
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 54 55 56 57 58 59 60
#include <iostream>
using namespace std;
int *duplicateArray(const int *, int );
void displayArray(const int [], int );
int main()
{
const int SIZE = 10;
int numbers[SIZE];
cout<<"Please enter " <<SIZE<<" numbers for the array: \n" ;
for (int i=0; i<SIZE; i++)
cin>> *(numbers + i);
int *dup = nullptr ;
dup = duplicateArray(numbers, SIZE);
cout<<"Here are the original numbers you entered:\n" ;
displayArray(numbers, SIZE);
cout<<"Here are the numbers in reverse order:\n" ;
displayArray(dup, SIZE);
delete [] dup;
dup = nullptr ;
system("pause" );
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int *duplicateArray(const int *arr, int size)
{
int *newArray = nullptr ;
if (size<=0)
return nullptr ;
newArray = new int [size];
for (int i=0; i<size; i++)
newArray[i] = arr[i];
return newArray;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void displayArray(const int arr[], int size)
{
for (int i=0; i<size; i++)
cout<<arr[i]<<" " ;
cout<<endl;
}
Last edited on Nov 6, 2014 at 3:51pm UTC
Nov 6, 2014 at 4:00pm UTC
Change line 50:
newArray[i] = arr[size - i - 1 ];
Nov 6, 2014 at 4:16pm UTC
Thanks coder777!! I did not know I was that close to having it done!
Topic archived. No new replies allowed.