# Thread: reverse order - arrays

1. ## reverse order - arrays

I'm now learning arrays and I'm stuck trying to print an array in reverse order. Here's the working part of my code. I've deleted the lead up (printf and scanf statements).

Code:
```#include<stdio.h>
#include<conio.h>

int divide (int, int);		/*function prototype*/

int remainder (int, int);  /*function prototype*/

int reverse (int []);  /*function prototype*/

int main ()
{
int value, base, list[20], count = 0;

printf("\n** This program facilitates the conversion of a               decimal   number");
printf("\n   to bases 2 through 9 **\n");

while(count<20 && value!=0)

{

list[count] = remainder (value, base);

value = divide (value,base);

printf("%d",list[count]);

++count;

}

printf(" to base %d is the result.\n",base);

getch();

return 0;

}

/*This function returns the whole number*/

int divide (int a, int b )
{
int result;

result = a/b;

return result;
}

/*This function returns the remainder*/

int remainder (int x, int y)
{
int remain;

remain = x%y;

return remain;
}

/* This function reverses the order of the remainders in the array */

int reverse (int temp[])
{

return 0;

}```

2. Code:
```void rev_array ( int *a, int n )
{
int i, j;

for ( i = 0, j = n - 1; i < j; i++, j-- ) {
int hold = a[i];
a[i] = a[j];
a[j] = hold;
}
}```
-Prelude

3. ## how do i eliminate the leading zeros?

Thanks for your last response. My new question is in the subject.

4. 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;
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;
}```

5. I dont know where you have the leading zeros, but before you print, you can check the value, and if it is a zero, dont print it.

6. ## Could you simplify it?

some of the concepts in Prelude's reply we haven't dealt with yet in class, so I don't quite grab them. (thanks anyway)

However, the simple version from Salem, that works for me! could you incorporate your response into that code instead?

The array is for 20 elements. I have an answer of only three elements, so I have 17 zeros and then the 3 digits.

Thanks.

7. When I run the code, I get 123005. What leading zeros are you referring to?

Code:
```void print_array(int* array, int N)
{
int i=0;

while(array[i]==0)
i++;

for(;i<N;i++)
{
if(array[i] != 0)  // To stop the printing of 0s
printf("%d",array[i]);
}
}```
This took out the zeros when I ran the code you provided.

8. ## those zeros

I'm testing with 45 converting to base 6. Remainders are 311.

Reverse order is 113, but I'm getting 00000000000000000113. These are the zeros I'm trying to eliminate.

Please note that I am running my original code with the inclusion of Salem's reverse order code.

9. ## Re: those zeros

Where do you initialise variables value and base before using them? I can't see that happening.

10. ## complete code

Code:
```#include<stdio.h>
#include<conio.h>

int divide (int, int);		/*function prototype*/

int remainder (int, int);  /*function prototype*/

int reverse (int []);      /*function prototype*/

int main ()
{
int value, base,i, list[20], count = 0;

printf("\n** This program facilitates the conversion of a decimal number");
printf("\n   to bases 2 through 9 **\n");
printf("\nEnter a decimal value to be converted\t:");
scanf("%d",&value);
printf("\nEnter the (base) to be converted to :\t");
scanf("%d",&base);
printf("\n");

for (i=0;i<20;i++)  /*initializing the array */
list[i] =0;

while(count<20 && value!=0)

{

list[count] = remainder (value, base);

value = divide (value,base);

printf("%d",list[count]);

++count;

}

printf(" is the original order of the remainders.\n\n");

reverse(list);
printf(" to base %d is the result.\n",base);
/* I can't get rid of these leading zeros. */

getch();

return 0;
}

/*This function returns the whole number*/

int divide (int a, int b )
{
int result;

result = a/b;

return result;
}

/*This function returns the remainder*/

int remainder (int x, int y)
{
int remain;

remain = x%y;

return remain;
}

/* This function reverses the order of the remainders in the array */

int reverse (int temp[])
{
int i;

for ( i = 19 ; i >= 0 ; i-- )

printf( "%d", temp[i] );

return 0;

}```

11. If you just need to print the array backwards rather than actually reversing it (as per Salems code) then -

Code:
```int main()
{
int i,j;
int array[10]={0,0,0,5,0,0,0,3,1,1};
for ( i = 9 ; i >= 0 ; i-- )
{

if(array[i]==0)
{
j=i;
while(j>=0)
{
if(array[j]!=0)
break;
else
j--;
}
if(j<0)
continue;

}

printf( "%d ", array[i] );
}

return 0;
}```

12. Code:
```#include <stdio.h>

void PrintRev(int arr[], int len)
{
int *s = arr;
int *e = arr + len - 1;

while (*e == 0 && e >= s) e--;

while (e >= s)
printf("%d ", *e--);
}

int main()
{
int arr1[10]={1,2,3,4,0,0,0,0,0,0};

PrintRev(arr1, sizeof arr1 / sizeof arr1[0]);
putchar('\n');
return 0;
}```

13. ## thanks guys

everyone's been great!!

Have a great week.