Question

9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

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


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #4

#include


#include


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];

    

    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;

    

}


answered by: Happy.cat
Add a comment
Answer #3
[REMOVED]
Add a comment
Know the answer?
Add Answer to:
9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.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...

  • Write a program to reverse an integer number by using a function called reverse. The program...

    Write a program to reverse an integer number by using a function called reverse. The program reads in an integer number and prints its reverse order. Note that the function receives the number via a pointer, and uses the pointer to write the reverse number on the main function. The function MUST be used AS IS: void reverse(int *n) Language in C Bonus Problem: Number Reverser reverse c Write a program to reverse an integer number by using a function...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • in a program, write a function that accepts three arguments: an array, the size of the...

    in a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n.

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • 1. The following program calls the function countLarger that accepts three arguments: an integer array, an...

    1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...

  • write the program in c++ Pointen & A???ys: Write u function that is passed a pointer...

    write the program in c++ Pointen & A???ys: Write u function that is passed a pointer to a float array, and the size the array as an int. The function should return a pointer to the maximum element in the array The function should be: int *maxp(float* ap, int size) Write a main() program to lest the function with different input arrays. Grading Criteria (1 point each): Uses cin/cout correct function definition prompts user for input, reads it in correct...

  • Write a C++ program that does the following : 1. Accepts array size from the keyboard....

    Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT