-1

I need to create a number of functions that deal with matrices of arbitrary size. I am familiar with the declare syntax used here, but this is for an university assignment, and my professor told me that using 'declare' there is overkill. I can't find anything relevant online, any help?

Basically I'm looking to get the matrix size via keyboard and then work with the resulting matrix, I'm stuck with the declare Currently I have:

type myMatrix is array (Natural range <>, Natural range <>) of Integer; 
type myVector is array (Natural range <>) of Integer;       

And I use it as:

procedure Lab1 is
 begin
 declare A, B: myVector(1..5):=
 (3, 14, 15, 92, 6);

which doesn't allow to specify the size at runtime, and as:

  declare 
    int1:Integer:=generate_random_number(50)+2; 
    int3:Integer:=generate_random_number(50)+2; -- +2 so we don't get length/size 0 or 1
    X, Y:myVector(1..int1):=(others=>generate_random_number(20));
    MT:myMatrix(1..int1, 1..int3):=(others =>(others=>generate_random_number(10))); -- 'others' used for all the unmentioned elements, http://rosettacode.org/wiki/Array_Initialization
    MS:myMatrix(1..int3, 1..int1):=(others =>(others=>generate_random_number(10))); 
    F3_result:myMatrix(1..int1, 1..int1);  
  begin 
    F3_result:=F3(X, Y, MT, MS);
  end;

which uses a declare block. I might need to use the resulting array later on, and as I understand here F3 is a local variable, and therefore can't be reused?

Are there any other ways?

Community
  • 1
  • 1
pchr8
  • 115
  • 7
  • 3
    In what sense is it overkill? Of course there are other ways, including "new" and unchecked_deallocate, but you could simply refactor out the "declare" block as a call to a procedure (declared locally, if you don't want to pass everything in as parameters) or a function returning F3_result, and see if he notices... –  Nov 11 '15 at 13:04
  • 1
    This [example](http://stackoverflow.com/a/12923999/230513) contrasts `declare` and `new`. – trashgod Nov 11 '15 at 17:44
  • 1
    As an aside, generate_random_number seems to generate integers, which may not be appropriate with array bounds of type Natural. Aka have you thought about what happens if generate_random_number returns -50, when declaring an array of bounds 1 .. -50 ? – NWS Nov 11 '15 at 19:17
  • @trashgod : in the heap allocation part of the example you linked, there is no explicit deallocation : does this introduce a memory leak or is there some behind-the-scenes magic happening? –  Nov 12 '15 at 11:21
  • @BrianDrummond: no magic; I've updated the answer to provide some broader context regarding heap/pool storage. – trashgod Nov 12 '15 at 15:12

1 Answers1

6

I agree with your teacher that putting a declare just after a begin will in general show that the declare block is unneeded (the only exception I can think of is because you want to handle exceptions that might arise as part of the declaration of the variables within the enclosing subprogram).

So you can just remove both begin and declare at the start of your subprogram, and that should work the same.

manuBriot
  • 2,755
  • 13
  • 21
  • 1
    aha! if *that*'s what he was getting at ... yes, simply hoisting the declarations above `begin` is the correct approach and a new block is overkill. But if you need to read keyboard input after `begin` to get the parameters, that would not be possible. –  Nov 12 '15 at 11:14