music |
OSdata.com |
vectors
summary
This subchapter looks at vectors or one dimensional arrays.
free computer programming text book projecttable of contents
|
music |
OSdata.com |
This subchapter looks at vectors or one dimensional arrays.
free computer programming text book projecttable of contents
|
This subchapter is a stub section. It will be filled in with instructional material later. For now it serves the purpose of a place holder for the order of instruction.
Professors are invited to give feedback on both the proposed contents and the propsed order of this text book. Send commentary to Milo, PO Box 1361, Tustin, California, 92781, USA.
This subchapter looks at vectors or one dimensional arrays.
All arrays in LISP must be declared before they are used.
LISP ARRAY(x) LISP function that takes three or more arguments of type name, type, and dimnsions, and creates an array of that name, type, and dimensions.
(ARRAY SIMPLE T 1024)
The example creates a LISP vector array named SIMPLE that can contain any arbitrary s-expressions. The array elements are numbered 0 to 1,023.
The storage of a signle dimension array in memory is obvious: a linear sequence of memory locations (possibly with padding for instruction alignment purposes).
1 | 1 | 0x1000 | 1 | |||
3 | 13 | 0x1008 | 13 | |||
5 | 10,000 | 0x1010 | 10,000 |
Stanford CS Education Library This [the following section until marked as end of Stanford University items] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.
C has the usual facilities for grouping things together to form composite types-- arrays and records (which are called structures).
The simplest type of array in C is one which is declared and used in one place. There are more complex uses of arrays which I will address later along with pointers. The following declares an array called scores to hold 100 integers and sets the first and last elements. C arrays are always indexed from 0. So the first int in scores array is scores[0] and the last is scores[99].
int scores[100];
scores[0] = 13; // set first element
scores[99] = 42; // set last element
Its a very common error to try to refer to non-existent scores[100] element. C does not do any run time or compile time bounds checking in arrays. At run time the code will just access or mangle whatever memory it happens to hit and crash or misbehave in some unpredictable way thereafter. Professional programmers language. The convention of numbering things 0..(number of things - 1) pervades the language. To best integrate with C and other C programmers, you should use that sort of numbering in your own data structures as well.
Stanford CS Education Library This [the above section] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.
ABSOLUTE VALUE |B. APL monadic function that when applied to a vector returns a vector with each element having the absolute value of the corresponding element of the source vector.
ADDITION A+B. APL dyadic function that adds all of the corresponding element of the vectors A and B and places the results into vector A. If B is a scalar, this fucntion adds the scalar B to every element in vector A and places the results into vector A.
AND ∧(x) APL dyadic function that returns the logical AND of two vectors. Form is A∧B. This function can be used with two vectors to logically AND all of the corresponding elements of the two vectors; or with a vector and a scalar to logically AND the scalar to every element of the vector with the value of the scalar. Each element of the operation must intiially have the value of one (1) or zero (0). One is TRUE and zero is FALSE.
PostScript aload(x) PosScript array operator that places on the stack all of the elements of an array followed by the array itself.
LISP ARRAY(x) LISP function that takes three or more arguments of type name, type, and dimnsions, and creates an array of that name, type, and dimensions.
PL/I DIM(x, n) Pl/I built-in function that returns the size of an array. The data item x is the name of the array to be examined. The data item n is the number of the dimension of the specififed array for which the extent is to be returned. The function is most efficient if data item n is of type FIXED BINARY (15). It is an error if the array has less than n dimensions or if n is zero or less or if the array x is not currently allocated. For a vector, the value of n should be 1.
PL/I LBOUND(x, n) Pl/I built-in function that returns the lower bound for the specififed dimension of an array. The data item x is the name of the array to be examined. The data item n is the number of the dimension of the specififed array for which the extent is to be returned. The function is most efficient if data item n is of type FIXED BINARY (15). It is an error if the array has less than n dimensions or if n is zero or less or if the array x is not currently allocated. For a vector, the value of n should be 1.
PL/I HBOUND(x, n) Pl/I built-in function that returns the upper bound for the specififed dimension of an array. The data item x is the name of the array to be examined. The data item n is the number of the dimension of the specififed array for which the extent is to be returned. The function is most efficient if data item n is of type FIXED BINARY (15). It is an error if the array has less than n dimensions or if n is zero or less or if the array x is not currently allocated. For a vector, the value of n should be 1.
The following code example shows how to use a DO loop to safely access all of the elements of a one dimensional array (vector) without going past the boundaries (beginning and end) of the array. This can also be used to handle the possibility that the declaration of the size of an array might change.
DO I = LBOUND(ARRAY-NAME,1) TO HBOUND(ARRAY_NAME,1);
X = ARRAY_NAME(I);
code that goes in this loop
END; /* DO LOOP */
PL/I SUM(x) Pl/I built-in function that returns the total sum of all of the values of the elements in an arithmetic array x. Note that the IBM PL/I-F compiler converts all array values to floating point to perform the operation and returns a result in floating point. This may create small rounding errors for commercial programmers needing dollar and cents accuracy.
PL/I PROD(x) Pl/I built-in function that returns the total product of all of the values of the elements in an arithmetic array x. The PROD function is always carried out in floating point arithmetic. This may create small rounding errors for commercial programmers needing dollar and cents accuracy.
Ah, variable names.ÊLength is not a virtue in a name; clarity of expression is.ÊA global variable rarely used may deserve a long name, maxphysaddr say.ÊAn array index used on every line of a loop neednt be named any more elaborately than i.Ê Saying index or elementnumber is more to type (or calls upon your text editor) and obscures the details of the computation.ÊWhen the variable names are huge, its harder to see whats going on.ÊThis is partly a typographic issue; consider
for(i=0 to 100)
array[i]=0 ;
vs.
for(elementnumber=0 to 100)
array[elementnumber]=0;
The problem gets worse fast with real examples.ÊIndices are just notation, so treat them as such. Rob Pike, Notes on Programming in C, February 21, 1989
Coding example: I am making heavily documented and explained open source code for a method to play music for free almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).
View music player in action: www.musicinpublic.com/.
Create your own copy from the original source code/ (presented for learning programming).
return to table of contents
free downloadable college text book
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
previous page | next page |
Tweets by @osdata |
free computer programming text book projectBuilding a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming. If you like the idea of this project, Supporting the entire project: If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church) free downloadable college text book on computer programming. |
This web site handcrafted on Macintosh computers using Tom Benders Tex-Edit Plus and served using FreeBSD .
UNIX used as a generic term unless specifically used as a trademark (such as in the phrase UNIX certified). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.
Names and logos of various OSs are trademarks of their respective owners.
Copyright © 2010, 2011 Milo
Created: December 7, 2010
Last Updated: February 27, 2011
return to table of contents
free downloadable college text book
previous page | next page |