answersLogoWhite

0


Best Answer

This is easier to achieve using C++ than C, because we can make use a single template function that can cater for arrays of any type in a type-safe manner. In C, we must use macro trickery to cater for different array types, but macros are not type-safe. However, if we limit ourselves to arrays of integers, we can provide a concrete implementation:

void swap_oddeven (int* p, size_t size) {

if (size<2) return;

for (size_t index=1; index<size; index+=2) swap (p[index], p[index-1]);

}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

Use the following function:

void exchange_odd_even (int* arr, size_t len) {

for (int i=1; i<len; i=i+2) swap (arr[i-1], arr[i]);

}

Note that we traverse the array from index 1 rather 0, incrementing in steps of 2. This is because index 1 is an even element (the 2nd element) and for every even element in the array we can guarantee there has to be a preceding odd element to swap with. Doing it this way means we don't need to cater for the special case where 'len' is odd and the final odd element has no even element to swap with.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C program that interchanges the odd and even elements of an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How are arrays processed?

Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.


Write a program to sort elements of an array in ascending order?

import java.util.Arrays; public class arraysort { public static void main(String[] a) { int array[] = { 2, 5, -2, 6, -3 }; Arrays.sort(array); for (int i : array) { System.out.println(i); } } }


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How do you write a program that accepts 50 integer values and sort them in basic programming?

Create an array with 50 elements and input the integers one a time, filling the array. Use an insertion sort on the array for each input except the first. Alternatively, input the values first and then use insertion sort.


Write a program to add array of N elements?

int sumArray(int N, int* A) { int sum = 0; do sum += A[N-1] while (N-- &gt; 1); return sum; }


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


How do you write a program in C to find and display the sum of each row and column of a 2 dimensional array of type float?

array type


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }


Write a c program to find the maximum of two numbers and print out with its position?

maxValue = function (array) {mxm = array[0];for (i=0; i&lt;array.length; i++) {if (array[i]&gt;mxm) {mxm = array[i];}}return mxm;}; i don't know


Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i &lt; array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }