comp.lang.ada
 help / color / mirror / Atom feed
From: "Phaedrus" <phaedrusalt@hotmail.com>
Subject: Re: Converting Binary to Decimal
Date: Mon, 26 Mar 2001 20:57:55 GMT
Date: 2001-03-26T20:57:55+00:00	[thread overview]
Message-ID: <n3Ov6.15480$ue1.1287176@newsread2.prod.itd.earthlink.net> (raw)
In-Reply-To: 99lofd$19j37@tech.port.ac.uk

Simplify, simplify!  If you're going to put together a piece of code
and you're a beginner in the language, try stepping into the problem.

First step, get the input.  Test to make sure you're getting it correctly.
Use meaningful names, please!  For instance, "a_bin_num" implies
that it's a variable, which it isn't.  It's a type, so try calling it
"a_bin_num_type",
that should help to eliminate some confusion.  Also, your "Binary_Number"
(This might be a good time to think about an uppercase/lowercase convention.)
isn't a binary number, it's a binary digit.  Be precise, it pays off when you
start writing longer pieces of code.

Second step, conversion.  If you've got good data in your array, converting
it to the output type should be a piece of cake.

Third (and final) step, output.  Ada makes this easy, but still it's useful
to keep the step alive.  For instance, if you had accidentally set the following
Ada.Integer_Text_Io.Default_Base := 2;
somewhere in your code, you'd realize that everything else was correct and
so the error HAD to be in the output format.  (And no, you can't just set the
Default_Base to 2, and use it to read the number for you.  Shame, isn't it?)

Following the old Input-Process-Output style will get you through a LOT of
programming courses.

Phaedrus

"WM" <wwminirl@hotmail.com> wrote in message
news:99lofd$19j37@tech.port.ac.uk...
> I am writing a program like this:
> -----------------------------------------------------------------------
> with Ada.Text_Io,Ada.Integer_Text_Io;
> use Ada.Text_Io,Ada.Integer_Text_Io;
>
> procedure Bin_2_Dec_Test is
>
>    answer : integer;
>    subtype Binary_Number is Integer range 0..1; --define data type
>    type a_bin_num is array(1..8) of Binary_Number; --define array type
>
>    bin : Binary_Number;
>
>    function Bin_To_Dec(bin:Binary_Number) return Integer is
>       Answer : Integer :=0;
>    begin
>       for J in reverse 1..8 loop
>          Answer := Answer + a_bin_num(Bin(J))*(2**(J-1));
>       end loop;
>       return Answer;
>    end Bin_To_Dec;
>
> begin
>    Put_Line("input binary number please: ");
>    for num in 1..8 loop
>       Get(a_bin_num(num));
>    end loop;
>    Put_Line("decimal is: ");
>    Put(Answer(a_bin_num));
> end Bin_2_Dec_Test;
> -------------------------------------------------------------------
> It gives me a lot of errors... really don't know what's wrong...
> Please give some hints, thanks!
>
>
>





  parent reply	other threads:[~2001-03-26 20:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-25 21:37 Converting Binary to Decimal WM
2001-03-26  4:51 ` Wilhelm Spickermann
2001-03-26 12:36 ` Des Walker
2001-03-26 20:57 ` Phaedrus [this message]
2001-03-28  8:15   ` Converting Binary to Decimal - holy war alert Martin Dowie
2001-03-28 18:43     ` Phaedrus
2001-03-29  7:31       ` Martin Dowie
2001-03-29 22:50         ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox