I'm approaching the Ada language. I wrote this simple program that loops over an array and increments every single value, but the compiler gives me an error of type:
hello.adb:8:07: left hand side of assignment must be a variable
The program in question is this:
with Ada.Text_IO;
procedure hello is
type myArrayDefinition is array (1 .. 10) of integer;
myArray : constant myArrayDefinition := (1 => 3, others => 2);
begin
for A in 1 .. 10 loop
myArray(A) := myArray(A) + 1;
end loop;
end hello;
Could anyone help me to understand the problem?
You can't modify the value of a constant. There are no special problems related to modifying the values of arrays.
Related
I have these 10 numbers (one in each line) in a Text File and I need them to be sorted in a chronological order starting with the highest number. I wrote a code which works just fine, but the problem is that the code isn't flexible, because once I add another number to the Text File it won't work since the code is set to sort 10 numbers only...this is due to my array of integers which is supposed to read the values before the sorting process starts, but won't allow me to add a variable to the array's properties so it will be able to read and sort any-size text file...I know there has to be a way of making a program which can sort any-size file of this structure, just please tell me how I could improve my code. (If you think my way isn't too efficient, it's because that's my homework from high school and I need to use these arrays to implement a bubblesort).
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
numbers, sortednumbers : TextFile;
count : integer=0;
number : array[1..10] of integer;
I : integer;
Procedure Swap(var X, Y : Integer);
var
Temp : integer;
begin
Temp := X;
X := Y;
Y := Temp;
end;
procedure Assign;
var I : Integer;
begin
reset(numbers);
for I := 1 to count do ReadLn(numbers, number[I]);
end;
procedure BubbleSort;
var I, J : integer;
begin
for I := 2 to count do
begin
for J := count downto I do
if (number[J] > number[J - 1]) then
Swap(number[J - 1], number[J]);
end;
end;
begin
AssignFile(numbers, 'Numbers.txt');
AssignFile(sortednumbers, 'Sorted-Numbers.txt');
Reset(numbers);
While not EoF(numbers) do
begin
ReadLn(numbers);
Inc(count);
end;
Reset(numbers);
ReWrite(sortednumbers);
Assign;
BubbleSort;
For I := 1 to count do writeln(sortednumbers, number[I]);
CloseFile(numbers);
CloseFile(sortednumbers);
end.
Use a dynamic array. This is an array that can change the number of elements it holds.
Instead of declaring:
number : array[1..10] of integer;
instead skip the bounds declaration:
Number : array of integer;
Then, before you start using it, set its length:
SetLength(Number, 10);
Once you are done, free the memory by setting it to have length 0:
SetLength(Number, 0);
Note that:
Indexes of a dynamic array start from 0, not 1. That is, Number[0] is the first element and Number[9] is the tenth. This is quite common in programming but can be confusing if it's not something you've come across before.
There are other 'quality' changes you can make to your code too. As commenter David Heffernan said, stop using global variables. I would also suggest using try/finally for resource allocation and cleanup, and using streams (and another link, wrap this over TFileStream) instead of the old-style file IO you're currently doing. Also, consider your variable names - Number for an array of numbers is odd - why not add the S and call it Numbers, for example?
That might be a bit much all at once, so go slowly in small steps, save and backup often (preferably into source control), and have fun!
And if it had not been a school project:
function Comparer(List: TStringList; index1, index2: integer): integer;
begin
try
Result:= StrToInt(List[index1]) - StrToInt(List[Index2]);
except
raise Exception.Create('Your file does not contain numbers');
end;
end;
function SortNumbers
var
lStringList: TStringlist;
begin
lStringList := TStringlist.create;
try
lStringList.LoadFromFile('Numbers.txt');
lStringList.CustomSort(Comparer);
lStringList.SaveToFile('Sorted-Numbers.txt');
finally
lStringList.Free;
end;
end;
However the Delphi RTL uses Quicksort, not Bubblesort.
I've been trying to make a program that will ask user to input elements of an array and then use that array to make a new one whose elements would be every 2nd element of inputted array. This is what I was writing:
program Keanu;
uses crt;
type Arr=array of integer;
var n,i:integer;
A,C:Arr;
begin
writeln('--Enter desired length of array--');
readln(n);
setlength(A,n);
setlength(C,n);
writeln('Elements of array A:');
for i:=1 to n do
readln(A[i]);
writeln('Elements of array C are:');
i:=1;
while (i<=n) do
begin
c[i]:=a[i];
i:=i+2;
end;
write('C = {');
for i:=1 to n do
begin
if c[i]=0 then continue else
begin
write(c[i],' ');
end;
end;
write('}');
readln;
end.
But as you can notice this is far from efficient way to make this program do the job.
First, because my new array will contain blank/empty elements(zeros) which I simply ignored with continue statement and I dont want to do that if possible.
Second,I have problem when inputting an even number for array length.Last element of new array in output window is very small,negative number and he shouldn't be there at all.I know this has to do something with my counter "i" crossing into "undefined" indexes of array.
I also tried replacing while loop with some variations of:
for i:=0 to n do
c[i]:=a[2*i-1] ;
Which is more elegant way but I still get , besides desired result , those large numbers , again because of crossing limits of an array.I suspect this has to be done with proper steps of how new array is made and moving those elements next to each other with no blank elements.
So, if anyone can give me some solutions of how to get these loop steps and limits into right order and make efficient,shortest algorithm, and preferably without using while loop if possible ,and definitely without ignoring those blank elements of new array.
Declaring variables by one character A, C: array of integer is a bad practice. The name of variable will tell you about it's type and meaning. (highly recommended)
And read about Hungarian notation, then you'll understand why TArr and arrA instead of Arr and A. Not necessary, but recommended. (also recommended)
As for the second arrC, you can use operator div to make it twice smaller than the first array arrA.
You can also crete a new variable that will be n div 2, in order not to change n div 2 in the whole code (good practice):
nDivided := n div 2;
SetLength(arrA,n);
SetLength(arrC, nDivided);
This will make your program quite a bit efficient: you'll save n - (n div 2) * sizeof(integer) bytes.
Here are both cases (for even and odd N). No "blank elements" and no "very small, negative number in the end of new array(-32768)".
N is 6 N is 5
Elements of array A: Elements of array A:
arrA[1]=1 arrA[1]=1
arrA[2]=2 arrA[2]=2
arrA[3]=3 arrA[3]=3
arrA[4]=4 arrA[4]=4
arrA[5]=5 arrA[5]=5
arrA[6]=6
Elements of array C are: Elements of array C are:
arrC[1]=2 arrC[1]=2
arrC[2]=4 arrC[2]=4
arrC[3]=6
Anyway, here is code (a little changed). Also not very good (efficent) but it does exactly what you need.
program WorkingWithArrays;
uses crt;
type
TArr = Array of Integer;
var
i, n: integer;
arrA, arrC: TArr;
begin
writeln('Enter the length of array:');
readln(n);
SetLength(arrA,n);
SetLength(arrC, n div 2);
writeln('Elements of array A:');
for i:=1 to (n) do begin
arrA[i]:=i;
writeln('#> arrA[',i,']=', arrA[i]);
end;
writeln('Elements of array C are:');
i:=1;
while (i <= n div 2) do
begin
arrC[i]:=arrA[i+i];
i:=i+1;
end;
for i:=0 to (n div 2) do begin
if arrC[i]=0 then continue else begin
writeln('#> arrC[',i,']=', arrC[i]);
end;
end;
readln;
end.
// compiled here: http://www.compileonline.com/compile_pascal_online.php
I am learning Ada at the moment and the current lesson is on arrays. Consider the following program;
procedure Main is
type T_Bool is array (Boolean) of Integer;
type T_Intg is array (Integer range 1 .. 2) of Integer;
A1 : T_Bool := (others => 0);
A2 : T_Intg := (others => 0);
begin
-- None of these statements work
A2 := A1;
A2 (1 .. 2) := A1 (false .. true);
A2 := A1 (Boolean'First .. Boolean'Last);
end Main;
According to the Adacore university tutor, it is possible to copy values from one array to another as long as the lengths are the same. In the code snippet, why can you not assign arrays with the same size but different indexing methods, despite the fact that the length is the same?
What is the correct way to copy this across? Is it a case of looping through all indexes in the Boolean type range and copying the corresponding array index across or is there another clever way of doing it?
The tutorial says, on slide 3, that "all arrays are (doubly) typed", which means that - as usual for different types - you can't assign between your two array types; so yes, you need a loop.
There are "clever" ways using, for example, unchecked conversion; but, really, don't go there!
It is possible to copy arrays without any explicit conversion if they are of the same type and length with only the starting index differing:
procedure Test is
type T is array (Positive range <>) of Integer;
A : T(1 .. 10);
B : T(21 .. 30);
begin
A := B;
end Test;
Otherwise it is possible to assign arrays of different type with explicit conversion, but apart from having the same dimensionality (and more, see ARM 4.6) the index types also have to be convertible. Boolean is an enumeration type which is not convertible to Integer.
Explicit conversion with convertible index types would look like this:
procedure Main is
type TMod is mod 10000;
type T_Mod is array (TMod range 5000 .. 5001) of Integer;
type T_Intg is array (Integer range 1 .. 2) of Integer;
A1 : T_Mod := (others => 0);
A2 : T_Intg := (others => 0);
begin
A2 := T_Intg(A1);
end Main;
So yes, it would look like you need to copy elements in a loop in your example.
Thanks for your answers guys, it is definitely useful information. It goes even deeper than I thought. In the quiz part of the tutorial, I learned that even this is an error
procedure Main is
type T1 is array (Integer range 1 .. 10) of Integer;
type T2 is array (Integer range 1 .. 10) of Integer;
A1 : T1;
A2 : T2;
begin
A1 := (others => 0);
-- Cannot do this
A2 := A1;
end Main;
Types A2 and A1 may well be defined in the same way, but Ada considers them unrelated and therefore incompatible. It would be the equivalent in C++ of doing
typedef int MyType1;
typedef int MyType2;
MyType1 MyVar1;
MyType2 MyVar2;
// Error - Cannot assign differing types,
// despite them being semantically the same
MyVar2 = MyVar1;
In this particular case (or when there are only few values for index), it is possible to assign in once with an aggregate.
A2 := (1 => A1 (False),
2 => A1 (True));
The complete example:
https://gcc.godbolt.org/z/josscfPzh
I am trying to use the 'Last attribute with a 2D array in Ada, but I can't seem to find the correct syntax to do so.
I know that if I have a 1D array/vector I can use A'last or A'last(n) where n is the nth dimension. But if I do the following
type UnconstrainedArray_2D is array (Integer range <>, Integer range <>) of Integer;
function temp(tempIn : in Integer;
Table : in UnconstrainedArray_2D) return Integer is
tempTable : UnconstrainedArray_2D(0..tempIn, 0..tempIn);
begin
for i in 0..tempTable'last(1) loop
for j in 0..tempTable'last(2) loop
tempTable(i, j) := Table(i,j);
end loop;
end loop;
end temp;
I get the following compile time error:
Storage_Error stack overflow (or erroneous memory access)
So what am I doing wrong?
I am using GNAT Pro 6.4.1 on Linux.
I'd be very surprised if you got a compile-time Storage_Error on that code.
I've grabbed a copy of your code and modified it as follows; it compiles without error using GNAT (gcc-4.4):
procedure Array_2D is
type UnconstrainedArray_2D is array (Integer range <>, Integer range <>) of Integer;
function temp(tempIn : in Integer;
Table : in UnconstrainedArray_2D) return Integer is
tempTable : UnconstrainedArray_2D(0..tempIn, 0..tempIn);
begin
for i in 0..tempTable'last(1) loop
for j in 0..tempTable'last(2) loop
tempTable(i, j) := Table(i,j);
end loop;
end loop;
return 42; -- added this
end temp;
begin
null;
end Array_2D;
(Note that I had to add the missing return statement in temp.)
Your syntax for the 'Last attribute (not "command") is correct, but since Ada arrays can have arbitrary lower and upper bounds, it's better to use the 'Range attribute instead:
for i in tempTable'Range(1) loop
for j in tempTable'Range(2) loop
tempTable(i, j) := Table(i,j);
end loop;
end loop;
As for the Storage_Error exception, that could easily happen at run time (not compile time) if you call your temp function with a very large value for tempIn. Remember that it has to allocate enough space to hold tempIn**2 Integer objects. Presumably you've also created another UnconstrainedArray_2D object to be passed in as the Table parameter.
It's conceivable that the compiler itself could die with a Storage_Error exception, but I don't see anything in your code that might cause that.
Show us a complete (but small) program that demonstrates the problem you're having, along with the exact (copy-and-pasted) error message. Please distinguish clearly between compile-time and run-time errors.
Your tempTable might have a range of 0..tempIn, but you don't know what range your Table has.. They could be of different length, too.
You would have to check that the length is the same and then use relative indices, like this:
function temp(tempIn : in Integer;
Table : in UnconstrainedArray_2D) return Integer is
tempTable : UnconstrainedArray_2D(0..tempIn, 0..tempIn);
begin
if tempTable'Length (1) /= Table'Length (1) or else
tempTable'Length (2) /= Table'Length (2)
then
raise Constraint_Error; -- or something else
end if;
for i in 0 .. tempTable'Length (1) - 1 loop
for j in 0 .. tempTable'Length (2) - 1 loop
tempTable(tempTable'First (1) + i, tempTable'First (2) + j) :=
Table(Table'First (1) + i, Table'First (2) + j);
end loop;
end loop;
end temp;
that way it is ensured that both tables are same length and all indices are valid.
If your tempTable is allowed to be smaller than Table, simply adjust the length check to >. The indices would still be valid.
I don't see an actual value for tempIn set. If the value for tempIn coming into the function temp has not been properly initialized or explicitly set, then the value in tempIn could be anything and probably not something you would like.
I was thinking of a default value. (probably shouldn't post when I am not feeling well :-)
I am trying to get better at functional programming. As a start I am planning on trying out with couple of languages like Pascal, Scheme, ML etc. First I started with Pascal. I am trying to insert user input into a integer array in pascal and then get them reverse.
1 program ReverseList;
2
3 var
4 i: Integer;
5 k: Integer;
6 a: array[1..100] of Integer;
7 begin
8 i := 0;
9 repeat
10 writeln('Enter a number');
11 readln(k);
12 if k > -1 then
13 i := i + 1;
14 a[i] := k;
15 until(k < 0);
16 for i := 1 to i do
17 writeln(a[i]);
18 end.
In past I have mostly been a java developer so I was so custom to using all the lists thats available. Also ideally I was wondering if I can build a list where I can iterate over the list based on the number of elements in that list.
It would be great if anyone could point me on the direction of good tutorials in functional programming as well as syntax on above mentioned programming languages.
There are several problems with your program:
The array is not initialized.
There is no input checking, both i=0 and i>100 result in an illegal array index.
The array index and the value are the same, is that correct?
You only write the first 10 numbers (but you use a different index, which is certain to be out of range).
The output is not in reverse.
There are also several pascal tutorials.
By the way, Pascal isn't a functional language. So if you really want to learn a functional language, you better try another one (like Lisp, Ml or probably F#).
It was a good practice and I managed to figure a solution for this. I am sure there are better ways to do, and also this doesn't look like I am using the functionalities of functional programming. But if anyone wants to provide a better solution please do so,
{author: Null-Hypothesis}
program ReverseList;
var
i: Integer; {integer to keep the array length}
k: Integer; {user input value}
a: array[1..100] of Integer; {array to store the user inputs}
begin
i := 0;
repeat {iterate until user input is negative or number of inputs exceed array size}
writeln('Enter a number or enter negative value to exit the program.');
readln(k);
if(k > -1) and (i < 100) then {check for negative value and size of the array}
begin
i := i + 1; {increase array index}
a[i] := k {assign value to array}
end
else
break; {exit if array size exceed the limit of array}
until(k < 0);
writeln;
{Printing the user input before the reversing the list}
writeln('Original order of the list');
for i := 1 to i do
writeln(a[i]);
writeln;
{Printing the reverse list}
writeln('Reversed List');
for i := i downto 1 do {decrement array index}
writeln(a[i]);
writeln('Bye!!!');
end.
Happy Coding, off to the next language...