I know how the two dimensions array works in pascal, [rows, columns].
How the three or more dimensions works? Please help, thanks.
a[rows, columns, ???]
A one dimensional array is declared as array[lowIndex..HighIndex] of <type>, as in
intArray: array[0..9] of integer;
A two dimensional array is simply an array containing an array, as in:
twoDim: array[0..9,0..9] of integer;
You reference it using the indexes:
twoDim[0,0] := 1;
twoDim[0,1] := 2;
A three dimensional array simply extends the two dimensional, as in this declaration:
threeDim: array[0..9,0..9,0..9] of integer;
You also use it with the indexes, in the same way:
threeDim[0,0,0] := 1;
threeDim[9,0,1] := 12;
As far as what to call the individual dimensions, use names that mean something related to the purpose they serve. For instance, if I was keeping an array of hourly readings for something each day over the period of a week for four weeks, I could declare the array as:
Readings: array[1..4,1..7,1..24] of integer;
I could then declare variables to name the indexes:
var
Week, Day, Hour; Integer;
I could then reference the three dimensions of the array easily. To initialize them all to zero, for instance, I could do something like this:
for Week := 1 to 4 do
for Day := 1 to 7 do
for Hour := 1 to 24 do
Readings[Week, Day, Hour] := 0;
Related
i have a function that puts a number in a var (var1). if var1 = 3 then create 3 new variables and assign a value for later send that values in an out type. I don't know how can i do, my code is something like this where "pos" acts like an INSTR() that i know its each 13 chars. And "newvar||var1" is because i want my vars names be newvar1, newvar2, newvar3, etc.:
FOR n IN 1..var1
LOOP
pos NUMBER(4):= 0;
newvar||var1 NUMBER(3);
newvar||var1 := SUBSTR(TO_NUMBER(numberInsideString), 1, 1+pos);
pos := pos+13;
END LOOP;
My question is, how a person who really know PLSQL would do that, im learning PLSQL please help. thank you.
What you want to try to do suggests that you need a one dimensional array in order repeatedly to assign new values for each iteration within the loop. One of the types in OWA package such as vc_arr(composed of VARCHAR2 data type values) is handy to define an array.
Coming to the code, you can start with moving the variables into the declaration section, and write such a simple code as a sample exercise
SET serveroutput ON
DECLARE
pos INT := 0;
numberInsideString VARCHAR2(100):='as12df34sdg345apl8976gkh4f11öjhöh678u7jgvj';
newvar OWA.vc_arr;
BEGIN
FOR n IN 1..3
LOOP
newvar(n) := SUBSTR(numberInsideString, 1, 1+pos);
pos := pos+5;
Dbms_Output.Put_Line(newvar(n));
END LOOP;
END;
/
a
as12df
as12df34sdg
I need to construct a matrix; a number of columns and rows are also in the first row of the matrix, I'll make an example so its more clearer.
4 3
1 2 3
5 6 7
9 10 8
1 11 13
Where m=4 (number of rows) and n=3 (number of columns)
This is an example of a text file. Is something like this even possible?
Program Feb;
const
max=100;
type
Matrix=array[1..max,1..max] of integer;
var datoteka:text;
m,n:integer;
counter:integer;
begin
assign(datoteka,'datoteka.txt');
reset(datoteka);
while not eoln(datoteka) do
begin
read(datoteka, m);
read(datoteka, n);
end;
repeat
read eoln(n)
until eof(datoteka)
write (m,n);
end.
My code isn't a big help, cause I don't know how to write it.
First, have a look at the code I wrote to do the task, and then look at my explanation below.
program Matrixtest;
uses
sysutils;
var
NoOfCols,
NoOfRows : Integer;
Source : TextFile;
Matrix : array of array of integer;
FileName : String;
Row,
Col : Integer; // for-loop iterators to access a single cell of the matrix
Value : Integer;
begin
// First, construct the name of the file defining the matrix
// This assumes that the file is in the same folder as this app
FileName := ExtractFilePath(ParamStr(0)) + 'MatrixDef.Txt';
writeln(FileName); // echo it back to the screen so we can see it
// Next, open the file
Assign(Source, FileName);
Reset(Source);
read(Source, NoOfRows, NoOfCols);
writeln('Cols: ', NoOfCols, 'Rows: ', NoOfRows);
SetLength(Matrix, NoOfCols, NoOfRows);
readln(source); // move to next line in file
// Next, read the array data
for Row := 1 to NoOfRows do begin
for Col := 1 to NoOfCols do begin
read(Source, Value);
Matrix[Col - 1, Row - 1] := Value;
end;
end;
// Display the array contents
for Row := 1 to NoOfRows do begin
for Col := 1 to NoOfCols do begin
writeln('Row: ', Row, ' contents', Matrix[Col - 1, Row - 1]);
end;
end;
Close(Source); // We're done with the file, so close it to release OS resources
readln; // this waits until you press a key, so you can read what's been displayed
end.
In your program, you can use a two-dimensional array to represent your matrix. Free Pascal supports multi-dimensional arrays; see https://wiki.lazarus.freepascal.org/Multidimensional_arrays for more information.
This is a complex task, so it helps to know how to do more basic things like reading an array of a size known at compile-time from a text file.
The wrinkle in this task is that you are supposed to read the dimensions (numbers of rows and columns) of the matrix at run-time from the file which contains the matrix's contents.
One inefficient way to do this would be to declare the matrix array with huge dimensions, larger than anything you would expect in practice, using the type of array declaration in the Wiki page linked above.
A better way is to use dynamic arrays, whose dimensions you can set at run-time. To use this, you need to know:
How to declare a dynamic array in Free Pascal
How to set the dimensions of the array at run-time, once you've picked them up from your matrix-definition file (hint: SetLength is the way to do this)
The fact that a Free Pascal dynamic array is zero-based
The easiest way of managing zero-based arrays is to write your code (in terms of Row and Column variables) as if the matrix were declared as array[1..NoOfRows, 1..NoOfColumns] and subtract one from the array indexes only when you actually access the array, as in:
Row := 3;
Column := 4;
Value := Matrix[Row - 1, Column - 1];
I'm very new to Pascal and still learning much. I have to write a code that :
Takes input of a string
Split the string into two characters each (Snippet)
Use the Snippet to get an index from an array
Transpose the Snippet to a certain value
If Index + Transpose is larger than the length of the Array, return nothing
If not, append the transposed Snippet to a result string
Return the transposed string
I can only write 1 through 3, the rest is still a blur for me. Helps are appreciated.
(And I also want to improve it without many for loops. Any thoughts?)
program TransposeString;
var
melody : Array[1..24] of String[2] = ('c.', 'c#', 'd.', 'd#', 'e.', 'f.', 'f#', 'g.', 'g#', 'a.', 'a#', 'b.', 'C.', 'C#', 'D.', 'D#', 'E.', 'F.', 'F#', 'G.', 'G#', 'A.', 'A#', 'B.');
songstring, transposedstring : String;
transposevalue : byte;
function Transpose(song : String; transposevalue : byte): String;
var
songsnippet : String[2];
iter_song, iter_index, index : byte;
begin
for iter_song := 1 to length(song) do
begin
if iter_song mod 2 = 0 then continue;
songsnippet := song[iter_song] + song[iter_song + 1]; //Split the string into 2 characters each
for iter_index := 1 to 24 do
begin
if melody[iter_index] = songsnippet then
begin
index := iter_index; //Get Index
break;
end;
end;
//Check Transpose + Index
//Transpose Snippet
//Append Snippet to Result String
end;
end;
begin
readln(songstring);
readln(transposevalue);
transposedstring := transpose(songstring, transposevalue);
writeln(transposedstring);
end.
As a starter for you to work from, rather than just spoon-feeding an answer:
You have the index of the snippet (note) in index. Assuming the notes are in order you need to return the note from the array positions above it, so
result := result + melody[iter_index + transposevalue];
You need to check the length of the array before trying to read from it, otherwise it'll crash (step 5). This is just an if statement.
I wouldn't worry too much about for loops - 2 deep nesting isn't that bad. If you wanted to split it out a bit then GetTransposedNote(const note:string): string; could be split out as a new function.
Things you may want to think about are:
What if you can't find the note in the array?
Do you want to be case-sensitive
What if the input string has an odd number of characters?
You are most of the way there already, though.
I am new to Smalltalk and am taking a class init currently. One of the problems we were assigned requires creating a new Class "HeavyMatrix", which is called like this —
aMatrix := HeavyMatrix new.
Notice that there is no initial size assignment. The size is supposed to be undetermined upon creation. Then, the class adds values to the matrix with the following method —
aMatrix setRow:1 column:2 value:7.
All the values in the matrix which have not been declared in this way default to 0. The new Class HeavyMatrix is not supposed to subclass anything other than Object (i.e. not Matrix or Array). Can any Smalltalkers help me out? There are some other methods the class must contain but I think I can figure everything else once I am able to initialize the class. Thanks!
What can I suggest you… First of all you can just used ordered collections and inside setRow:column:value: if current column size is smaller that required than add what is missing an OrderedCollection instances. The same with desired row size (add 0s). The implementation will be something like this:
initialize
rows := OrderedCollection new
setRow: aRow column: aColumn value: aValue
| row |
rows size < aRow ifTrue: [
(aRow - rows size) timesRepeat: [rows add: OrderedCollection new]].
row := rows at: aRow.
row size < aColumn ifTrue: [
(aColumn - row size) timesRepeat: [row add: 0]].
row at: aColumn put: aValue
getRow: aRow column: aColumn
aRow <= rows size ifTrue: [
|row|
row := rows at: aRow.
aColumn <= rows size ifTrue: [^ row at: aColumn]].
^ 0
But more efficient way to do that is to implement a sparse matrix I think. You have a lot of structures described at wiki. Please note, that this matrices are only efficient when you have a low amount of elements compering to the size of the matrix itself. But if you'll check even the easiest implementation, like storing a coords-value data in dictionary, than it will suite you a lot:
initialize
dict := Dictionary new
setRow: aRow column: aColumn value: aValue
dict at: aRow#aColumn put: aValue
getRow: aRow column: aColumn
dict at: aRow#aColumn ifAbsent: [0]
In an xquery expression, I have obtained a set of values within a for-expression, and one value is in a separate variable.
Now, I want to subtract the single value from first value of the list, and then subtract consecutive members of the list from each other-- and in the resulting set of difference values, I want to obtain the min/max values...
The query upto now looks like this--
let $value1:= 1998
let $rows_citations:=
$doc//div[#id="patent_citations"]
/div[#id="patent_citations_v"]
/table[#class="rel_patent"]
/tbody/tr[1]
/following-sibling::tr
for $pos in $rows_citations/position()
let $date2_c := customfn:dateconverter1($rows_citations[$pos]/td[3])
Now the subtraction I want is between first value of date2_c and value 1, and after that between consecutive members of date2_c... And from the resulting list I want the min/max values... How do I go about doing this?
I am esp. confused about creating a new list variable that stores all the differences, esp. when we are already inside a for loop, and are iterating over each value of a list (via variable date2_c)
I. This XQuery 3.0 query (which is also a pure XPath 3.0 expression):
let $pVal := 1,
$vList := (2,4,7,11,16),
$vList2 := ($pVal, subsequence($vList, 1, count($vList)-1)),
$vSubtactedList :=
map-pairs(function($m as xs:integer, $n as xs:integer) as xs:integer
{
$m - $n
},
$vList,
$vList2
)
return
(min($vSubtactedList), max($vSubtactedList))
produces the wanted result the minimum and maximum values from the list of subtractions:
1 5
II. XQuery 1.0 solution:
let $pVal := 1,
$vList := (2,4,7,11,16),
$vList2 := ($pVal, subsequence($vList, 1, count($vList)-1)),
$vSubtactedList :=
for $i in 1 to count($vList)
return
$vList[$i] - $vList2[$i]
return
(min($vSubtactedList), max($vSubtactedList))
This again produces the same correct result:
1 5