Here's my program:
Description:
The program asks you to enter a number in decimal, and then it begins to divide that number by number basis. It begins to divide and we stop when the quotient is equal to 0, while setting the table in the result, and displaying so reversed, we get the number correctly. It has decremented the counter, because the last element of the array containing n elements is indexed (n-1).
Code:
#include <stdio.h>
int main()
{
int base[14][20],counter[14],rest=0,i,division,decimal;
for(i=0;i<=14;i++)
counter[i] = 0;
printf("Enter a number in decimal :\n");
scanf("%d",&decimal);
for(i=2;i<=16;i++)
{
division = decimal;
while(division != 0)
{
rest = division%i;
division = division/i;
base[i-2][counter[i-2]++] = rest;
}
counter[i-2]--; //last element of tab is indexed n-1
printf("\nBase %d : ",i);
while(counter[i-2] >= 0)
{
if (base[i-2][counter[i-2]] >= 10)
printf("%c",base[i-2][counter[i-2]--]+55); //10+55 = 65(A)
else
printf("%d",base[i-2][counter[i-2]--]);
}
}
printf("\n");
system("pause");
return 0;
}
All goes well, except for an access violation that comes out of nowhere, especially with relatively large numbers, like 10,000 will be a problem, although the conversion is the longest one in binary, and it does not exceed the limit of the table ie 20.
Can you tell me where is the problem, though I fumbled with the debugger in vain?
Bookmarks