Friday, October 30, 2009

Chap8[I]a Reversing contents and copy to another array

Write a program to copy the contents of one array into another in the reverse order.

void main()
{
int arr1[5]={1,2,3,4,5};
int arr2[5];
int i,k;

for (i=4,k=0;i>=0;i--,k++)
arr2[k]=arr1[i];

for(i=0;i<5;i++)
printf("\nValue of arr2[%d] is %d",i,arr2[i]);

}


With Pointers


void main()
{
int arr1[5]={1,2,3,4,5};
int arr2[5];
int *m;
int i,*j,k,l=5;
j=arr1;
m=arr2;
func(j,m,l);
for(i=0;i<5;i++)
printf("\nValue of arr2[%d] is %d",i,arr2[i]);
}

func(int *j, int *m, int l)
{
int i;
for(j=j+4,i=0;i<5;i++,j--,m++)
*m=*j;
}

No comments:

Post a Comment