Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 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?
Reply With Quote
  #2  
Old 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
Reply With Quote
  #3  
Old 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
Reply With Quote
  #4  
Old 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!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Access violation or not"
Thread Thread Starter Forum Replies Last Post
Skype 4.2.0.169 access violation Shin-Chen Technology & Internet 6 12-08-2010 11:03 PM
PHP has encountered an Access Violation at 01B9AD16 cornto Software Development 4 02-01-2010 12:49 PM
Access violation ... in module ntdll.dll James O''''Reilly Vista Help 4 15-07-2009 09:54 PM
0xC0000005 Access Violation Error Farley Operating Systems 5 14-03-2009 09:21 AM
Access violation at address 0000702b Aliya Operating Systems 5 05-01-2009 10:25 AM


All times are GMT +5.5. The time now is 04:05 PM.