Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the project I need to complete in C++ using Xcode on a Mac : Reverse Arr

ID: 3878118 • Letter: H

Question

Here is the project I need to complete in C++ using Xcode on a Mac:

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 newarray . 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 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.

I have tried to solve this project many times. Part of the problem is that I am not familiar with C++ and Xcode. I don't know how to work it properly. So, the problem I am having is that I am getting a blank output. I know I need to create a "data.txt" and put it in the same folder as the code. However, I have tried that so many times and nothing works at all. If you are familiar with Xcode on a Mac, I need specific instructions on exactly what to do to get this work. I have been trying for days to figure this out and I am about to pull my eyeballs out. LOL! So, if anyone can help at all, I would truly appreciate it.

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

Note:
To make XCode find your file,
In Xcode go to Product > Scheme > Edit Scheme > Run (on the right) > Options (middle top)
Select your working directory there... the directory where you have placed your .txt file

Let me know if it still does not work for you...

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

int* reverse(int num[], int n);
void display(int num[], int n);

int main()
{
string filename;
int n;
int numbers[100];
int *rev;
  
cout << "Enter value of n: " << endl;
cin >> n;
  
cout << "Enter the numbers: " << endl;
for(int i = 0; i < n; i++)
cin >> numbers[i];
  
  
cout << "Original input array is " << endl;
display(numbers, n);
  
rev = reverse(numbers, n);
cout << "Reversed array is " << endl;
display(rev, n);
delete[] rev; //deallocate
  
  
cout << "Enter filename containing atleast " << n << " numbers: " << endl;
cin >> filename;
ifstream inFile(filename.c_str());

  
if(inFile.is_open())
{
for(int i = 0; i < n; i++)
inFile >> numbers[i];
inFile.close();
  
cout << "Numbers read from file " << endl;
display(numbers, n);
  
rev = reverse(numbers, n);
cout << "Reversed array is " << endl;
display(rev, n);
delete[] rev; //deallocate

}
  
}

int* reverse(int num[], int n)
{
int *rev = new int[n];
for(int i = 0, j = n-1; i < n; i++, j--)
rev[i] = num[j];
return rev;
}
void display(int num[], int n)
{
for(int i = 0; i < n; i++)
{
if(i % 10 == 0) //print newline for every 10 numbers
cout << endl;
cout << num[i] << " ";

}
cout << endl;
}


sample input file: num1.txt
===========
4 6 8 10 12 14 16 18

output
=======
Enter value of n:
5
Enter the numbers:
1 2 3 4 5
Original input array is

1 2 3 4 5
Reversed array is

5 4 3 2 1
Enter filename containing atleast 5 numbers:
num1.txt
Numbers read from file

4 6 8 10 12
Reversed array is

12 10 8 6 4
Program ended with exit code: 0