comp.lang.ada
 help / color / mirror / Atom feed
* invalid parameter list
@ 2006-02-17 23:13 isaac2004
  2006-02-18  6:02 ` jimmaureenrogers
       [not found] ` <tfadv19pqf17h3prdhquk3e5f30j6vhdhl@4ax.com>
  0 siblings, 2 replies; 20+ messages in thread
From: isaac2004 @ 2006-02-17 23:13 UTC (permalink / raw)


hello
i am getting an error about an invalid parameter in this code

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
procedure Assignment3 is

---------------------------------------------------------------------
   --| program that takes a range in the form of a minimum and maximum
number,
   --| and prompts for the length, to find all sociable chains inside
of the range
   --| to that desired chain
   --| Isaac Levin, Western Washington
   --| February 2006

---------------------------------------------------------------------------

   Start : Natural;
   Stop  : Natural;
   Max   : Natural range 1 .. 30;




   -- start of function that takes the inputted number and finds the
sum of the divisors
   -- function description
   function Sum_Of_Divisors (
         N : Integer)
     return Integer;

   -- function body

   function Sum_Of_Divisors (
         N : Integer)
     return Integer is
      C   : Integer;
      Sum : Integer;

   begin
      Sum := 1;
      C:=2;
      while C**2 <= N loop --Number

         if N mod C = 0 then
            if C * 2 = N then
               Sum := Sum + C;
            else
               Sum := Sum + C + N / C;
            end if;
         end if;
         C := C + 1 ;
      end loop;
      return Sum ;

   end Sum_Of_Divisors;




   procedure Sociable_Chain (
         Start,
         Max   : Natural) is
      A : array (1 .. 30) of Positive;
      I : Positive;

   begin
      A(1) :=Start; -- makes first number of array the prompt number
      A(2) := Sum_Of_Divisors (A(1)); -- 2nd number in array is sum of
divisors of prompt
      while I < Max and A (1) /= A(I) loop -- loop till length is
reached and till A(I) = A(1)
         I:= I + 1;
         A(I) := Sum_Of_Divisors (A(I - 1));


      end loop;

   end Sociable_Chain;

begin


   Put(Item => " Please enter your starting integer. ");
   Get(Item => Start);
   New_Line;
   Put(Item => " Please enter your ending integer. ");
   Get(Item => Stop);
   New_Line;
   Put(Item => " Please choose the length of your chain. ");
   Get(Item => Max);



   --         for N in Minnumber..Maxnumber loop
   --            Sociable_Chain(Minnumber, Length);

   --         end loop;

-- Put (Item => Sum_Of_Divisors (Start), Width => 0  ) ;

---------------------------------------------------error here
   Put (Item => Sociable_Chain ( Start, Max ), Width => 0  ) ;

-----------------------------------------------------------------------------------------


end Assignment3 ;

am i specifying the wrong values for my procedure

thanx for the help




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

* Re: invalid parameter list
  2006-02-17 23:13 invalid parameter list isaac2004
@ 2006-02-18  6:02 ` jimmaureenrogers
       [not found] ` <tfadv19pqf17h3prdhquk3e5f30j6vhdhl@4ax.com>
  1 sibling, 0 replies; 20+ messages in thread
From: jimmaureenrogers @ 2006-02-18  6:02 UTC (permalink / raw)



isaac2004 wrote:
> hello
> i am getting an error about an invalid parameter in this code
>
...
>
>    procedure Sociable_Chain (
>          Start,
>          Max   : Natural) is
>       A : array (1 .. 30) of Positive;
>       I : Positive;
>
>    begin
>       A(1) :=Start; -- makes first number of array the prompt number
>       A(2) := Sum_Of_Divisors (A(1)); -- 2nd number in array is sum of
> divisors of prompt
>       while I < Max and A (1) /= A(I) loop -- loop till length is
> reached and till A(I) = A(1)
>          I:= I + 1;
>          A(I) := Sum_Of_Divisors (A(I - 1));
>
>
>       end loop;
>
>    end Sociable_Chain;
>
> begin
>
>
>    Put(Item => " Please enter your starting integer. ");
>    Get(Item => Start);
>    New_Line;
>    Put(Item => " Please enter your ending integer. ");
>    Get(Item => Stop);
>    New_Line;
>    Put(Item => " Please choose the length of your chain. ");
>    Get(Item => Max);
>
>
>
>    --         for N in Minnumber..Maxnumber loop
>    --            Sociable_Chain(Minnumber, Length);
>
>    --         end loop;
>
> -- Put (Item => Sum_Of_Divisors (Start), Width => 0  ) ;
>
> ---------------------------------------------------error here
>    Put (Item => Sociable_Chain ( Start, Max ), Width => 0  ) ;
>
> -----------------------------------------------------------------------------------------
>
>
> end Assignment3 ;
>
> am i specifying the wrong values for my procedure

The Put procedure you are calling expects the value associated
with Item to be an Integer. You call procedure Sociable_Chain.
Procedures do not return values. The procedure Put has no
value to associate with its Item parameter.

Have you considered converting Sociable_Chain to a
function returning a Natural?

Jim Rogers




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

* Re: invalid parameter list
       [not found] ` <tfadv19pqf17h3prdhquk3e5f30j6vhdhl@4ax.com>
@ 2006-02-18  8:04   ` isaac2004
  2006-02-18 14:34     ` Björn Persson
  0 siblings, 1 reply; 20+ messages in thread
From: isaac2004 @ 2006-02-18  8:04 UTC (permalink / raw)


so how do i preform a case test with a picked integer to test the
function for bugs. do i just put it in a loop till all chains are
found. the output for this is writing the chains to a txt file and then
outputting them on the screen. my loop would look something like this

for N Start.. Stop loop

Sociable_Chain ( N, Max) ;

end loop;

that is how am i thinking will this be buggy or what

my whole code i like this after your help

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
procedure Assignment3 is

---------------------------------------------------------------------
   --| program that takes a range in the form of a minimum and maximum
number,
   --| and prompts for the length, to find all sociable chains inside
of the range
   --| to that desired chain
   --| Isaac Levin, Western Washington
   --| February 2006

---------------------------------------------------------------------------

   Start : Natural;
   Stop  : Natural range Start..Integer'Last;
   Max   : Natural range 1 .. 30;




   -- start of function that takes the inputted number and finds the
sum of the divisors
   -- function description
   function Sum_Of_Divisors (
         N : Integer)
     return Integer;

   -- function body

   function Sum_Of_Divisors (
         N : Integer)
     return Integer is
      C   : Integer;
      Sum : Integer;

   begin
      Sum := 1;
      C:=2;
      while C**2 <= N loop --Number

         if N mod C = 0 then
            if C * 2 = N then
               Sum := Sum + C;
            else
               Sum := Sum + C + N / C;
            end if;
         end if;
         C := C + 1 ;
      end loop;
      return Sum ;

   end Sum_Of_Divisors;






      procedure Sociable_Chain (
            Start,
            Max   : Natural) is
         A : array (1 .. 30) of Positive;
         I : Positive := 2;

      begin
         A(1) :=Start; -- makes first number of array the prompt number
         A(2) := Sum_Of_Divisors (A(1));
         -- 2nd number in array is sum of divisors of prompt
         while I < Max and A (1) /= A(I) loop
            -- loop till length is reached and till A(I) = A(1)
            I:= I + 1;
            A(I) := Sum_Of_Divisors (A(I - 1));


         end loop;

      end Sociable_Chain;

      begin


         Put(Item => "Please enter your starting integer.");
         Get(Item => Start);
         New_Line;
         Put(Item => " Please enter your ending integer.");
         Get(Item => Stop);
         New_Line;
         Put(Item => " Please enter the length of largest chain. ");
         Get(Item => Max);


for N Start.. Stop loop

Sociable_Chain ( N, Max) ;

end loop;



      end Assignment3 ;




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

* Re: invalid parameter list
  2006-02-18  8:04   ` isaac2004
@ 2006-02-18 14:34     ` Björn Persson
  2006-02-18 21:19       ` isaac2004
  0 siblings, 1 reply; 20+ messages in thread
From: Björn Persson @ 2006-02-18 14:34 UTC (permalink / raw)


isaac2004 wrote:
> the output for this is writing the chains to a txt file and then
> outputting them on the screen. my loop would look something like this
> 
> for N Start.. Stop loop
> 
> Sociable_Chain ( N, Max) ;
> 
> end loop;
> 
> that is how am i thinking will this be buggy or what

You can do the writing either inside Sociable_Chain or in another 
procedure. If you'll do it in another procedure you'll need to get the 
whole array out of Sociable_Chain and pass it to that other procedure.

>    function Sum_Of_Divisors (
>          N : Integer)
>      return Integer is
>       C   : Integer;
>       Sum : Integer;
> 
>    begin
>       Sum := 1;
>       C:=2;
>       while C**2 <= N loop --Number
> 
>          if N mod C = 0 then
>             if C * 2 = N then
>                Sum := Sum + C;
>             else
>                Sum := Sum + C + N / C;
>             end if;
>          end if;
>          C := C + 1 ;
>       end loop;
>       return Sum ;
> 
>    end Sum_Of_Divisors;

If I call this function with N=9 it will return 7. Is that really right?

Why Integer? What should the function return if N is -38? (What are the 
proper divisors of 1 or 0 anyway?)

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: invalid parameter list
  2006-02-18 14:34     ` Björn Persson
@ 2006-02-18 21:19       ` isaac2004
  2006-02-18 23:31         ` Björn Persson
  0 siblings, 1 reply; 20+ messages in thread
From: isaac2004 @ 2006-02-18 21:19 UTC (permalink / raw)


>You can do the writing either inside Sociable_Chain or in another
procedure. If you'll do it in another procedure you'll need to get the
whole array out of Sociable_Chain and pass it to that other procedure.

does that loop look correct though, also how do i write the a chain to
a txt file?


>If I call this function with N=9 it will return 7. Is that really right?

Why Integer? What should the function return if N is -38? (What are the

proper divisors of 1 or 0 anyway?)


should i use natural then instead of integer, there are no divisors of
1 or 0, the divisors of 9 is 7 ( 1+ 3+3)




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

* Re: invalid parameter list
  2006-02-18 21:19       ` isaac2004
@ 2006-02-18 23:31         ` Björn Persson
  2006-02-18 23:34           ` Martin Dowie
  0 siblings, 1 reply; 20+ messages in thread
From: Björn Persson @ 2006-02-18 23:31 UTC (permalink / raw)


isaac2004 wrote:
> does that loop look correct though,

Yes, except that the keyword "in" is missing.

> also how do i write the a chain to a txt file?

Declare a variable of the type Ada.Text_IO.File_Type, open the file with 
Ada.Text_IO.Create and write each number with Ada.Integer_Text_IO.Put.

>> If I call this function with N=9 it will return 7. Is that really right?
>> 
>> Why Integer? What should the function return if N is -38? (What are the
>> proper divisors of 1 or 0 anyway?)
> 
> should i use natural then instead of integer,

According to Wikipedia (http://en.wikipedia.org/wiki/Divisor), only 
positive divisors can be proper divisors, so the sum of a set of proper 
divisors will always be positive. It is therefore pointless to search 
for sociable chains among the negative intgers, so yes, I think you 
should use Natural.

> there are no divisors of 1 or 0,

Then shouldn't the sum of their proper divisors be 0? Your function will 
return 1.

> the divisors of 9 is 7 ( 1+ 3+3)

The divisors of 9 are -9, -3, -1, 1, 3 and 9. The _proper_ divisors are 
1 and 3. 1+3=4.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: invalid parameter list
  2006-02-18 23:31         ` Björn Persson
@ 2006-02-18 23:34           ` Martin Dowie
  2006-02-19  0:06             ` isaac2004
  2006-02-19  0:15             ` Ada.Text_IO.Create (was: invalid parameter list) Björn Persson
  0 siblings, 2 replies; 20+ messages in thread
From: Martin Dowie @ 2006-02-18 23:34 UTC (permalink / raw)


>> also how do i write the a chain to a txt file?
> 
> Declare a variable of the type Ada.Text_IO.File_Type, open the file with 
> Ada.Text_IO.Create and write each number with Ada.Integer_Text_IO.Put.

Beware that if the file already exists Ada.Text_IO.Create will raise an 
exception, so you have to use a "begin ... exception ... end;" block.

I always think a "Overwrite_Existing_File : Boolean := False" parameter 
on this function would be handy...

Cheers

-- Martin



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

* Re: invalid parameter list
  2006-02-18 23:34           ` Martin Dowie
@ 2006-02-19  0:06             ` isaac2004
       [not found]               ` <dmlfv11b4qagaf3gec853k9on191576gqc@4ax.com>
  2006-02-19 15:54               ` Björn Persson
  2006-02-19  0:15             ` Ada.Text_IO.Create (was: invalid parameter list) Björn Persson
  1 sibling, 2 replies; 20+ messages in thread
From: isaac2004 @ 2006-02-19  0:06 UTC (permalink / raw)


> there are no divisors of 1 or 0,


>Then shouldn't the sum of their proper divisors be 0? Your function will
return 1.


>> the divisors of 9 is 7 ( 1+ 3+3)


>The divisors of 9 are -9, -3, -1, 1, 3 and 9. The _proper_ divisors are
1 and 3. 1+3=4.

so what should i do to my code to fix this, take out the C := C + 1 ;
part
i still dont understand how to test the sociable chain part and then
finally write to a file, do i just go something like

chains : File_Type;


code

Create(File => chains, Mode => Out_File, Name => "chains.txt")
Put (File => chains, Item => Sociable_Chain ( Start, Max), Width => 0
); 

is this right? can i plz get some code examples . thank you




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

* Ada.Text_IO.Create (was: invalid parameter list)
  2006-02-18 23:34           ` Martin Dowie
  2006-02-19  0:06             ` isaac2004
@ 2006-02-19  0:15             ` Björn Persson
  2006-02-19  8:14               ` Ada.Text_IO.Create Martin Dowie
  1 sibling, 1 reply; 20+ messages in thread
From: Björn Persson @ 2006-02-19  0:15 UTC (permalink / raw)


Martin Dowie wrote:
> Beware that if the file already exists Ada.Text_IO.Create will raise an 
> exception, so you have to use a "begin ... exception ... end;" block.

Really? I don't see that in AARM95 A.8.2 or A.10.2.

The following test program works just fine with GCC 4.0.2 on Fedora 4:

with Ada.Text_IO;
procedure Creation_Test is
    The_File  : Ada.Text_IO.File_Type;
    File_Name : String := "test_file";
begin
    Ada.Text_IO.Create(The_File, Ada.Text_IO.Out_File, File_Name);
    Ada.Text_IO.Put(The_File, "one");
    Ada.Text_IO.Close(The_File);
    Ada.Text_IO.Create(The_File, Ada.Text_IO.Out_File, File_Name);
    Ada.Text_IO.Put(The_File, "two");
    Ada.Text_IO.Close(The_File);
end Creation_Test;

$ gnatmake creation_test
gcc -c creation_test.adb
gnatbind -x creation_test.ali
gnatlink creation_test.ali
$ ./creation_test
$ ./creation_test
$ ./creation_test
$ cat test_file
two
$

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: invalid parameter list
       [not found]               ` <dmlfv11b4qagaf3gec853k9on191576gqc@4ax.com>
@ 2006-02-19  4:06                 ` isaac2004
       [not found]                   ` <uavfv1pjdorpim6lpu8sjfdlq4lmmn1jes@4ax.com>
  0 siblings, 1 reply; 20+ messages in thread
From: isaac2004 @ 2006-02-19  4:06 UTC (permalink / raw)


so what your saying is that a chain of 30 length will cause an error
when attempted, sorry for sounding confused but my prof said that my
code would work for no more than 30 numbers in a chain.

>{And NO, I am NOT going to post the source code that runs that im not expecting you to give me code to do it but to tell me what to fix. i understand that there is something wrong with my sociable chain function but i dont understand the problems i am being told. the code writes the starting digit to an array and then finds the sum of its divisors and writes that to the second portion of the array and etc. is there a way i can just test the sociable chain function so i can see if the arithmetic works. im sorry i dont understand all terms that you guys are saying but ive only been coding for a month. i wrote all the code myself and dont expect someone to say

your code is inefficient use this.

i dont want that, all im asking for is layman explanation of how to do
it. thank you




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

* Re: Ada.Text_IO.Create
  2006-02-19  0:15             ` Ada.Text_IO.Create (was: invalid parameter list) Björn Persson
@ 2006-02-19  8:14               ` Martin Dowie
  2006-02-19 19:45                 ` Ada.Text_IO.Create Jeffrey R. Carter
  0 siblings, 1 reply; 20+ messages in thread
From: Martin Dowie @ 2006-02-19  8:14 UTC (permalink / raw)


Bj�rn Persson wrote:
> Martin Dowie wrote:
>> Beware that if the file already exists Ada.Text_IO.Create will raise 
>> an exception, so you have to use a "begin ... exception ... end;" block.
> 
> Really? I don't see that in AARM95 A.8.2 or A.10.2.

Sorry, I got confused - it's "Open" that raises an exception 
(Name_Error) if the file /doesn't/ already exist.

That's the subprogram I'd prefer to have an extra "Create_If_Absent : 
Boolean := False" parameter. And, yes, I know it control coupling (bad) 
but IMHO, I would find the code cleaner.

Cheers

-- Martin



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

* Re: invalid parameter list
  2006-02-19  0:06             ` isaac2004
       [not found]               ` <dmlfv11b4qagaf3gec853k9on191576gqc@4ax.com>
@ 2006-02-19 15:54               ` Björn Persson
  1 sibling, 0 replies; 20+ messages in thread
From: Björn Persson @ 2006-02-19 15:54 UTC (permalink / raw)


isaac2004 wrote:
> i still dont understand how to test the sociable chain part and then
> finally write to a file, do i just go something like
> 
> chains : File_Type;
> 
> 
> code
> 
> Create(File => chains, Mode => Out_File, Name => "chains.txt")

Correct this far.

> Put (File => chains, Item => Sociable_Chain ( Start, Max), Width => 0); 

I assume you have some resource where you can look up the definition of 
Ada.Integer_Text_IO.Put. (If not, tell your teacher you need a reference 
to the standard library, so that you can know how to use it.) You'll see 
that the type of Item is Integer. The compiler would accept the 
statement above if you'd turn Sociable_Chain into a function and made it 
return Integer (or a subtype of Integer such as Natural) � but what 
number should it return? You need to print the whole chain, not just one 
integer. You'll have to go through the array and call Put once for each 
number in the chain, and once more to print the length. So how do you go 
through an array and do something with each element? With a loop of course:

for Index in An_Array'Range loop
    Put(File => chains, Item => An_Array(Index), Width => 0);
end loop;

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: invalid parameter list
       [not found]                   ` <uavfv1pjdorpim6lpu8sjfdlq4lmmn1jes@4ax.com>
@ 2006-02-19 16:21                     ` Björn Persson
  0 siblings, 0 replies; 20+ messages in thread
From: Björn Persson @ 2006-02-19 16:21 UTC (permalink / raw)


Dennis Lee Bieber wrote:
> 	For the example data (12000 .. 15000, and chain length of 30) I
> encounter an overflow error.

Isaac probably doesn't know yet what an overflow is.

What Dennis is saying is that on his computer, this line:

                Sum := Sum + C + N / C;

sometimes fails because the sum becomes too big. Assuming Dennis hasn't 
changed the type of Sum, this means that sometimes the addition results 
in a sum that is greater than the upper limit of Integer, whatever that 
limit is on Dennis' platform.

I can show you ways around this problem, but first you should fix your 
implementation of Sum_Of_Divisors and get your program through the 
compiler. Once your program produces correct results for short chains 
we'll see if the Constraint_Error turns out to be a problem and what we 
can do about it.

You had a version of Sum_Of_Divisors that was correct but a bit 
inefficient. Then you replaced it with a version that's more efficient 
but incorrect. You need to fix it so that it always returns the same 
result as the first version you posted.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: Ada.Text_IO.Create
  2006-02-19  8:14               ` Ada.Text_IO.Create Martin Dowie
@ 2006-02-19 19:45                 ` Jeffrey R. Carter
  2006-02-19 21:49                   ` Ada.Text_IO.Create Larry Kilgallen
  0 siblings, 1 reply; 20+ messages in thread
From: Jeffrey R. Carter @ 2006-02-19 19:45 UTC (permalink / raw)


Martin Dowie wrote:

> That's the subprogram I'd prefer to have an extra "Create_If_Absent : 
> Boolean := False" parameter. And, yes, I know it control coupling (bad) 
> but IMHO, I would find the code cleaner.

I'm not sure I see the value of this. If you're opening for reading, a 
Create_If_Absent would create an empty file; End_Of_File would be true 
immediately. If you're opening for writing, aren't you going to overwrite the 
contents anyway? In that case, doesn't Create do what you want?

-- 
Jeff Carter
"Perfidious English mouse-dropping hoarders."
Monty Python & the Holy Grail
10



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

* Re: Ada.Text_IO.Create
  2006-02-19 19:45                 ` Ada.Text_IO.Create Jeffrey R. Carter
@ 2006-02-19 21:49                   ` Larry Kilgallen
  2006-02-20  6:09                     ` Ada.Text_IO.Create isaac2004
  0 siblings, 1 reply; 20+ messages in thread
From: Larry Kilgallen @ 2006-02-19 21:49 UTC (permalink / raw)


In article <fV3Kf.1931$VI6.308@newsread1.news.pas.earthlink.net>, "Jeffrey R. Carter" <spam@spam.com> writes:
> Martin Dowie wrote:
> 
>> That's the subprogram I'd prefer to have an extra "Create_If_Absent : 
>> Boolean := False" parameter. And, yes, I know it control coupling (bad) 
>> but IMHO, I would find the code cleaner.
> 
> I'm not sure I see the value of this. If you're opening for reading, a 
> Create_If_Absent would create an empty file; End_Of_File would be true 
> immediately. If you're opening for writing, aren't you going to overwrite the 
> contents anyway? In that case, doesn't Create do what you want?

Depending on your operating system, there is a difference between
creating a new version of a file for write and opening the existing
version for write.



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

* Re: Ada.Text_IO.Create
  2006-02-19 21:49                   ` Ada.Text_IO.Create Larry Kilgallen
@ 2006-02-20  6:09                     ` isaac2004
  2006-02-20  6:43                       ` Ada.Text_IO.Create Gautier
       [not found]                       ` <3stiv1l39144oscbs5he2v6h5uvfmli03q@4ax.com>
  0 siblings, 2 replies; 20+ messages in thread
From: isaac2004 @ 2006-02-20  6:09 UTC (permalink / raw)


alright i am getting very confused reading all these different posts
about what i should do, so i am going to list what needs fixed

Sum_Of_Divisors needs to be more efficient

function to write out of Sociable_Chain function

for loop to write correct arrays to chains.txt.

is this all correct.




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

* Re: Ada.Text_IO.Create
  2006-02-20  6:09                     ` Ada.Text_IO.Create isaac2004
@ 2006-02-20  6:43                       ` Gautier
       [not found]                       ` <3stiv1l39144oscbs5he2v6h5uvfmli03q@4ax.com>
  1 sibling, 0 replies; 20+ messages in thread
From: Gautier @ 2006-02-20  6:43 UTC (permalink / raw)


isaac2004:

> alright i am getting very confused reading all these different posts
> about what i should do, so i am going to list what needs fixed
> 
> Sum_Of_Divisors needs to be more efficient
> 
> function to write out of Sociable_Chain function
> 
> for loop to write correct arrays to chains.txt.
> 
> is this all correct.

Seems so.
What you need most is to think by yourself and work harder.
(for that, it's helpful to switch off the internet access for a
while...)
It will be very useful for your studies, especially in math.
Good luck!

G.



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

* Re: Ada.Text_IO.Create
       [not found]                       ` <3stiv1l39144oscbs5he2v6h5uvfmli03q@4ax.com>
@ 2006-02-21 22:15                         ` isaac2004
  2006-02-22  7:45                           ` Range error (was: Ada.Text_IO.Create) Björn Persson
  0 siblings, 1 reply; 20+ messages in thread
From: isaac2004 @ 2006-02-21 22:15 UTC (permalink / raw)


alright everybody who helped me thank you because i am near completion

i only have one more question. when i run my code for a length of 30 it
hits a range error, any help on how on fix it

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Integer_Text_Io;
use Ada.Integer_Text_Io;
procedure Assignment3 is

---------------------------------------------------------------------
   --| program that takes a range in the form of a minimum and maximum
number,
   --| and prompts for the length, to find all sociable chains inside
of the range
   --| to that desired chain
   --| Isaac Levin, Western Washington
   --| February 2006

---------------------------------------------------------------------------

   Start  : Natural;
   Stop   : Natural;
   Max    : Natural range 1 .. 30;
   Chains : File_Type;


function Sum_Of_Divisors (
         N : Integer)
     return Integer is
       C: Integer;
      Sum : Integer;

   begin
      Sum := 1;
      C:=2;
      while C**2 <= N loop --Number

         if N mod C = 0 then
            if C * 2 = N then
               Sum := Sum + C;
            else
               Sum := Sum + C + N / C;
            end if;
         end if;
         C := C + 1 ;
      end loop;
      return Sum ;

   end Sum_Of_Divisors;



   procedure Sociable_Chain (
         Start,
         Max   : Natural) is
      A : array (1 .. 30) of Natural;
      I : Natural                    := 2;

   begin
      A(1) :=Start; -- makes first number of array the prompt number
      A(2) := Sum_Of_Divisors (A(1));
      -- 2nd number in array is sum of divisors of prompt
      while I < Max and A (1) /= A(I) loop
         -- loop till length is reached and till A(I) = A(1)
         I:= I + 1;
         A(I) := Sum_Of_Divisors (A(I - 1));
      end loop;
      if A(1) = A(I) then
         for Index in 1 .. I loop
            Put(
               File  => Chains,
               Item  => A (Index),
               Width => 0);
            New_Line (File => Chains) ;
            Put(
               Item  => A (Index),
               Width => 0);
            New_Line ;
         end loop;
         New_Line (File => Chains) ;
         New_Line ;
      end if ;
   end Sociable_Chain;

begin


   Put(Item => "Please enter your starting integer.");
   Get(Item => Start);
   New_Line;
   Put(Item => " Please enter your ending integer.");
   Get(Item => Stop);
   New_Line;
   Put(Item => " Please enter the length of largest chain. ");
   Get(Item => Max);




   Create(
      File => Chains,
      Mode => Out_File,
      Name => "chains.txt");

   for N in Start .. Stop loop
      Sociable_Chain ( N, Max) ;
   end loop;

   Close (File => Chains) ;

end Assignment3 ; 
   

thank you




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

* Range error (was: Ada.Text_IO.Create)
  2006-02-21 22:15                         ` Ada.Text_IO.Create isaac2004
@ 2006-02-22  7:45                           ` Björn Persson
  2006-02-22  8:27                             ` Range error Björn Persson
  0 siblings, 1 reply; 20+ messages in thread
From: Björn Persson @ 2006-02-22  7:45 UTC (permalink / raw)


isaac2004 wrote:
> i only have one more question. when i run my code for a length of 30 it
> hits a range error, any help on how on fix it

OK, it turns out that you need far bigger numbers than the type Integer 
(or Natural for that matter) can handle. With the inputs 12000, 15000 
and 30, the program will encounter numbers at least as big as 
13606250238463924.

You will have to define an integer type of your own, with a large enough 
range, and use it for all those variables that will handle sums of 
divisors; not for the chain lengths of course.

This also means that you can't read or print these numbers with 
Ada.Integer_Text_IO. You will have to make your own instance of 
Ada.Text_IO.Integer_IO. Ada.Integer_Text_IO is a predefined instance of 
Ada.Text_IO.Integer_IO, instantiated with Integer. Now you'll have to 
instantiate Ada.Text_IO.Integer_IO with your own type instead.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

* Re: Range error
  2006-02-22  7:45                           ` Range error (was: Ada.Text_IO.Create) Björn Persson
@ 2006-02-22  8:27                             ` Björn Persson
  0 siblings, 0 replies; 20+ messages in thread
From: Björn Persson @ 2006-02-22  8:27 UTC (permalink / raw)


Bj�rn Persson wrote:
> With the inputs 12000, 15000 
> and 30, the program will encounter numbers at least as big as 
> 13606250238463924.

Oops, that was a mistake. The biggest number encountered was "only" 
925898859564000, but that's still far too big for the predefined Integer 
type.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



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

end of thread, other threads:[~2006-02-22  8:27 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-17 23:13 invalid parameter list isaac2004
2006-02-18  6:02 ` jimmaureenrogers
     [not found] ` <tfadv19pqf17h3prdhquk3e5f30j6vhdhl@4ax.com>
2006-02-18  8:04   ` isaac2004
2006-02-18 14:34     ` Björn Persson
2006-02-18 21:19       ` isaac2004
2006-02-18 23:31         ` Björn Persson
2006-02-18 23:34           ` Martin Dowie
2006-02-19  0:06             ` isaac2004
     [not found]               ` <dmlfv11b4qagaf3gec853k9on191576gqc@4ax.com>
2006-02-19  4:06                 ` isaac2004
     [not found]                   ` <uavfv1pjdorpim6lpu8sjfdlq4lmmn1jes@4ax.com>
2006-02-19 16:21                     ` Björn Persson
2006-02-19 15:54               ` Björn Persson
2006-02-19  0:15             ` Ada.Text_IO.Create (was: invalid parameter list) Björn Persson
2006-02-19  8:14               ` Ada.Text_IO.Create Martin Dowie
2006-02-19 19:45                 ` Ada.Text_IO.Create Jeffrey R. Carter
2006-02-19 21:49                   ` Ada.Text_IO.Create Larry Kilgallen
2006-02-20  6:09                     ` Ada.Text_IO.Create isaac2004
2006-02-20  6:43                       ` Ada.Text_IO.Create Gautier
     [not found]                       ` <3stiv1l39144oscbs5he2v6h5uvfmli03q@4ax.com>
2006-02-21 22:15                         ` Ada.Text_IO.Create isaac2004
2006-02-22  7:45                           ` Range error (was: Ada.Text_IO.Create) Björn Persson
2006-02-22  8:27                             ` Range error Björn Persson

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