How can I correct my code to successfully reverse the array?

Hi all,
I'm having a bit of trouble understanding what I'm doing wrong. I need to create a function to reverse the array and then the function has to return a pointer to the new array...

It works, but I'm not sure if it is RIGHT... I'm just having trouble visualizing/understanding.

I'd appreciate any insight. Especially if you have any sites showing similar cases. Thanks.


Edit: I've edited this post... originally i could not get it to reverse, then i finally figured it out :) Not sure if this is what I was supposed to do or not (i'm a beginner, that goes without saying lol)

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
using namespace std;

// Function prototype
int *duplicateArray(const int *, int);
void displayArray(const int[], int);
void doubleArray(int [], int);
void showvalueDouble(int[], int);
void reverse(int[], int);
void printarray(int [], int );

int main()
{
   const int SIZE3 = 10;

   int array3[SIZE3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

   int *dup3 = 0;


   // Duplicate the arrays.
   dup3 = duplicateArray(array3, SIZE3);
// Display the original arrays.
   cout << "Here are the original array contents:\n";
   displayArray(array3, SIZE3);


//call reverse
reverse(dup3, SIZE3);
// Display the new arrays.
//display the numbers in reverse
   cout << "The values of the array in reverse are: \n";
printarray(dup3, SIZE3);

   cout << "\nHere are the duplicate arrays:\n";
   displayArray(dup3, SIZE3);
   //call double
    doubleArray(array3, SIZE3);
    //display double
    cout << "The double values are: \n";
    showvalueDouble(array3, SIZE3);


   // Free the dynamically allocated memory and
   // set the pointers to 0.

   delete [] dup3;
   dup3 = 0;
   return 0;
}

int *duplicateArray(const int *arr, int size)
{
   int *newArray = 0;

   // Validate the size. If 0 or a negative
   // number was passed, return a null pointer.
   if (size <= 0)
      return 0;

   // Allocate a new array.
   newArray = new int[size];

   // Copy the array's contents to the
   // new array.
   for (int index = 0; index < size; index++)
      newArray[index] = arr[index];

   // Return a pointer to the new array.
   return newArray;
}

void displayArray(const int arr[], int size)
{
   for (int index = 0; index < size; index++)
      cout << arr[index] << " ";
   cout << endl;
}
void doubleArray(int nums[], int size)
{
    for (int index = 0; index < size; index++)
        nums[index] *=2;
}

void showvalueDouble(int nums[], int size)
{
    for (int index = 0; index < size; index++)
        cout << nums[index] << " ";
    cout << endl;
}

void printarray(int dup3[], int count)
{
    for(int i = 0; i < count; ++i)
        cout<<dup3[i]<<' ';

    cout<<'\n';
}
void reverse(int dup3[], int count)
{
   int temp;
   for (int i = 0; i < count/2; ++i)
   {
      temp = dup3[i];
      dup3[i] = dup3[count-i-1];
      dup3[count-i-1] = temp;
}   }


Last edited on
Topic archived. No new replies allowed.