reverse an array of integers using recursion

#include <stdio.h>


void reverse(int a[], int start, int end) {

if(start < end) {
int tmp = a[start];
a[start] = a[end];
a[end] = tmp;

reverse(a, start + 1, end - 1);

}

}


int main() {

int a[] = {100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 1};

reverse(a, 0, 10);


int i;

for(i = 0; i < 11; ++i){
printf("%d ", a[i]);
}

printf("\n");
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s