i wrote this program to sort and convert decimal integers into binary form.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_ARRAY 5
int fillarray(void);
int main()
{
int char_array[MAX_ARRAY];
char binary_array[MAX_ARRAY];
int lastchar, stop;
int lastcharprint;
int compare1, compare2;
int binary, binaryvalue, binarycount, binarycount2;
lastchar = 0;
stop = 0;
lastcharprint = 0;
compare1 = 0;
/* call function */
while (stop != 1) {
char_array[lastchar] = fillarray();
/* check for newline */
if (char_array[lastchar] == '\n' && char_array[lastchar] != 10) {
stop = 1;
}
else {
lastchar = lastchar + 1;
}
/* check for full array */
if (lastchar == MAX_ARRAY) {
stop = 1;
lastchar = lastchar - 1;
}
}
/* sort values */
while (compare1 <= lastchar) {
compare2 = compare1 + 1;
while (compare2 <= lastchar) {
if (char_array[compare2] < char_array[compare1]) {
int temp;
temp = char_array[compare1];
char_array[compare1] = char_array[compare2];
char_array[compare2] = temp;
}
compare2 = compare2 + 1;
}
compare1 = compare1 + 1;
}
/* end of sort function */
/* calculate binary */
int bintemp[32];
binarycount = 0;
binarycount2 = 1;
while (binarycount <= lastchar) {
binary = char_array[binarycount];
binaryvalue = 0;
while (binaryvalue < 32 && binary > 0) {
bintemp[binaryvalue] = binary % 2;
binary = binary / 2;
binaryvalue = binaryvalue + 1;
}
binaryvalue = binaryvalue - 1;
while (binaryvalue >= 0) {
binary_array[binarycount] = binary_array[binarycount] + (bintemp[binaryvalue] * pow(10, binaryvalue));
binaryvalue = binaryvalue - 1;
}
binarycount = binarycount + 1;
}
while (lastcharprint <= lastchar && char_array[lastcharprint] != '\n' || char_array[lastcharprint] == 10) {
printf("\n%d (decimal), %d (binary)", char_array[lastcharprint], binary_array[lastcharprint]);
lastcharprint = lastcharprint + 1;
}
printf("\n\nEnd of program\n");
return 0;
}
int fillarray()
{
int c, value;
value = 0;
c = getchar();
while (c != '\n') {
value = 10 * value + c - '0';
c = getchar();
}
return (value);
}

this works perfectly for the sorting part, but when i input any number greater than 5 it will return some random value.

hope you can help