Board index » delphi » binary convertion

binary convertion

Quote
> i try to create a program that can convert number from decimal to binary,
> but i try to run the program delphi report that
> illegal operation....can you help me?
> here is my code...
> program binary;
> var  i, q : integer;
>      B : array [1..4] of real;
> begin
>   i := 0; q := 0;
>   write('enter number => ');  readln(i);
>   write('enter number => ');  readln(q);
>   repeat
>     B[i] := q mod 2 ;
>     q := q div 2 ;
>     i := i + 1 ;
>   until i = 0 ;
>   write(q,i)
> end.

   Is this really your code?  How about telling us what error you
received?
   In any case, I see 3 glaring problems.  Whether they solve the problem
or not, I don't know...
  1. In the Repeat loop, you are indexing (subscripting) the array B by
whatever value is read in by the earlier prompt.  If the value that's
typed in is anything other than 1..4, the program will abort with an "out
of bounds" error - because B is an array whose dimension subscripts are
1..4.
  2. Also in the Repeat loop you not only use whatever I has bee given in
the prompt, but you then _increment_ it in the loop!  Why?  Don't you see
that you're using I as both a value to be converted _and_ as a loop
terminator/control?  BIG problem there...
  3. Lastly, if you're always _incrementing_ I, how can the loop ever
terminate with a condition where I = 0????  Unless you entered a negative
value where I was obtained, it will never happen!
   BTW, you don't need to initialize I and Q (to 0) if you're prompting
for and getting user values for them.
 

Re:binary convertion


thats the shittiest code i've ever seen :)

Quote
Mike Copeland <mrc...@primenet.com> wrote in message

news:MPG.133a09506f946be8989d7a@news.primenet.com...
Quote
> > i try to create a program that can convert number from decimal to
binary,
> > but i try to run the program delphi report that
> > illegal operation....can you help me?
> > here is my code...
> > program binary;
> > var  i, q : integer;
> >      B : array [1..4] of real;
> > begin
> >   i := 0; q := 0;
> >   write('enter number => ');  readln(i);
> >   write('enter number => ');  readln(q);
> >   repeat
> >     B[i] := q mod 2 ;
> >     q := q div 2 ;
> >     i := i + 1 ;
> >   until i = 0 ;
> >   write(q,i)
> > end.

>    Is this really your code?  How about telling us what error you
> received?
>    In any case, I see 3 glaring problems.  Whether they solve the problem
> or not, I don't know...
>   1. In the Repeat loop, you are indexing (subscripting) the array B by
> whatever value is read in by the earlier prompt.  If the value that's
> typed in is anything other than 1..4, the program will abort with an "out
> of bounds" error - because B is an array whose dimension subscripts are
> 1..4.
>   2. Also in the Repeat loop you not only use whatever I has bee given in
> the prompt, but you then _increment_ it in the loop!  Why?  Don't you see
> that you're using I as both a value to be converted _and_ as a loop
> terminator/control?  BIG problem there...
>   3. Lastly, if you're always _incrementing_ I, how can the loop ever
> terminate with a condition where I = 0????  Unless you entered a negative
> value where I was obtained, it will never happen!
>    BTW, you don't need to initialize I and Q (to 0) if you're prompting
> for and getting user values for them.

Re:binary convertion


Of course the repeat until loop would terminate, i is an integer and it
would eventually reach 0 (try it) but the rest of the code is crud, or we
dont have all the facts.

Kenn

Quote
Mike Copeland wrote in message ...
>> i try to create a program that can convert number from decimal to binary,
>> but i try to run the program delphi report that
>> illegal operation....can you help me?
>> here is my code...
>> program binary;
>> var  i, q : integer;
>>      B : array [1..4] of real;
>> begin
>>   i := 0; q := 0;
>>   write('enter number => ');  readln(i);
>>   write('enter number => ');  readln(q);
>>   repeat
>>     B[i] := q mod 2 ;
>>     q := q div 2 ;
>>     i := i + 1 ;
>>   until i = 0 ;
>>   write(q,i)
>> end.

>   Is this really your code?  How about telling us what error you
>received?
>   In any case, I see 3 glaring problems.  Whether they solve the problem
>or not, I don't know...
>  1. In the Repeat loop, you are indexing (subscripting) the array B by
>whatever value is read in by the earlier prompt.  If the value that's
>typed in is anything other than 1..4, the program will abort with an "out
>of bounds" error - because B is an array whose dimension subscripts are
>1..4.
>  2. Also in the Repeat loop you not only use whatever I has bee given in
>the prompt, but you then _increment_ it in the loop!  Why?  Don't you see
>that you're using I as both a value to be converted _and_ as a loop
>terminator/control?  BIG problem there...
>  3. Lastly, if you're always _incrementing_ I, how can the loop ever
>terminate with a condition where I = 0????  Unless you entered a negative
>value where I was obtained, it will never happen!
>   BTW, you don't need to initialize I and Q (to 0) if you're prompting
>for and getting user values for them.

Re:binary convertion


In article <sd258dgg9n...@corp.supernews.com>, bob...@structurex.net
says...

Quote
> thats the shittiest code i've ever seen :)

   And how does that _help_ him?  (If you had looked closely, you could
have seen the code isn't mine...).  I replied to a poster here with some
things I thought useful, but you, sir, did _nothing_ to help him or the
newsgroup.
   I suggest you take your (useless) comments of this nature somewhere
else, since most of us are here to help...
Quote
> Mike Copeland <mrc...@primenet.com> wrote in message
> news:MPG.133a09506f946be8989d7a@news.primenet.com...
> > > i try to create a program that can convert number from decimal to
> binary,
> > > but i try to run the program delphi report that
> > > illegal operation....can you help me?
> > > here is my code...
> > > program binary;
> > > var  i, q : integer;
> > >      B : array [1..4] of real;
> > > begin
> > >   i := 0; q := 0;
> > >   write('enter number => ');  readln(i);
> > >   write('enter number => ');  readln(q);
> > >   repeat
> > >     B[i] := q mod 2 ;
> > >     q := q div 2 ;
> > >     i := i + 1 ;
> > >   until i = 0 ;
> > >   write(q,i)
> > > end.

> >    Is this really your code?  How about telling us what error you
> > received?
> >    In any case, I see 3 glaring problems.  Whether they solve the problem
> > or not, I don't know...
> >   1. In the Repeat loop, you are indexing (subscripting) the array B by
> > whatever value is read in by the earlier prompt.  If the value that's
> > typed in is anything other than 1..4, the program will abort with an "out
> > of bounds" error - because B is an array whose dimension subscripts are
> > 1..4.
> >   2. Also in the Repeat loop you not only use whatever I has bee given in
> > the prompt, but you then _increment_ it in the loop!  Why?  Don't you see
> > that you're using I as both a value to be converted _and_ as a loop
> > terminator/control?  BIG problem there...
> >   3. Lastly, if you're always _incrementing_ I, how can the loop ever
> > terminate with a condition where I = 0????  Unless you entered a negative
> > value where I was obtained, it will never happen!
> >    BTW, you don't need to initialize I and Q (to 0) if you're prompting
> > for and getting user values for them.

Re:binary convertion


Quote
> Of course the repeat until loop would terminate, i is an integer and it
> would eventually reach 0 (try it)

   It won't terminate in a reasonable number of iterations, though.  (No,
I won't try it...)  That's not the way to design a loop's termination...
Quote
> but the rest of the code is crud, or we dont have all the facts.
> >> i try to create a program that can convert number from decimal to binary,
> >> but i try to run the program delphi report that
> >> illegal operation....can you help me?
> >> here is my code...
> >> program binary;
> >> var  i, q : integer;
> >>      B : array [1..4] of real;
> >> begin
> >>   i := 0; q := 0;
> >>   write('enter number => ');  readln(i);
> >>   write('enter number => ');  readln(q);
> >>   repeat
> >>     B[i] := q mod 2 ;
> >>     q := q div 2 ;
> >>     i := i + 1 ;
> >>   until i = 0 ;
> >>   write(q,i)
> >> end.

> >   Is this really your code?  How about telling us what error you
> >received?
> >   In any case, I see 3 glaring problems.  Whether they solve the problem
> >or not, I don't know...
> >  1. In the Repeat loop, you are indexing (subscripting) the array B by
> >whatever value is read in by the earlier prompt.  If the value that's
> >typed in is anything other than 1..4, the program will abort with an "out
> >of bounds" error - because B is an array whose dimension subscripts are
> >1..4.
> >  2. Also in the Repeat loop you not only use whatever I has bee given in
> >the prompt, but you then _increment_ it in the loop!  Why?  Don't you see
> >that you're using I as both a value to be converted _and_ as a loop
> >terminator/control?  BIG problem there...
> >  3. Lastly, if you're always _incrementing_ I, how can the loop ever
> >terminate with a condition where I = 0????  Unless you entered a negative
> >value where I was obtained, it will never happen!
> >   BTW, you don't need to initialize I and Q (to 0) if you're prompting
> >for and getting user values for them.

Re:binary convertion


In comp.lang.pascal.borland, Mike Copeland <mrc...@primenet.com> wrote:

Quote
>> program binary;
>> var  i, q : integer;
>>      B : array [1..4] of real;
>> begin
>>   i := 0; q := 0;
>>   write('enter number => ');  readln(i);
>>   write('enter number => ');  readln(q);
>>   repeat
>>     B[i] := q mod 2 ;
>>     q := q div 2 ;
>>     i := i + 1 ;
>>   until i = 0 ;
>>   write(q,i)
>> end.
>   1. In the Repeat loop, you are indexing (subscripting) the array B by
> whatever value is read in by the earlier prompt.  If the value that's
> typed in is anything other than 1..4, the program will abort with an "out
> of bounds" error - because B is an array whose dimension subscripts are
> 1..4.

Correct. The line that reads in a value of i should be deleted.

Quote
>   2. Also in the Repeat loop you not only use whatever I has bee given in
> the prompt, but you then _increment_ it in the loop!  Why?  Don't you see
> that you're using I as both a value to be converted _and_ as a loop
> terminator/control?  BIG problem there...

Actually, it is q that is being converted. The loop index is i.
There are also some applications in which it is useful to use your input
value as an index, although not necessarily to increment it (though even
this may have a use, as long as you are careful about it and know what you
are doing - for example, you may have a general indexing parameter which is
passed in to give you a start position, and has an updated position passed
out).

Quote
>   3. Lastly, if you're always _incrementing_ I, how can the loop ever
> terminate with a condition where I = 0????  Unless you entered a negative
> value where I was obtained, it will never happen!

It is q that should be tested here, not i. That way the loop will terminate
when there are no more bits left to be set.

An additional error is that the values of B are not initialised. Thus B will
not necessarily contain the correct result, even when the above errors are
corrected. Input checking also needs to be added to prevent the array
subscript going out of range.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Re:binary convertion


In comp.lang.pascal.borland, Kenn <Ken...@{*word*269}.net> wrote:

Quote
> Of course the repeat until loop would terminate, i is an integer and it
> would eventually reach 0 (try it) but the rest of the code is crud, or we
> dont have all the facts.

Actually you would get a "subscript out of range" error first. Assuming your
compiler/IDE has such checks built in. Otherwise you will get the standard
unpredictable effects.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Other Threads