Chapter - 9: Arrays

Write a program that interchanges the odd and even elements of an array.


D
Sections
5
Exercises
#include<stdio.h>
#include<conio.h>

void swap(int*, int*);

int main()
{
	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, i;

	for (i = 0; i < 10 - 1; i += 2)
		swap(&a[i], &a[i + 1]);

	for (i = 0; i < 10; i++)
		printf("%d\t", a[i]);

	_getch();
	return 0;
}

void swap(int* a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

© 2021 Garbage Valuegarbage value logo