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.
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
Comments
Leave a comment