problem with code for converting decimal to other bases

when i give any input, a window appears saying "a problem has occured, windows needs to close"

Cprob1

i cant understand what im doing wrong here.
any help will highly appreciated :)

this is my code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int ip_num,ip_base,op_num,op_base,i=0, acc[10];

char base_digits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

printf("enter the input number:\n");
scanf("%d",& ip_num);

printf("choose the base to convert to:");
scanf("%d",& op_base);


for(i=0;i<9;i++)
{
if(ip_num==0)
break;
acc[i]=(ip_num % op_base);
ip_num=ip_num/op_base;
}

for(i;i>=0;i--)
{
printf("%c",base_digits[acc[i]]);
}
}

Edited 4 Years Ago by somjit{}

Your second for() loop is causing the crash because the first for() loop leaves i at a an index with a junk value since it increments then checks to see if ip_num is 0.

There are two quick ways to fix this:

1) You can assume that ip_num is never zero when entering the loop on the first run and move the break condition to the bottom of the loop.
2) You can start the second for() loop with i = i-1 (or i--).

Either way works and I'm sure there are many other ways to go about solving this problem but I didn't want to modifiy what you had too much.

sfuo

thanks a lot :)
i changed the for() loop like u said and everythings working as it should :)
new for loop:

for(i=0;i<9 && ip_num!=0;i++)
{

acc[i]=(ip_num % op_base);
ip_num=ip_num/op_base;
printf("index: %d \n",i);

if(ip_num==0)
break;
}

however, when i wrote the 1st (faulty) code, i assumed it would work in the following manner when ip_num becomes 0 :

1) for() loop starts, i(lets suppose it was 3 in the prev iteration) gets incremented (thus i=4 now)
2) if() checks ip_num, and ip_num is 0; and hence break executed
3) exit from loop, with i=4
4) this i=4 goes into next for() loop.

however this led to the crash while running the code, and as u said

the first for() loop leaves i at a an index with a junk value since it increments then checks to see if ip_num is 0.

i dont understand why does i get a junk value instead of 4(assumed above)?

would be great if u could help me out again :)

thanks once again
somjit.

Edited 4 Years Ago by somjit{}

Yeah sorry my wording may have been a bit misleading. The variable i does have a value of 4; however, acc[i] contains a junk value. So what I guess I should have said is that your array "acc" has a junk value at index i and anything past that (since it is not being set anywhere).

Now what I do not know is why it is crashing for trying to access the index of the array with that junk value. I tried accessing the array at a large index and it just spat out a junk number but when I typed in a value of ~100000 or so it crashed like before. I'm not sure if there is a "magic" number around there but I'm sure there is a hard limit or just undefined behaviour going on. Either way you don't every play out of bounds like that anyways because you could be overwriting other allocated blocks of memory or could just simply crash like this.

Yeah sorry my wording may have been a bit misleading. The variable i does have a value of 4; however, acc[i] contains a junk value. So what I guess I should have said is that your array "acc" has a junk value at index i and anything past that (since it is not being set anywhere).

and perhaps that junk value was creating problems when the code went to the line printf("%c",base_digits[acc[i]]); and base_digits[] was going out of bounds.

but i still am in doubt as to why that should happen, maybe something's going on underneath, i really cant understand. when break() occurs, i assumed that it would be a clean break(), but clearly it isnt turning out so, and acc[i] is catching a junk value..
just dont understand why it was behaving like that...
and its making my head a less happier place.. :/ :(

when break() occurs, i assumed that it would be a clean break()

It is.

but clearly it isnt turning out so, and acc[i] is catching a junk value.

When break occurs it leaves the loop just as it should. But acc[i] simply isn't initialized. Let's play it through for ip_num = "12" and op_base = 10:

In the first iteration i is 0, ip_num is 12 and acc[0] is set to 2.
In the second iteration i is 1, ip_num is 2 and acc[1] is set to 1.
In the third iteration i is 2 and the loop end because ip_num is 0. Nothing is written to acc[2].

So after the loop i is 2 and acc[i] is a junk value because nothing has been written to acc[2].

i modified the code a bit further, with the for() loop being

for(i=0;i<10 && ip_num!=0;i++)
{

acc[i]=(ip_num % op_base);
ip_num=ip_num/op_base;
printf("index: %d \n",i);

if(ip_num==0)
break;

if(i==9) {
puts("array acc[] full");
break;

}
}

now, if i put 20,000 decimal, and ask it convert to binary, ie ip_num=20000, op_base=2, then index i goes upto 9, and then the "array acc[] full" statement is printed, and all is ok. that is, the code terminates as is should, without any unexplained crashes or anything.
but without these 3 lines,

    if(i==9) {
    puts("array acc[] full");
    break;

the execution crashes again when i ask to convert 20000 dec to binary.
I only put up these lines after the previous experience with the crashes being cured by a if()-break() statement. i do get a feeling that junk values are taken up by either acc[] or the index i, but exactly which one, where and how that i dont understand yet.

any help on this would be awesome :)

thanks again
somjit.

Edited 4 Years Ago by somjit{}

If you let i get larger than 9, it will cause problems because acc is only large enough to hold 10 elements. That is unrelated to your earlier error where acc[i] contained a junk value for reasons that sfuo and I already explained.

@sepp2k

thats a really helpful explanation. cleared my views a lot :) and yes i now get why the crashes occur.

Edited 4 Years Ago by somjit{}

This article has been dead for over six months. Start a new discussion instead.