comp.lang.ada
 help / color / mirror / Atom feed
* Problem from a Newbie
@ 2004-01-13  8:44 Cecilia Chew
  2004-01-13 12:37 ` David C. Hoos
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Cecilia Chew @ 2004-01-13  8:44 UTC (permalink / raw)


Hello All,
I'm a newbie in Ada. There are a lot of question mark in my mind. I'm 
now doing a simple program and the desired output is a signal wave as below.

*      **      **      *
  *    *  *    *  *    *
   *  *    *  *    *  *
    **      **      **
And the coding that i have done is as follow.

with Ada.Text_Io, Ada.Float_Text_IO;
use Ada.Text_Io, Ada.Float_Text_IO;

procedure Graph1 is
    Y : Integer;
    Max_Point : Float;
    Min_Point : Float;
    Pi : Constant := 3.142;
begin

    Max_Point := 1.0;
    for X in 0 .. 360 loop
       for r in 0.0 .. Max_Point loop
          Y := sin (X * 2 * Pi/360);
          Put ("*");
       end loop;
    end loop;
end Graph1;

Could someone tell me what are the mistakes in the coding? Error message 
from the compiler is the 'sin' is undefined. what should i do with this?


-- 
Cecilia Chew
c e c i l i a <AT> g r e e n l i m e . c o m



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

* Re: Problem from a Newbie
  2004-01-13  8:44 Problem from a Newbie Cecilia Chew
@ 2004-01-13 12:37 ` David C. Hoos
  2004-01-13 17:42 ` Jeffrey Carter
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: David C. Hoos @ 2004-01-13 12:37 UTC (permalink / raw)


The Sin function is contained in the package
Ada.Numerics.Generic_Elementary_Functions
which must be "withed," and then instantiated
for type "Float."

The result of the function is a value of type
"Float," so it cannot be assigned to the
Integer variable "Y," as you have done in
your code.

Also, the loop variable "R" in your code must
be a discrete type, not a real type.

"Cecilia Chew" <cecilia@nowhere.com> wrote in message
news:4003b00c_2@news.tm.net.my...
> Hello All,
> I'm a newbie in Ada. There are a lot of question mark in my mind. I'm
> now doing a simple program and the desired output is a signal wave as
below.
>
> *      **      **      *
>   *    *  *    *  *    *
>    *  *    *  *    *  *
>     **      **      **
> And the coding that i have done is as follow.
>
> with Ada.Text_Io, Ada.Float_Text_IO;
> use Ada.Text_Io, Ada.Float_Text_IO;
>
> procedure Graph1 is
>     Y : Integer;
>     Max_Point : Float;
>     Min_Point : Float;
>     Pi : Constant := 3.142;
> begin
>
>     Max_Point := 1.0;
>     for X in 0 .. 360 loop
>        for r in 0.0 .. Max_Point loop
>           Y := sin (X * 2 * Pi/360);
>           Put ("*");
>        end loop;
>     end loop;
> end Graph1;
>
> Could someone tell me what are the mistakes in the coding? Error message
> from the compiler is the 'sin' is undefined. what should i do with this?
>
>
> -- 
> Cecilia Chew
> c e c i l i a <AT> g r e e n l i m e . c o m
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada-france.org
> http://www.ada-france.org/mailman/listinfo/comp.lang.ada
>
>




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

* Re: Problem from a Newbie
  2004-01-13  8:44 Problem from a Newbie Cecilia Chew
  2004-01-13 12:37 ` David C. Hoos
@ 2004-01-13 17:42 ` Jeffrey Carter
  2004-01-14  5:49 ` Steve
       [not found] ` <kc8fd1-ul5.ln1@beastie.ix.netcom.com>
  3 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2004-01-13 17:42 UTC (permalink / raw)


Cecilia Chew wrote:

> Hello All,
> I'm a newbie in Ada. There are a lot of question mark in my mind. I'm 
> now doing a simple program and the desired output is a signal wave as 
> below.
> 
> *      **      **      *
>  *    *  *    *  *    *
>   *  *    *  *    *  *
>    **      **      **
> And the coding that i have done is as follow.
> 
> with Ada.Text_Io, Ada.Float_Text_IO;
> use Ada.Text_Io, Ada.Float_Text_IO;

You do not use Ada.Float_Text_IO.

> 
> procedure Graph1 is
>    Y : Integer;
>    Max_Point : Float;
>    Min_Point : Float;
>    Pi : Constant := 3.142;

Pi is defined in Ada.Numerics to 50 decimal places. It's probably better 
to use Ada.Numerics.Pi than to duplicate it with your own constant.

> begin
> 
>    Max_Point := 1.0;
>    for X in 0 .. 360 loop
>       for r in 0.0 .. Max_Point loop

A for loop operates over a discrete subtype (such as [Integer range]
0 .. 360 used for X), so this construct is illegal.

>          Y := sin (X * 2 * Pi/360);

Sin is, as your compiler told you, undefined. You may define your own, 
or you may use an instanciation of 
Ada.Numerics.Generic_Elementary_Functions, or you may use 
Ada.Numerics.Elementary_Functions, which is an instanciation of 
Generic_Elementary_Functions for type Float.

Sin in [Generic_]Elementary_Functions returns a floating point type; Y 
is type Integer, so the assignment is illegal unless you've defined your 
own Sin function that returns Integer.

>          Put ("*");

This is going to produce a single line of *s regardless of the results 
of your calculations, so why bother performing the calculations at all?

>       end loop;
>    end loop;
> end Graph1;

-- 
Jeff Carter
"Sir Robin the-not-quite-so-brave-as-Sir-Lancelot,
who had nearly fought the Dragon of Angnor,
who nearly stood up to the vicious Chicken of Bristol,
and who had personally wet himself at the
Battle of Badon Hill."
Monty Python & the Holy Grail
68




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

* Re: Problem from a Newbie
  2004-01-13  8:44 Problem from a Newbie Cecilia Chew
  2004-01-13 12:37 ` David C. Hoos
  2004-01-13 17:42 ` Jeffrey Carter
@ 2004-01-14  5:49 ` Steve
  2004-01-14 21:08   ` Simon Wright
       [not found] ` <kc8fd1-ul5.ln1@beastie.ix.netcom.com>
  3 siblings, 1 reply; 9+ messages in thread
From: Steve @ 2004-01-14  5:49 UTC (permalink / raw)


In addition to the other comments...
You don't need to convert degrees to radians as you have done.  There is a
version of sin in Ada.Numerics.Elementary_Functions that takes a second
value as the number of units in a cycle.  So instead of using:

  sin( X * 2.0 * Pi / 360.0 )

You can use

  sin( X, 360.0 )

Which (I think) reads a lot cleaner.

Steve
(The Duck)

"Cecilia Chew" <cecilia@nowhere.com> wrote in message
news:4003b00c_2@news.tm.net.my...
> Hello All,
> I'm a newbie in Ada. There are a lot of question mark in my mind. I'm
> now doing a simple program and the desired output is a signal wave as
below.
>
> *      **      **      *
>   *    *  *    *  *    *
>    *  *    *  *    *  *
>     **      **      **
> And the coding that i have done is as follow.
>
> with Ada.Text_Io, Ada.Float_Text_IO;
> use Ada.Text_Io, Ada.Float_Text_IO;
>
> procedure Graph1 is
>     Y : Integer;
>     Max_Point : Float;
>     Min_Point : Float;
>     Pi : Constant := 3.142;
> begin
>
>     Max_Point := 1.0;
>     for X in 0 .. 360 loop
>        for r in 0.0 .. Max_Point loop
>           Y := sin (X * 2 * Pi/360);
>           Put ("*");
>        end loop;
>     end loop;
> end Graph1;
>
> Could someone tell me what are the mistakes in the coding? Error message
> from the compiler is the 'sin' is undefined. what should i do with this?
>
>
> -- 
> Cecilia Chew
> c e c i l i a <AT> g r e e n l i m e . c o m





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

* Re: Problem from a Newbie
       [not found] ` <kc8fd1-ul5.ln1@beastie.ix.netcom.com>
@ 2004-01-14 16:42   ` Jean-Pierre Rosen
  2004-01-15  8:56     ` Dmitry A. Kazakov
  2004-01-14 17:41   ` Jeffrey Carter
  1 sibling, 1 reply; 9+ messages in thread
From: Jean-Pierre Rosen @ 2004-01-14 16:42 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 317 bytes --]


"Dennis Lee Bieber" <wlfraed@ix.netcom.com> a �crit dans le message de news:kc8fd1-
[...]
>                 for j in 0..integer(Position) loop
>                         put(" ");       --space output across line
>                 end loop;
BTW, this can be written as:
  put ((1..Integer(position)+1 => ' '));





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

* Re: Problem from a Newbie
       [not found] ` <kc8fd1-ul5.ln1@beastie.ix.netcom.com>
  2004-01-14 16:42   ` Jean-Pierre Rosen
@ 2004-01-14 17:41   ` Jeffrey Carter
  2004-01-15  1:03     ` Jeffrey Carter
  1 sibling, 1 reply; 9+ messages in thread
From: Jeffrey Carter @ 2004-01-14 17:41 UTC (permalink / raw)


Dennis Lee Bieber wrote:

>         Cycle : float := 360.0;         --use degrees directly in sin()
>         HalfWidth : float := 30.0;      --scale 0..1 to 0..30 (and -1..0)
>         StepCycle : float := Cycle / 45.0;      --reduce total points
>         Length : integer := integer((2.0 * Cycle) / StepCycle); --#points

These all appear to be constants, and should be declared as such. The 
first 3 could be named numbers.

> 
>         Point : float;
>         Position : float;
> 
> begin
>         for i in 0..Length loop
>                 Point := ff.sin(float(i) * StepCycle, Cycle);
>                 Position := Point * HalfWidth + HalfWidth + 1.0;
>                 new_line;
>                 for j in 0..integer(Position) loop
>                         put(" ");       --space output across line
>                 end loop;
>                 put("*");
>         end loop;
> end wave;


-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall
44




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

* Re: Problem from a Newbie
  2004-01-14  5:49 ` Steve
@ 2004-01-14 21:08   ` Simon Wright
  0 siblings, 0 replies; 9+ messages in thread
From: Simon Wright @ 2004-01-14 21:08 UTC (permalink / raw)


"Steve" <nospam_steved94@comcast.net> writes:

> You can use
> 
>   sin( X, 360.0 )
> 
> Which (I think) reads a lot cleaner.

I think 
   Sin (X, Cycle => 360.0)
is even better (otherwise, after a brief absence, the idea of a
2-argument sin function may be baffling!)
 
-- 
Simon Wright                               100% Ada, no bugs.



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

* Re: Problem from a Newbie
  2004-01-14 17:41   ` Jeffrey Carter
@ 2004-01-15  1:03     ` Jeffrey Carter
  0 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2004-01-15  1:03 UTC (permalink / raw)


Jeffrey Carter wrote:

> Dennis Lee Bieber wrote:
> 
>>         Cycle : float := 360.0;         --use degrees directly in sin()
>>         HalfWidth : float := 30.0;      --scale 0..1 to 0..30 (and -1..0)
>>         StepCycle : float := Cycle / 45.0;      --reduce total points
>>         Length : integer := integer((2.0 * Cycle) / StepCycle); --#points
> 
> These all appear to be constants, and should be declared as such. The 
> first 3 could be named numbers.
> 
>>         Point : float;
>>         Position : float;
>>
>> begin
>>         for i in 0..Length loop
>>                 Point := ff.sin(float(i) * StepCycle, Cycle);
>>                 Position := Point * HalfWidth + HalfWidth + 1.0;
>>                 new_line;
>>                 for j in 0..integer(Position) loop
>>                         put(" ");       --space output across line
>>                 end loop;
>>                 put("*");
>>         end loop;
>> end wave;

I apologize for leaving this big chunk of quoted text at the end for now 
reason. It's something I try to avoid.

-- 
Jeff Carter
"We use a large, vibrating egg."
Annie Hall
44




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

* Re: Problem from a Newbie
  2004-01-14 16:42   ` Jean-Pierre Rosen
@ 2004-01-15  8:56     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2004-01-15  8:56 UTC (permalink / raw)


On Wed, 14 Jan 2004 17:42:59 +0100, "Jean-Pierre Rosen"
<rosen@adalog.fr> wrote:

>"Dennis Lee Bieber" <wlfraed@ix.netcom.com> a �crit dans le message de news:kc8fd1-
>[...]
>>                 for j in 0..integer(Position) loop
>>                         put(" ");       --space output across line
>>                 end loop;
>BTW, this can be written as:
>  put ((1..Integer(position)+1 => ' '));

Or, using number to character multiplication:

with Strings.Fixed; use Strings.Fixed;
...
Put ((Integer (Position) + 1) * ' ');

--
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



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

end of thread, other threads:[~2004-01-15  8:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-13  8:44 Problem from a Newbie Cecilia Chew
2004-01-13 12:37 ` David C. Hoos
2004-01-13 17:42 ` Jeffrey Carter
2004-01-14  5:49 ` Steve
2004-01-14 21:08   ` Simon Wright
     [not found] ` <kc8fd1-ul5.ln1@beastie.ix.netcom.com>
2004-01-14 16:42   ` Jean-Pierre Rosen
2004-01-15  8:56     ` Dmitry A. Kazakov
2004-01-14 17:41   ` Jeffrey Carter
2004-01-15  1:03     ` Jeffrey Carter

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