Question

Write a function int[] reverseTwo(int[] array). Use a for-loops.The input for this function is an int...

Write a function int[] reverseTwo(int[] array). Use a for-loops.The input for this function is an int array. Your job is to reverse every adjacent element pairs in this array. The reversed array shall be returned. If the length of the array is odd, then the last element does not need to be reversed. Sample input/output: Input [1, 2, 3, 4] Output [2, 1, 4, 3]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* C program to revesre array elements pair wise */

#include<stdio.h>

#include<stdlib.h>

// This function reverse array elements in pair wise

// Your function prototype was wrong i.e using return type '[]' got error.

// for array as return type we have to use pointer i.e *

// and we always want size when we pass array to the function because it's heavy operation to find size in function

int * reverseTwo(int arr[], int n){

    // Calculate size of array

    int i, temp, nValue;                // Declare variables

    // this checks for even and odd condition

    if(n % 2 == 0)

        nValue = n;

    else

        nValue = n-1;

    // Traverse array elemnts upto the last with 2 elemnts at a time

    for(i = 0; i < nValue; i+=2){

        // swap to elementa

        temp = arr[i];

        arr[i] = arr[i+1];

        arr[i+1] = temp;

    }

    // return array to calling function

    return arr;

}

// main driver method

int main(){

    // Declaration of array, size i.e n and i variables

    int arr[100], *ptr, i, n;   

    

    // Promote message for user and get input

    printf("Enter Value of n: ");

    scanf("%d",&n);

    // Get array elements from user

    printf("\nEnter array elements:\n");  

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

        printf("element - %d : ", i);

        scanf("%d", &arr[i]);  

    }  

    

    // Display array on the console

    printf("\nArray before function call: ");

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

        printf("%d  ", arr[i]);  

    }   

    // Call above function to check our functionality

    ptr = reverseTwo(arr, n);

    // Display array on the console

    printf("\nArray After function call: ");

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

        printf("%d  ", *(ptr + i));  

    }

    printf("\n");

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a function int[] reverseTwo(int[] array). Use a for-loops.The input for this function is an int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT