Answer to Question #259632 in C++ for jye

Question #259632

Defines a function that saves elements of a real number array in the opposite direction, declaring the function as void Reverse (double arr[], const int size, double*& rev). Write the main program using the upper function.



1
Expert's answer
2021-11-01T05:32:42-0400


SOLUTION FOR THE ABOVE QUESTION


SOLUTION CODE


#include <iostream>
using namespace std;


//define a function called reverse returning void to reverse the elements
void reverse(double arr[], const int size, double*& rev)
 {
    rev = new double[size];
    for (int i=0; i<size; i++)
    {
       rev[size-1-i] = arr[i];
    }
}


//definition of the main function
int main() {
	//declare the size of the array as a constant and initialize it to some value
	//we will use an array of size 8
	//the array declaration
    const int array_size = 8;
    //array initialization
    double my_arr[array_size] = {4.5, 5.5, 6.5, 7.5, 8.5 ,9.5, 10.5, 11.5};
    double* rev;
    //print the original array 
    cout<<"The orinal array is: "<<endl;
    for(int j = 0; j< array_size; j++)
    {
    	cout << my_arr[j] << " ";
	}
   
   //Now call the reverse function to reverse the elements of the array
    reverse(my_arr, array_size, rev);


    cout << "\n\nThe Reversed array is: "<<endl;
    for (int i=0; i<array_size; i++) 
    {
     cout << rev[i] << " ";
    }
    cout << endl;
    return 0;
}



SAMPLE PROGRAM OUTPUT






Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS