Reverse Array/Array Copy

code:

#include<stdio.h>
int main()
{
    int ara[]={10,20,30,40,50,60,70,80,90,100}; //took this array for various limit
    int ara2[10]; //max value here will be 10

    int i,j;
    for(i=0,j=9;i<10;i++,j--) //here in for loop will i value increase array index and j value will decrease array index
    {
        ara2[j]=ara[i]; //copying the values of ara[] to ara2[]
    }
    for(i=0;i<10;i++)
    {
        ara[i]=ara2[i]; //now after copying the value of ara2[i] where index i to 10 has took all the reverse value of ara[] to ara[i] directly it is copying now to ara[i] index i will be followed
    }
    for(i=0;i<10;i++){
        printf("%d\n",ara[i]); //now printing from ara[i] value the reverse of ara[] finally
    }

    return 0;
}

Used Subeen vai’s book for the example

another:

#include<stdio.h>
int main(){
int i,j,a1[]={10,20,40,60,70};
int a2[5];
for(i=0,j=4;i<5,j>=0;i++,j--){
    a2[j]=a1[i];
}
for(j=0;j<5;j++){
    printf("%d\t",a2[j]);
}
return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.