Rewrite the following function using pointers Rewrite the fo

Rewrite the following function using pointers:

Rewrite the following function using pointers: void foo (int x[], int n) {int i, j, t; for (i = 0, j = n-1; i

Solution

#include <iostream>
#include<string.h>
using namespace std;

void fooWithPointer(int x[],int n)
{
    int i,j,t;
    for(i=0,j=n-1;i<j;i++,j--)
    {
        t = *(x+i);
        *(x+i) = *(x+j);
        *(x+j) = t;
    }
}

void foo(int x[],int n)
{
    int i,j,t;
    for(i=0,j=n-1;i<j;i++,j--)
    {
        t = x[i];
        x[i] = x[j];
        x[j] = t;
    }
}

void printArray(int arr[],int size)
{
    int i;
    for(i=0;i<size;i++)
        cout << arr[i] << \" \";
    cout << endl;
}

int main ()  
{
    int arr[] = {1,2,3,4,5};
    fooWithPointer(arr,5);
    printArray(arr,5);
    return 0;  
}

Rewrite the following function using pointers: Rewrite the following function using pointers: void foo (int x[], int n) {int i, j, t; for (i = 0, j = n-1; iSolu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site