1

So I have been given an assignment to read in a file put the numbers into two matrices, multiply the matrices, and finally put the output into a .txt file.

I have never used Ada before and I figured it would be a good challenge. I am stuck in trying to determine the bounds for the two separate arrays.

This is what I currently have:

currentSpread := I;
g := Ada.Numerics.Generic_Complex_Elementary_Functions.Sqrt(I);
while J < g loop

  if(I mod J = 0) THEN
     if(currentSpread > ((I/J - J)/2)) THEN
        currentSpread := ((I/J - J)/2);
        arrayBounds := J;
     end if;

  end if;

  J := J + 1;
end loop;

The problem I am having is with the sqrt function. I want to find the factors for the best bounds of the matrix multiplication and this was the only way that I thought to implement it.

The error I am getting is:

invalid prefix in selected component "Ada.Numerics.Generic_Complex_Elementary_Functions"

Thanks a lot for any help.

--Update

Full Code as requested:

with Ada.Text_IO;use Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Numerics.Generic_Complex_Elementary_Functions;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Numerics.Complex_Elementary_Functions;
with Ada.Numerics.Generic_Complex_Types;

procedure Main is

   dataFile : File_Type;
   resultFile : File_Type;
   value : Integer;
   I : Integer := 0;
   J : Integer;
   currentSpread : Integer;
   arrayBounds : Integer;
   g : Integer;

begin

  Ada.Text_IO.Open(File => dataFile, Mode => Ada.Text_IO.In_File, Name =>"C:\Users\Jeffrey\Desktop\data.txt");


   while not End_Of_File(dataFile) loop
      Ada.Integer_Text_IO.Get(File => dataFile, Item => value);
      Ada.Integer_Text_IO.Put(Item => value);
      Ada.Text_IO.New_Line;
      I := I + 1;
   end loop;

   Ada.Integer_Text_IO.Put(I);
   I := I/2;
   J := 1;
   currentSpread := I;
   g := Ada.Numerics.Generic_Complex_Elementary_Functions.Sqrt(I); 
   while J < g loop

      if(I mod J = 0) THEN
         if(currentSpread > ((I/J - J)/2)) THEN
            currentSpread := ((I/J - J)/2);
            arrayBounds := J;
         end if;

      end if;

        J := J + 1;

   end loop;

   declare
     type newArray is array(Integer range <>, Integer range<>) of Integer;
      X : Integer := J;
      Y : Integer := I/J;
      Arr1 : newArray(1..Y, 1..X);
      Arr2 : newArray(1..X, 1..Y);
      finAnswer : newArray(1..X, 1..X);
   begin

      for z in 1 .. X loop
         for k in 1 .. Y loop
            Ada.Integer_Text_IO.Get(File => dataFile, Item => value);
            Arr1(z, k) := value;
         end loop;
      end loop;

      for z in 1 .. Y loop
         for k in 1 .. X loop
            Ada.Integer_Text_IO.Get(File => dataFile, Item => value);
            Arr2(z, k) := value;
         end loop;
      end loop;

      for l in 1 .. X loop
         for m in 1 .. Y loop
            for n in 1 .. X loop
               finAnswer(l, n) := finAnswer(l, n) + Arr1(l, n)* Arr2(n, m);
            end loop;
         end loop;
      end loop;
   end;



   Ada.Text_IO.Close(File => dataFile);
end Main;

I am using the square root exclusively to figure out the factors of a number nothing else. How I have it set up now is it will go up to the square root and then it will take the smallest spread of the factors. I do not care about rounding errors or anything else if it isn't a perfect square it can round either way.

Thanks.

Silence
  • 13
  • 1
  • 5
  • I have 'g' and 'I' currently set as Integer looking back over it I will either have to cast the answer from the sqrt to Integer or initialize g as a float and cast it for the while loop. – Silence Nov 05 '14 at 23:52
  • Perhaps you're missing a `with` clause? Please update your question to show a complete compilable (apart from the "invalid prefix" error) source file. And are you sure you want *complex* square root? Are you trying to read **N** numbers into a sqrt(**N**) by sqrt(**N**) matrix? What if **N** isn't a perfect square? – Keith Thompson Nov 06 '14 at 00:01
  • Additionally I tried a variety of Sqrt functions to see if I could get it to work but I kept getting the same error. So I figured I was just doing something wrong. – Silence Nov 06 '14 at 00:37
  • I think you're doing more things wrong than will fit into an answer. As the name implies, `Ada.Numerics.Generic_Complex_Elementary_Functions` is a *generic* package; you can't use it without instantiating it. Using complex types for this problem makes no sense. You're reading multiple integer values into `value`, but each value you read is simply clobbered by the next one. Does the assignment actually require to you read **N** numbers and compute **√N** to determine the size of a square matrix? – Keith Thompson Nov 06 '14 at 00:51
  • You need to store the numbers *somewhere* as you read them, which means you need an array to store them in *before* you know how many there are -- unless you can store the number of items at the beginning of the input file, or you read the input file twice. Knowing how the assignment is actually worded would be *very* helpful. – Keith Thompson Nov 06 '14 at 00:53
  • It doesn't require me to use the sqrt but I figured it would be easier done that way. I am not looking for efficiency with this assignment or really good programming practices, they are appreciated, but right now my main goal is getting the program to work. I am indeed reading the file once to get the number of elements in it. I am then reading it again to store the values in the arrays. I am sure you just answered my main problem in that I can not just use it without instantiating it. – Silence Nov 06 '14 at 01:05
  • Looking back over the assignment one of the bounds for the array is actually required to be 5 so I don't even have to put that part in >.< Thanks for your help anyway. You still helped me out a great bit in understanding Ada a little more. What was that bit about you saying that me reading in from the file was just jumbling value up though will that cause an unexpected error later maybe? – Silence Nov 06 '14 at 01:06
  • In your loop, you read successive integer values into your variable `value`. You never store them anywhere else. Every time you read a new number, it overwrites whatever you had previously stored in `value`, and the information is lost. Since you know what the size of the matrix needs to be, you can just define a 5-by-5 array and read numbers into successive elements of that array. – Keith Thompson Nov 06 '14 at 01:09
  • I just noticed you said *one* of the bounds is required to be 5, not both. We still don't know enough about your requirements to help you in any meaningful way, but if you still need help you might post a new question. – Keith Thompson Nov 06 '14 at 16:37
  • Yeah I ended up figuring out another solution altogether for the problem. I think I got an A but won't know until next week. Thanks for the help anyway. – Silence Nov 07 '14 at 00:39

1 Answers1

6

Generic_Complex_Elementary_Functions is a generic package. It cannot be used directly. That is why the compiler gives you an error on this line:

Ada.Numerics.Generic_Complex_Elementary_Functions.Sqrt(I);

To use it, you have to instantiate the generic Generic_Complex_Types with the floating-point type you want to use, and then instantiate Generic_Complex_Elementary_Functions with an instance of Generic_Complex_Types. Fortunately, you don't have to go through all that if you're willing to use the built-in type Float; the language provides Ada.Numerics.Complex_Elementary_Functions for you, that uses Float as the floating-point type, or Ada.Numerics.Long_Complex_Elementary_Functions that uses Long_Float if your compiler vendors support it.

However, I don't think you want to use Complex anything. That deals with complex numbers, and I doubt that you want to use those. Use Ada.Numerics.Elementary_Functions (or Long_Elementary_Functions), which deal with real numbers.

Finally, even this isn't going to work:

Ada.Numerics.Elementary_Functions.Sqrt(I)

if I is an integer, because the argument type needs to be a Float. You'll have to use a type conversion.

Ada.Numerics.Elementary_Functions.Sqrt(Float(I))
ajb
  • 31,309
  • 3
  • 58
  • 84