computer science

posted by .

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

  • computer science -

    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;

    }

  • computer science -

    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

  • computer science -

    you're welcome! :)

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. programming

    I really need help in this it makes me confuse.I should make a program to find a pattern...so can somebody help me... Write a function to recognize an pattern within another array. Your function should have the following prototype. …
  2. college

    Write a C++ program to fill an array with 6 numbers, and then reverse the values of this array. Note: By using a function with two parameters with the name of the array and the size of this array.
  3. computer programming c++

    write a program that can read 10 interger values into an array and find the total for that array in the program , write 3 fiunctions: a function to read values , a function to find the total of element value and a function to print …
  4. Math

    Don breaks a4 by 7array into a 2 by 7 array and another array. What is the fact for dons second array?
  5. math

    don break a 4x7 array into a 2x7 array and another array. what is the fact for dons second array?
  6. Computer Science

    I need help with this question please! Please write me step by step instructions or how should I begin the problem. Thank you for your time ;) Write a function that takes an array and returns true if all the elements in the array are …
  7. computer science

    Write a method printBackwards that prints an array of integers in reverse order, in the following format. For the array {1, -3, 4, 7, 2}: element [4] is 2 element [3] is 7 element [2] is 4 element [1] is -3 element [0] is 1
  8. 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 example, …
  9. programming

    A. Write a program which reads an array of up to 200 integers from input and then does all the following in order 1. searches the array for a specific integer given by the user and outputs its position in the array; 2. outputs the …
  10. Math

    Tony broke a larger array into 2 x 9 array and a 4 x 9 array. What did the larger array look like?

More Similar Questions