* Converting Binary to Decimal @ 2001-03-25 21:37 WM 2001-03-26 4:51 ` Wilhelm Spickermann ` (2 more replies) 0 siblings, 3 replies; 8+ messages in thread From: WM @ 2001-03-25 21:37 UTC (permalink / raw) 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! ^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: Converting Binary to Decimal 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 2 siblings, 0 replies; 8+ messages in thread From: Wilhelm Spickermann @ 2001-03-26 4:51 UTC (permalink / raw) To: comp.lang.ada [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1: Type: text/plain; charset=us-ascii, Size: 845 bytes --] On 25-Mar-01 WM wrote: > I am writing a program like this: ... > subtype Binary_Number is Integer range 0..1; --define data type ... > function Bin_To_Dec(bin:Binary_Number) return Integer is ... > Answer := Answer + a_bin_num(Bin(J))*(2**(J-1)); ... > It gives me a lot of errors... really don't know what's wrong... > Please give some hints, thanks! The first error message I get points to Line 16, Character 39 and says: "array type required in indexed component". The "indexed component" is "Bin(J)". "Bin" is not an array, it�s an integer (0 or 1). The poor naming is the reason of the problems: Rename every occurrence of "Binary_Number" to "Binary_Digit_Type" and every occurrence of A_Bin_Num to "Binary_Number_Type" and then look at the function declaration: Did You really want what is written there? Wilhelm ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Converting Binary to Decimal 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 2 siblings, 0 replies; 8+ messages in thread From: Des Walker @ 2001-03-26 12:36 UTC (permalink / raw) Hi, this is not really an answer to your question, please disregard if it is not relevant. Have you considered reading your binary number as a string and using predefined attributes to convert it. e.g. with Ada.Text_Io; use Ada.Text_Io; procedure Convert_Test is Value_Str : String(1 .. 8); Last : Natural; Value_Int : Integer; begin Put_Line("input binary number please: "); Get_Line(Value_Str, Last); Value_Int := Integer'Value("2#" & Value_Str(1 .. Last) & '#'); Put_Line("Decimal is" & Integer'Image(Value_Int)); end Convert_Test; Admittedly if the user typed a bad digit, this version would produce a Constraint Error on the conversion, whereas yours would produce an error on the Get call. Des Walker Alenia-Marconi Systems ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Converting Binary to Decimal 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 2001-03-28 8:15 ` Converting Binary to Decimal - holy war alert Martin Dowie 2 siblings, 1 reply; 8+ messages in thread From: Phaedrus @ 2001-03-26 20:57 UTC (permalink / raw) 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! > > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Converting Binary to Decimal - holy war alert 2001-03-26 20:57 ` Phaedrus @ 2001-03-28 8:15 ` Martin Dowie 2001-03-28 18:43 ` Phaedrus 0 siblings, 1 reply; 8+ messages in thread From: Martin Dowie @ 2001-03-28 8:15 UTC (permalink / raw) 'a_bin_num' doesn't imply it's a variable - 'a' is the "indefinate article" and can be used to indicate the type of something without specifying _which_ item you are talking about e.g. My_Kettle : A_Kettle := (Make => Kenwood, Capacity_In_Litres => 1.5, -- etc); Your_Kettle : A_Kettle := (Make => Bosch, Capacity_In_Litres => 2.0, -- etc); etc. Phaedrus <phaedrusalt@hotmail.com> wrote in message news:n3Ov6.15480$ue1.1287176@newsread2.prod.itd.earthlink.net... > 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", ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Converting Binary to Decimal - holy war alert 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 0 siblings, 1 reply; 8+ messages in thread From: Phaedrus @ 2001-03-28 18:43 UTC (permalink / raw) In that case, would you please deliver to me A_Billion_Dollars? I don't care which billion dollars you deliver. The point I was trying to get across was that unless something is explicitly shown as a type, it tends to look like a variable. That's why I use the convention "_type" post-pended to my type definitions, and that's what I was recommending for his use, too. It's pretty hard to confuse "A_Dog_Type" with an actual instance of the type, and try to feed it or something. So, when do you think you can deliver the money? Phaedrus "Martin Dowie" <martin.dowie@baesystems.com> wrote in message news:3ac19bdf$1@pull.gecm.com... > 'a_bin_num' doesn't imply it's a variable - 'a' is the "indefinate article" > and > can be used to indicate the type of something without specifying _which_ > item you are talking about e.g. > > My_Kettle : A_Kettle := (Make => Kenwood, Capacity_In_Litres => 1.5, -- > etc); > Your_Kettle : A_Kettle := (Make => Bosch, Capacity_In_Litres => 2.0, -- > etc); > etc. > > > Phaedrus <phaedrusalt@hotmail.com> wrote in message > news:n3Ov6.15480$ue1.1287176@newsread2.prod.itd.earthlink.net... > > 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", > > > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Converting Binary to Decimal - holy war alert 2001-03-28 18:43 ` Phaedrus @ 2001-03-29 7:31 ` Martin Dowie 2001-03-29 22:50 ` Robert A Duff 0 siblings, 1 reply; 8+ messages in thread From: Martin Dowie @ 2001-03-29 7:31 UTC (permalink / raw) On A_Day :-) Seriously though the 'A_'/'An_' notation is used in some s/w houses (I know of 3 spread across the UK), precisely to to highlight the difference between the abstract and the specific. Phaedrus <phaedrusalt@hotmail.com> wrote in message news:bhqw6.5005$aP5.412427@newsread2.prod.itd.earthlink.net... > In that case, would you please deliver to me A_Billion_Dollars? I don't care > which billion dollars you deliver. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Converting Binary to Decimal - holy war alert 2001-03-29 7:31 ` Martin Dowie @ 2001-03-29 22:50 ` Robert A Duff 0 siblings, 0 replies; 8+ messages in thread From: Robert A Duff @ 2001-03-29 22:50 UTC (permalink / raw) "Martin Dowie" <martin.dowie@baesystems.com> writes: > Seriously though the 'A_'/'An_' notation is used in some s/w houses (I know > of 3 spread across the UK), precisely to to highlight the difference between > the abstract and the specific. It's used in Smalltalk, too. Eg, if a method takes two Strings, they will typically be called aString and anotherString. You can't declare that they're of type String in Smalltalk -- the name of the thing (or a comment) is the only hint. - Bob ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2001-03-29 22:50 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox