comp.lang.ada
 help / color / mirror / Atom feed
* "In reverse" in for loop acting weird.
@ 2002-09-29 17:28 gilrain
  2002-09-29 17:59 ` Martin Dowie
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: gilrain @ 2002-09-29 17:28 UTC (permalink / raw)


Hi all,

Okay, my program is working perfectly except for this one minor detail. The
result (stored in a string) prints exactly backward. What's baffling me is
that it's still backward even when I reverse the process for building the
string. Here's the offending code segment:

for j in reverse -15..15 loop
   case integer(abs(coefficients(j))) is
      when 0 => result(j+16) := '0';
      when 1 => result(j+16) := '1';
      when 2 => result(j+16) := '2';
      when 3 => result(j+16) := '3';
      when 4 => result(j+16) := '4';
      when 5 => result(j+16) := '5';
      when 6 => result(j+16) := '6';
      when 7 => result(j+16) := '7';
      when 8 => result(j+16) := '8';
      when 9 => result(j+16) := '9';
      when 10 => result(j+16) := 'A';
      when 11 => result(j+16) := 'B';
      when 12 => result(j+16) := 'C';
      when 13 => result(j+16) := 'D';
      when 14 => result(j+16) := 'E';
      when 15 => result(j+16) := 'F';
      when 16 => result(j+16) := 'G';
      when others => result(j+16) := '!';
   end case;
end loop; result(16) := '.';

Coefficients is an array of long floats which at this point in the program
only contains whole numbers. I want coefficients(15) to be the first
character in the string, and so on to -15. The above code prints it
backwards, but the thing which gets me is that taking out "reverse" doesn't
change the program output one bit!

Will someone clue me in?

Thanks,

John Thile






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "In reverse" in for loop acting weird.
  2002-09-29 17:28 "In reverse" in for loop acting weird gilrain
@ 2002-09-29 17:59 ` Martin Dowie
  2002-09-29 21:15   ` Martin Dowie
  2002-09-29 18:31 ` tmoran
  2002-09-29 18:49 ` Jeffrey Carter
  2 siblings, 1 reply; 7+ messages in thread
From: Martin Dowie @ 2002-09-29 17:59 UTC (permalink / raw)


"gilrain" <john.thile@murraystate.edu> wrote in message
news:PFGl9.81453$5M3.3538442@e3500-atl2.usenetserver.com...
> Will someone clue me in?

Absolutely! ;-)





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "In reverse" in for loop acting weird.
  2002-09-29 17:28 "In reverse" in for loop acting weird gilrain
  2002-09-29 17:59 ` Martin Dowie
@ 2002-09-29 18:31 ` tmoran
  2002-09-29 19:17   ` gilrain
  2002-09-29 18:49 ` Jeffrey Carter
  2 siblings, 1 reply; 7+ messages in thread
From: tmoran @ 2002-09-29 18:31 UTC (permalink / raw)


>for j in reverse -15..15 loop
>   case integer(abs(coefficients(j))) is
> ...
>     result(j+16) := ...
There is nothing there that depends on the order of j values.  If
coefficients(7) = 3, then result(7+16=23) := '3', quite independent of
any previous or later value of j.
  Perhaps you want result(16-j) instead of result(16+j), so
result(-7+16=9) would get the '3'.
  Getting stuck on this kind of problem is a good indication it's time
to take a break from your program.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "In reverse" in for loop acting weird.
  2002-09-29 17:28 "In reverse" in for loop acting weird gilrain
  2002-09-29 17:59 ` Martin Dowie
  2002-09-29 18:31 ` tmoran
@ 2002-09-29 18:49 ` Jeffrey Carter
  2002-09-29 19:19   ` gilrain
  2 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Carter @ 2002-09-29 18:49 UTC (permalink / raw)


gilrain wrote:
> Hi all,
> 
> Okay, my program is working perfectly except for this one minor detail. The
> result (stored in a string) prints exactly backward. What's baffling me is
> that it's still backward even when I reverse the process for building the
> string. Here's the offending code segment:
> 
> for j in reverse -15..15 loop
>    case integer(abs(coefficients(j))) is
>       when 0 => result(j+16) := '0';
>       when 1 => result(j+16) := '1';
>       when 2 => result(j+16) := '2';
>       when 3 => result(j+16) := '3';
>       when 4 => result(j+16) := '4';
>       when 5 => result(j+16) := '5';
>       when 6 => result(j+16) := '6';
>       when 7 => result(j+16) := '7';
>       when 8 => result(j+16) := '8';
>       when 9 => result(j+16) := '9';
>       when 10 => result(j+16) := 'A';
>       when 11 => result(j+16) := 'B';
>       when 12 => result(j+16) := 'C';
>       when 13 => result(j+16) := 'D';
>       when 14 => result(j+16) := 'E';
>       when 15 => result(j+16) := 'F';
>       when 16 => result(j+16) := 'G';
>       when others => result(j+16) := '!';
>    end case;
> end loop; result(16) := '.';
> 
> Coefficients is an array of long floats which at this point in the program
> only contains whole numbers. I want coefficients(15) to be the first
> character in the string, and so on to -15. The above code prints it
> backwards, but the thing which gets me is that taking out "reverse" doesn't
> change the program output one bit!

This little excerpt doesn't "print" anything, so understandably we're a 
little confused about what you're talking about. Something that shows 
the object and type declarations referenced, the output statements 
involved, the output obtained, and the output expected will likely get a 
more useful response.

Of the snippet provided above, it appears that you will put the same 
values in result regardless of the order that you traverse Coefficients, 
whether normally, reverse, or some sort of non-repeating random selection.

If the range of Coefficients is -15 .. 15, then your for loop would be 
better written as

for J in [reverse] Coefficients'range loop

In general, when working with arrays, it is rarely necessary to mention 
a magic number more than once. Once the magic number is used to define 
the range of the type or object, attributes, such as 'range above, 
eliminate the need to mention it again.

When you do need to mention a magic number more than once, it's a good 
idea to make it a named number or constant. For example, the 16 in your 
code is a good candidate for this. I suspect it may be

(Result'Length + 1) / 2.

-- 
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "In reverse" in for loop acting weird.
  2002-09-29 18:31 ` tmoran
@ 2002-09-29 19:17   ` gilrain
  0 siblings, 0 replies; 7+ messages in thread
From: gilrain @ 2002-09-29 19:17 UTC (permalink / raw)


tmoran@acm.org wrote:
<<Perhaps you want result(16-j) instead of result(16+j)...>>

Thank you very much! That fixed it.

John Thile (who does need to take a break, but can't)






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "In reverse" in for loop acting weird.
  2002-09-29 18:49 ` Jeffrey Carter
@ 2002-09-29 19:19   ` gilrain
  0 siblings, 0 replies; 7+ messages in thread
From: gilrain @ 2002-09-29 19:19 UTC (permalink / raw)


Thanks for the tips! I'm still learning proper style, so I found these quite
useful. I should have remembered 'range!

John Thile


"Jeffrey Carter" wrote:
> ...
> If the range of Coefficients is -15 .. 15, then your for loop would be
> better written as
>
> for J in [reverse] Coefficients'range loop
>
> In general, when working with arrays, it is rarely necessary to mention
> a magic number more than once. Once the magic number is used to define
> the range of the type or object, attributes, such as 'range above,
> eliminate the need to mention it again.
>
> When you do need to mention a magic number more than once, it's a good
> idea to make it a named number or constant. For example, the 16 in your
> code is a good candidate for this. I suspect it may be
>
> (Result'Length + 1) / 2.
> ...






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "In reverse" in for loop acting weird.
  2002-09-29 17:59 ` Martin Dowie
@ 2002-09-29 21:15   ` Martin Dowie
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Dowie @ 2002-09-29 21:15 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@no-sp-am.btopenworld.com> wrote in message
news:an7f2a$lo7$1@venus.btinternet.com...
> "gilrain" <john.thile@murraystate.edu> wrote in message
> news:PFGl9.81453$5M3.3538442@e3500-atl2.usenetserver.com...
> > Will someone clue me in?
>
> Absolutely! ;-)

Woops! Fat fingers attack! I had intended to cut-&-paste much of what Tom
Moran
described - only my one wasn't as succinct as his! I wasn't trying to rude!
sorry!!





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-09-29 21:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-29 17:28 "In reverse" in for loop acting weird gilrain
2002-09-29 17:59 ` Martin Dowie
2002-09-29 21:15   ` Martin Dowie
2002-09-29 18:31 ` tmoran
2002-09-29 19:17   ` gilrain
2002-09-29 18:49 ` Jeffrey Carter
2002-09-29 19:19   ` gilrain

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