9.10: Reverse Array

Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array. The program then passes the array to the your reverse array function, and prints the values of the new reversed array on standard output, one value per line. You may assume that the file data has at least N values.

Prompts And Output Labels. There are no prompts for the integer and no labels for the reversed array that is printed out.

Input Validation. If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently.

#include<iostream>
#include<fstream>
using namespace std;

int *reverse(const int *, int);

int main()
{
int N;
cin >> N;
if(N > 50 || N < 0)
exit(0);

int *arr1 = new int[N], index;

fstream datafile;
datafile.open("data.txt", ios::in | ios::out);

for(index = 0; index < N; index++)
{
datafile >> arr1[index];
}
int *temp;

temp = reverse(arr1,N);

for(index = 0; index < N; index++)
cout << temp[index] << endl;
}

int *reverse(const int *arr1, int N)
{
int index = N-1;
int *arr2;
arr2 = new int[N];
for(int count = 0; count < N; count++)
{
arr2[count] = arr1[index];
index--;
}
return arr2;
}

I need to return a pointer but I am returning junk, help

I have made some modifications to eliminate reading from keyboard and the datafile, and the code prints the array in reverse order. So logic is fine.

We will need to look at the data.
One thing to check is right after you have read your data file, print them out to see if they have been read properly.
If that works, print the array on entry to reverse, and just before exit.
All this is to locate where the problem is.
Once you know where the problem is, the problem scope will be reduced significantly.

Don't know if myprogramminglab lets you do all that, but you can do these tests on your own compiler.

Here's the modified code.
#include<iostream>
#include<fstream>
using namespace std;

int *reverse(const int *, int);

int main()
{
int N;
N=10;
/*
cin >> N;
if(N > 50 || N < 0)
exit(0);
*/
// int *arr1 = new int[N], index;
int *arr1=new int[N];
int index;
for(int i=0;i<N;i++)arr1[i]=i;
/*
fstream datafile;
datafile.open("data.txt", ios::in | ios::out);

for(index = 0; index < N; index++)
{
datafile >> arr1[index];
}
*/
int *temp;

temp = reverse(arr1,N);

for(index = 0; index < N; index++)cout << temp[index] << endl;
system("PAUSE");
}

int *reverse(const int *arr1, int N)
{
int index = N-1;
int *arr2;
arr2 = new int[N];
for(int count = 0; count < N; count++)
{
arr2[count] = arr1[index];
index--;
}
return arr2;

}

after the due date my teacher told me what was wrong with it apparently the entire problem was the .txt which I erroneously thought you had to have, ty for your help

you're welcome! :)

Sure, I can help you.

In your code, you are returning a pointer to the new array `arr2` from the `reverse` function. However, the issue you are facing is that the values you get when printing the reversed array are junk.

To fix this issue, you need to ensure that the new array `arr2` is properly initialized before assigning values to it in the `reverse` function.

Here's an updated version of your code with the necessary changes:

```cpp
#include<iostream>
#include<fstream>
using namespace std;

int *reverse(const int *, int);

int main()
{
int N;
cin >> N;
if (N > 50 || N < 0)
exit(0);

int *arr1 = new int[N], index;

fstream datafile;
datafile.open("data.txt", ios::in | ios::out);

for (index = 0; index < N; index++)
{
datafile >> arr1[index];
}
int *temp;

temp = reverse(arr1, N);

for (index = 0; index < N; index++)
cout << temp[index] << endl;

delete[] arr1; // Free memory allocated for arr1
delete[] temp; // Free memory allocated for temp

return 0;
}

int *reverse(const int *arr1, int N)
{
int *arr2 = new int[N]; // Initialize arr2 with the correct size
int index = N - 1;

for (int count = 0; count < N; count++)
{
arr2[count] = arr1[index];
index--;
}

return arr2;
}
```

With these changes, the code should now properly allocate memory for `arr2` and return a pointer to the reversed array without any junk values.

Let me know if you have any other questions!

In your code, you are creating a dynamic array `arr2` in the `reverse` function and returning a pointer to it. However, there is no memory allocation for the `arr2` array in the `reverse` function, which causes undefined behavior and may result in junk values being returned.

To fix this issue, you need to allocate memory for the `arr2` array before assigning values to it. You can use the `new` keyword to allocate memory dynamically. Here's the modified `reverse` function:

```cpp
int *reverse(const int *arr1, int N)
{
int index = N - 1;
int *arr2 = new int[N]; // Allocate memory for arr2
for(int count = 0; count < N; count++)
{
arr2[count] = arr1[index];
index--;
}
return arr2;
}
```

By allocating memory using `new int[N]`, you ensure that `arr2` has enough memory to store the reversed values. Make sure to deallocate this memory later to avoid memory leaks.

Additionally, in your `main` function, you are not closing the `datafile` after reading the input values. You can add `datafile.close();` after the `for` loop to close the file.

With these modifications, your code should properly reverse the array and return a valid pointer to the reversed array.