Frage deutsch
~~~~~~~~~~~~~~
Was ist ein Feld? Wie geht man damit um?
Question English
~~~~~~~~~~~~~~
What is an array? How do I use arrays in my programs?
Antwort 1
~~~~~~~~~~
[ von Thomas Antoni, 11.1999 - 14.4.2003 ]
.
Ein Feld (engl. "Array") ist eine Ansammlung von Variablen gleichen Typs, die
in einem zusammenhängenden Speicherbereich abgelegt werden und sich bequem in
Schleifen schreiben und lesen lassen.
Die einzelnen Variablen werden "Feldelemente" genannt und sind
durchnummeriert: Jedem Feldelement ist eine "Feldelement-Nummer", der sogenannte
"Index" zugeordnet.
Bevor man ein Feld benutzen kann, muss man es deklarieren.
Mit
DIM Alter%(100)
wird z.B. ein Feld namens "Alter%" vom Typ INTEGER mit 101 Feldelementen
(Indices 0 ... 100) deklariert bzw "dimensioniert", das z.B. das Lebensalter von
101 Personen enthalten kann. Die Indices beginnen also normalerweise mit
"0".
Felder sind hervorragend geeignet zum Speichern und Bearbeiten von
Messdatenreihen, Tabellen, Vektoren und Matritzen und ähnlichen gegliederten
Datensammlungen.
Einzelne Feldelemente lassen sich über ihren Index bequem und effizient auch
in Schleifen bearbeiten. Die folgende Befehlssequenz zeigt beispielsweise alle
im Feld Alter% gespeicherten Variablen auf dem Bildschirm an:
FOR i% = 0 TO 100
PRINT Alter%(i%)
NEXT i%
Auch mehrdimensionale Felder sind möglich. Mit
DIM C#(9,4)
wird z.B. ein zweidimensionales Feld vom Typ DOUBLE (doppelt lange
Gleitpunktzahl) deklariert (man sagt auch "dimensioniert"), das 10 Blöcke (Index
0...9) mit je 5 Feldelementen enthält. Der Zugriff auf das 2 Element des 3.
Blockes erfolgt so:
C# (2,3) = 86 .
Die einzelnen Blöcke eines mehrdimensionalen Feldes können auch
unterschiedliche Datentypen haben. In diesem Falle spricht man von
"Verbundfeldern" oder "Feldern anwenderderfinierten Typs" - in anderen
Programmiersprachen meist "Strukturen" oder "Structures" genannt. Diese werden
nicht mit DIM, sondern mit "TYPE ... END" deklariert
Weitere Informationen zu Feldern und Strukturen findest Du in meinem
QBasic-Kochbuch, das auf http://www.qbasic.de in der Rubrik
"QBasic -> Tutorials" zum Download bereitsteht. Dort werden auch solche Dinge
behandelt wie
• statische/ dynamische Felder (siehe auch
den FAQ-Eintrag "Wie entferne
ich Felder und Variablen wieder aus dem Speicher?" )
• Felder wieder aus dem Seicher löschen
(siehe auch den FAQ-Eintrag "Wie
entferne ich Felder und Variablen wieder aus dem Speicher?" )
• maximal mögliche
Feldlängen
• Übergabe von Feldern an
SUBs/FUNCTIONs
• Dasselbe Feld im Hauptprogramm und
verschiedenen SUBs/Functions verwenden
Antwort 2
~~~~~~~~~
[ von Helium m.beckah*gmx.de auf dem QB-Forum,
30.12.01 ]
.
Ein Datenfeld (Array) dient dazu, um mehrere Variablen des selben Typs in
einem zusammenhängenden Speicherbreich abzulegen.
Bevor du es verwenden kannst, musst du es wie folgt
deklarieren:
DIM Feld (groesse) as Typ
vobei groesse eine Zahl kleiner 32768 ist und "Typ" einem den standardmäßigen
QBasic-Datentypen (Integer, Long, Single, double, ...) entspricht.
z.B.
Dim Feld(10) as integer
For i = 0 to 10
Feld(i) = int(RND*100)
next
For i = 0 to 10
Print Feld(i)
next
Antwort 3
~~~~~~~~~~~~~~~~~
[ von ZaPPa ( dc-ware*onlinehome.de ) per mail, 23.2.03 ]
.
Ich habe einem Anfänger gerade den DIM-Befehl erklärt und bin über meine
Erklärung extrem stolz :)) Sie ist selbst für Kleinkinder und Omas verständlich.
Deshalb habe ich mir überlegt, Dir das ganze für Deine FAQ zu schicken! Also
bitteschön:
Und jetzt zur Erklärung des DIM-Befehls : Also es ist immer hilfreich, wenn
Du dir eine Variable als Schublade vorstellst in die Du einen Wert
hineinlegst.
Mit DIM Variable(5) erstellst Du sozusagen einen SCHRANK mit 6 Schubladen, die
mit 0...5 beschriftet sind. Das heißt, Du kannst einer Variable 6 verschiedene
Werte zuweisen. Die Einzelvariablen ("Feldelemente") heißen dann
Variable(0)...Variable(5).
Du kannst auch mehreren untereinanderliegenden Schubladenreihen anlegen. Das
ist dann ein dreidimensionales Feld. Mit DIM Variable (5, 3) erstellst Du zum Beispiel
einen Schrank (coole Metapher oder?) mit 4 Schubladenreihen (0...3), die je 6
Schubladen (0...5) enthalten.
Du kannst beliebig nahezu viele Reihen erstellen. DIM Variable(5, 3, 12, 24) ist also auch möglich. Das wäre dann ein vierdimensionales Feld.
Die an den DIM-Befehl angehängte Option "AS STRING" gibt an, dass diese
Variable eine Zeichenketten-Variable (oder Stringvariable) ist und erspart das
Eintppen des $-Zeichens nach dem Variablennamen.
DIM Variable(5) AS STRING * 8 legt zum
Beispiel ein Zeichenketten-Feld mit 6 Feldelementen (Schubladen) an. In jedes
der Feldelemente kann ein 8 Zeichen langer Text gespeichert werden.
So das isset! Das müsste verständlich sein. Ich hoffe, es kommt in die
Monster-FAQ
Rock this SHIT!!! DC-WARE'S STILL WORKIN'!
MfG David Wosnitza aKa ZaPPa
Answer 4
~~~~~~~~~
What is an array?
An array is a list of data item inmemory that all have the same name and
whose elements are differentiated by a subscript. In simple terms, an array can
be anything that is agroup of related numbers such as prices, phone
numbers,catalog order numbers and the list goes on and on.
Why do we need arrays ?From the beginning of the computer, programmers like
to "cheat". This means that they try to make the computer do as much as
possible, which makes the code for a program much shorter. Arrays can be huge,
and what person wants to input each individual variable. This is why we need to
"cheat" and use arrays to our advantage.
Answer 5
~~~~~~~~~
Can you explain an array to me?
Sure, I suppose. An array is a set of variables that can be accessed by the
same name, but with a different number (you can therefore use a FOR loop or
whatnot to acess the contents of every element). Try this example to get you on
your
feet:
DIM SHARED Values%(100)
FOR i% = 0 TO 100
Values%(i) = INT(RND * 1000)
NEXT i%
FOR i% = 0 TO 100
PRINT Values%(i)
NEXT i%
Answer 6
~~~~~~~~~
[ by ANTHRAX ( http://ddi.digital.net/~triplej/calcpage.htm ; triplej*digital.ne
) ]
*** What is an array?
An array is a list of data items in memory that all have the same name and
whose elements are differentiated by a subscript.
In simple terms, an array can be anything that is a group of related numbers
such as prices, phone numbers, catalog order numbers and the list goes on and
on.
*** Why do we need them?
From the beginning of the computer, programmers like to "cheat". This means
that they try to make the computer do as much as possible, which makes the code
for a program much shorter.
Arrays can be huge, and what person wants to input each individual variable.
This is why we need to "cheat" and use arrays to our advantage.
*** Sample code
This is a sample for the variable (avar)
CLS
PRINT "DEMO FOR ARRAYS"
FOR avar=1 to 100
INPUT "What is the product number?"; prodnum(avar):PRINT
INPUT "What is the product desciption(name)?"; prodscr$(avar):PRINT
INPUT "are there any more products:(Y/N)"; ans$
ans$ = UCASE$(LEFT$(ans$,1))
IF (ans$ <> "Y") THEN
EXIT FOR
END IF
NEXT avar
PRINT "hey, you made it"
PRINT prodscr$(1); tab(20); prodnum(1)
PRINT prodscr$(2); tab(20); prodnum(2)
END
*** What is the ARRAY ELEMENT in the above code?
avar is the array variable. This is simply a name that is given to the array.
The important information is held in the array number or element.
1 to 100 signifies how many prodnum and prodscr there are. Towards the end of
the program, we used this number to take out the 1st and 2nd inputs and print
them on the screen.
*** What is a PARALLEL ARRAY?
A parallel array is an array that has two or more shared informations. The
program uses a parallel array between prodnum and prodscr$. For example
prodnum(1) = 409 and prodscr$(1)= Huffy Bike. These two correspond to each
other. In your product database or ordering form, the Huffy bike is ordered as
number 409. Do you see how they are parallel. As a final statement, the
parallel arrays must have the same number of elements(1 to x) and must have
related value(depending on the users need."
*** What is a DYNAMIC ARRAY?
A dynamic array is used for constantly changing data. This is handled in a
different format. Dynamic arrays are another section. Do not ask me how to use
them, because I don't do anything with graphics.
submitted by ANTHRAX
contact him at http://ddi.digital.net/~triplej/calcpage.htm
or e-mail me at triplej*digital.ne
[ The QBasic-MonsterFAQ --- Start Page: www.antonis.de/faq ]