In: Computer Science
Write a program in which, you define and call each of the following functions, note that YOU CANNOT USE ARRAY NOTATION IN ANY FUNCTION AT ALL IN THIS EXERCISE, ONLY USE POINTER NOTATION TO MANIPULATE ARRAYS IN THE FUNCTIONS:
1. function read_array, that takes an array of 6 doubles and prompts the user to fill it through the keyboard.
2. function reverse_array, that takes an array of 6 doubles and reverses its contents.
3. function swap_arrays, that takes two arrays of type double and swaps their contents.
4. function print_array, that prints an array of 6 doubles.
5. call all these functions properly in your main program.
#include <iostream>
#define SIZE 6
using namespace std;
void read_array (double *);
void print_array (double *);
void reverse_array (double *);
void swap_array (double *, double *);
int
main ()
{
double a[SIZE], b[SIZE];
cout << "Enter first array :";
read_array (a);
cout << "First array is :";
print_array (a);
reverse_array (a);
cout << "\nFirst array after calling reverse_array() :";
print_array (a);
cout << "\nEnter second array :";
read_array (b);
cout << "Second array is :";
print_array (b);
cout << "\nBefore calling swap_array(),";
cout << "\nFirst array is :";
print_array (a);
cout << "\nSecond array is :";
print_array (b);
swap_array (a, b);
cout << "\nAfter calling swap_array(),";
cout << "\nFirst array is :";
print_array (a);
cout << "\nSecond array is :";
print_array (b);
return 0;
}
void
read_array (double *a)
{
for (int i = 0; i < SIZE; i++)
cin >> *(a + i);
}
void
print_array (double *a)
{
for (int i = 0; i < SIZE; i++)
cout << *(a + i) << ", ";
}
void
reverse_array (double *a)
{
for (int i = 0; i < SIZE / 2; i++)
{
double temp = *(a + i);
*(a + i) = *(a + SIZE - 1 - i);
*(a + SIZE - i - 1) = temp;
}
}
void
swap_array (double *a, double *b)
{
for (int i = 0; i < SIZE; i++)
{
double temp = *(a + i);
*(a + i) = *(b + i);
*(b + i) = temp;
}
}