TechArena Community Access violation or not

#1
21-10-2009
 Member Join Date: Jun 2009 Posts: 105
Access violation or not

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?
#2
21-10-2009
 Member Join Date: May 2008 Posts: 685
Re: Access violation or not

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;
}```
base, counter => 14 items, on i [2, 16] => base[i-2][counter [i-2] => explosion
#3
21-10-2009
 Member Join Date: May 2008 Posts: 2,383
Re: Access violation or not

You iterates in line 9 from 2 to 16. Then you'll tap in counter [i - 2]. Counter is an array of 14. So when you pass the last time you tap in meter [14], the 15th box.
__________________
The FIFA Manager 2009 PC Game
#4
21-10-2009
 Member Join Date: Jun 2009 Posts: 105
Re: Access violation or not

Ah yes, 16-2+1 that is 15, then base [15] [20] and counter [15]!

Thank you very much to two of you!

 Tags: