Easy Tutorial
❮ C Passing Arrays To Functions C Function Atol ❯

C Exercise Example 40

C Language Classic 100 Examples

Title: Reverse the output of an array.

Program Analysis: Swap the first element with the last one.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include<stdio.h>
#define N 10
int main()
{
    int a[N]={0,1,2,3,4,5,6,7,8,9};
    int i,t;
    printf("Original array is:\n");
    for(i=0;i&lt;N;i++)
        printf("%d ",a[i]);
    for(i=0;i&lt;N/2;i++)
    {
        t=a[i];
        a[i]=a[N-1-i];
        a[N-1-i]=t;
    }
    printf("\nSorted array:\n");
    for(i=0;i&lt;N;i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0;
}

The above example output is:

Original array is:
0 1 2 3 4 5 6 7 8 9 
Sorted array:
9 8 7 6 5 4 3 2 1 0

C Language Classic 100 Examples

❮ C Passing Arrays To Functions C Function Atol ❯