computer science

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

  1. 👍
  2. 👎
  3. 👁
  1. 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;

    }

    1. 👍
    2. 👎
  2. 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

    1. 👍
    2. 👎
  3. you're welcome! :)

    1. 👍
    2. 👎

Respond to this Question

First Name

Your Response

Similar Questions

  1. math

    For some constants $a$ and $b,$ let \[f(x) = \left\{ \begin{array}{cl} 9 - 2x & \text{if } x \le 3, \\ ax + b & \text{if } x > 3. \end{array} \right.\]The function $f$ has the property that $f(f(x)) = x$ for all $x.$ What is $a +

  2. Math

    Tony broke a larger array into 2 x 9 array and a 4 x 9 array. What did the larger array look like? Draw picture. Write a number sentence to show the relationship between the large array and the two smaller arrays.

  3. Computers C++

    1). The name of an array stores the __________ of the first array element. 1) memory address 2) value 3) element number 4) subscript 5) None of these

  4. math

    don break a 4x7 array into a 2x7 array and another array. what is the fact for dons second array? write a number sentence that models the relationship of the 4x7 array to the other two arrays.

  1. Computer Science

    Write a class “Program1” that has the method “allLess” (below) that receives two arrays of integers and returns true if each element in the first array is less than the element at the same index in the second array. For

  2. Programming

    Here is the source code for the Kitty class: public class Kitty { String name; int age; public Kitty(){ } public void setName(String n){ name = n; } public String getName(){ return name; } public void setAge(int a){ age = a; }

  3. Math

    Draw an array for 12x7 (You don;t have to draw all the boxes inside, just the shape and the dimensions.) How would you break up this array into two smaller arrays? Show your two smaller arrays an your drawing. Then fill in the

  4. Computer Science

    Can anyone please write a basic c++ code for the following instructions? Congratulations! You’ve been hired by the Fly-By-Night-Engineering-Company, Your new supervisor asks you to write a C++ program that will manage a list of

  1. COP 2000-6

    show an example of an user inputs a list of positive numbers terminated by 0 into an array, find the mean of the numbers in the array, and outputs th result. use a subprogram to input the numbers, a function to find the mean, and

  2. Math

    Don breaks a4 by 7array into a 2 by 7 array and another array. What is the fact for dons second array? Write a.number sentence that models the relationship of the 4 by 7 array to the other two arrays

  3. elementary

    Draw an array. Then write a fact family to describe your array

  4. programming

    Write a program which allows user to enter 10 student names and scores for Java programming 1 and Java programming 2. Allow user to choose which course they want to enter the record for. Use the loop to ask input from user. The

You can view more similar questions or ask a new question.