Question

In: Computer Science

Write a function that accepts an int array and the array's size as arguments.

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 int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.

 

Solutions

Expert Solution

#include <iostream>

using namespace std;

int *reverse(int[], int);

void print(int[], int);

int main()

{int size=10, array[10]={1,2,3,4,5,6,7,8,9,10};

    cout<<"Original array\n";

    print(array,size);

    int *reversedArray=reverse(array, size);

    cout<<"Reversed array\n";

    print(reversedArray, size);

    system("pause");

    return 0;

}

int *reverse(int a[],int n)

{int i,j;

    if(n<=0)

        return NULL;

    int *copy = new int[n];

    for(i=0;i<n;i++)

        copy[i]=a[n-i-1];

    return copy;

}

void print(int a[],int n)

{int i;

    for(i=0;i<n;i++)

        cout<<a[i] << " ";

    cout << endl;

}

Related Solutions