Count the number of leading zeros and reduce the array to the number of ints that need reversing. Modifying Preludes code -
Code:
#include <stdio.h>
void rev_array ( int *a, int n )
{
int i, j, k;
k=0;
/*count number of leading zeros*/
for(i=0;i<n;i++)
{
if(a[i]==0)
k++;
else
break;
}
/*limit array accordingling*/
a+=k;
n-=k;
for ( i = 0, j = n - 1; i < j; i++, j-- )
{
int hold = a[i];
a[i] = a[j];
a[j] = hold;
}
}
void print_array(int* array, int N)
{
int i=0;
while(array[i]==0)
i++;
for(;i<N;i++)
{
printf("%d",array[i]);
}
}
int main()
{
int array[10]={0,0,0,5,0,0,0,3,2,1};
rev_array(array,10);
print_array(array,10);
return 0;
}