http:^^www.cs.wisc.edu^~tick^cs302^week6.html
来自「This data set contains WWW-pages collect」· HTML 代码 · 共 406 行
HTML
406 行
Date: Wed, 11 Dec 1996 22:34:12 GMTServer: NCSA/1.5Content-type: text/htmlLast-modified: Fri, 11 Oct 1996 19:15:43 GMTContent-length: 10723<HTML><HEAD><TITLE>CS 302 Section 70 Lecture Notes - Week 9</TITLE></HEAD><BODY><H2><!WA0><!WA0><!WA0><A HREF="http://www.cs.wisc.edu/~tick/cs302.html#text" ><!WA1><!WA1><!WA1><IMG SRC="http://www.cs.wisc.edu/~tick/icons/arrowleft.gif" WIDTH=15 HEIGHT=15></A> Lecture Notes - Week 6</H2><HR><DL> <DT>Topic: <DD>One-dimensional arrays. One-dimensional array arguments. Parallel arrays.<P> <DT>Text: <DD>7.1 - 7.7, 7.9<P> <DT>Notes: <DD> <HR> <CENTER> <H3>Arrays</H3> </CENTER> <H4>Simple Variables</H4> <UL> <LI>So far we have used <EM>simple variables</EM> to store values. <LISTING> INTEGER SCORE REAL PRICE LOGICAL ANSWER </LISTING> <LI>Simple variables can only store a <EM>single</EM> value; i.e one integer, one real number or one logical value. <LI>To store another value we must declare a new variable with a different name. <LISTING> INTEGER SCORE1, SCORE2, SCORE3 </LISTING> </UL> <H4>Array Variables</H4> <UL> <LI>Arrays store several related values under a <EM>single</EM> variable name. <LI>All values stored in an array are of the same type, called the <EM>base type</EM>. <LI>Easier to store several values in an array than declaring separate variables for each. </UL> <H4>Declaring Array Variables</H4> <UL> <LI>Specify the <EM>name</EM> of the array, the <EM>base type</EM>, and the number of values to store - called the <EM>size</EM> of the array. <LISTING> basetype arrayname(arraysize) INTEGER SCORES(3) REAL PRICES(1000) LOGICAL ANSWRS(10) </LISTING> <LI>Array variables are declared together with simple variables. <LISTING> PROGRAM FOO REAL COST INTEGER SCORES(3), SUM </LISTING> <LI>Commonly use a <EM>constant</EM> (i.e. Fortran <TT>PARAMETER</TT> statement) to declare the size of an array. <LISTING> INTEGER MAXSIZ PARAMETER (MAXSIZ=3) REAL SCORES(MAXSIZ) </LISTING> <LI><TT>SCORES</TT> array variable stores three <TT>INTEGER</TT>'s under a <EM>single</EM> variable name. </UL> <H4>Referencing Arrays</H4> <UL> <LI>To access a particular value in an array you specify the array <EM>name</EM> and the <EM>subscript</EM> of the value (i.e. its position in the array). <LISTING> arrayname(subscript) </LISTING> <LI>The first value in an array has subscript 1, the last has subscript <EM>arraysize</EM>. <LISTING> SCORES(1) - first element SCORES(2) - second element SCORES(3) - last element </LISTING> <LI>Array variables can be used anywhere you would use a normal simple variable. <LISTING> PRINT *, 'Your score is ', SCORES(1) BAR = SCORES(1)*10 + COS(SCORES(2)) READ(1, 100, END=150) SCORES(3) </LISTING> <LI>Store values in an array using the <EM>assignment statement</EM>, just like simple variables <LISTING> arrayname(subscript) = value SCORES(1) = 3 SCORES(2) = SCORES(1)*6 </LISTING> </UL> <H4>Array Subscripts</H4> <UL> <LI>Array subscripts can be <EM>literals</EM>, <EM>variables</EM> or <EM>expressions</EM>. The value of the expression determines which element is accessed. <LISTING> INTEGER N, SCORES(3) N = 2 SCORES(N) = SCORES(N*4-5)-SCORES(1) </LISTING> <LI>Value of array subscript must be in the range 1 ... <EM>arraysize</EM>. <LI>Array subscript is an <TT>INTEGER</TT> expression enclosed by parentheses ().<P> <STRONG>Right</STRONG>: <LISTING> SCORES(N), SCORES(N*4-5) </LISTING> <STRONG>Wrong</STRONG>: <LISTING> SCORES(-13), SCORES(5), SCORES[2], SCORES(2.3) </LISTING> <LI><STRONG>Important</STRONG>: The type of the array variable, the base type, and the subscript are very different. <LISTING> REAL PRICES(1000) PRICES(N) = ... Variable Type -------------------------------------------------- PRICES ARRAY (all of the prices together) PRICES(N) REAL N INTEGER </LISTING> </UL> <H4>Arrays and DO Loops</H4> <UL> <LI>Arrays are commonly processed using a <TT>DO</TT> loop. <LISTING> INTEGER MAXSIZ, TOTAL, COUNT PARAMETER (MAXSIZ=100) REAL SCORES(MAXSIZ) C Read in all the scores DO COUNT=1, MAXSIZ READ *, SCORES(COUNT) END DO C Add up all the scores TOTAL = 0 DO COUNT=1, MAXSIZ TOTAL = TOTAL + SCORES(COUNT) END DO C Print out all the scores DO COUNT=1, MAXSIZ PRINT *, SCORES(COUNT) END DO STOP END </LISTING> Try doing this without arrays! <LI>The <TT>DO</TT> loop counter <TT>COUNT</TT> is used as the array subscript to <EM>sequentially</EM> access the values one at a time. </UL> <HR> <CENTER> <H3>Reading Arrays</H3> </CENTER> <H4>#1: DO Loop</H4> <UL> <LI>An array can be "filled up" by reading in the values in a <TT>DO</TT> loop. <LISTING> DO I=1, 100 READ *, SCORES(I) END DO </LISTING> <LI>Values <STRONG>must</STRONG> be typed in on different lines because the <TT>READ</TT> statement is re-executed each time. </UL> <H4>#2: Read Entire Array</H4> <UL> <LI>Can read in the entire array at once. <LISTING> READ *, SCORES </LISTING> <LI>Values may be entered on different lines or all on the same line. </UL> <H4>#3: Implied DO Loop</H4> <UL> <LI>Can read in any part of an array from the same line using an <EM>implied DO loop</EM>. <LISTING> READ *, (SCORES(I), I=50, 100) </LISTING> <LI>Equivalent to a <TT>DO</TT> loop inside the <TT>READ</TT> statement, so everything is read off the same line. </UL> <HR> <CENTER> <H3>Writing Arrays</H3> </CENTER> <H4>#1: DO Loop</H4> <UL> <LI>The values in an array can be printed on different lines using a <TT>DO</TT> loop. <LISTING> DO I=1, 100 PRINT *, SCORES(I) END DO </LISTING> </UL> <H4>#2: Print Entire Array</H4> <UL> <LI>Can print in the entire array at once on one line. <LISTING> PRINT *, SCORES </LISTING> </UL> <H4>#3: Implied DO Loop</H4> <UL> <LI>Can print in any part of an array on the same line using an <EM>implied DO loop</EM>. <LISTING> PRINT *, (SCORES(I), I=50, 100) </LISTING> </UL> <HR> <CENTER> <H3>Implied DO Loops</H3> </CENTER> <UL> <LI>Implied <TT>DO</TT> loops are loops within a <TT>READ</TT>, <TT>WRITE</TT> or <TT>PRINT</TT> statement. <LISTING> READ *, (array(count), count=start, end, inc) READ *, (SCORES(I), I=1, MAXSIZ, 1) PRINT *, (PRICES(NUM), NUM=1, 1000) </LISTING> <LI>Can read/write more than one array with an implied <TT>DO</TT> loop. <LISTING> READ *, (SCORES(I),GRADES(I), I=1, 100) </LISTING> reads <LISTING> SCORES(1), GRADES(1), SCORES(2), GRADES(2) ... </LISTING> <LI>Can use the <EM>end-of-file</EM> specifier with implied <TT>DO</TT> loops. <LISTING> READ(1,*,END=100) (SCORES(I), I=1, 100) </LISTING> <LI>Will abort implied <TT>DO</TT> loop prematurely if run out of data. </UL> <HR> <CENTER> <H3>Format Statements and Arrays</H3> </CENTER> <UL> <LI>Arrays can be read/written/printed using FORMAT statements. <LISTING> INTEGER SQRS(5) DO 100 I=1, 5 SQRS(I) = I**2 END DO PRINT 15, (SQRS(I), I=1, 5) 15 FORMAT(I3, I3, I3, I3, I3) </LISTING> prints <LISTING> 1 4 9 16 25 </LISTING> <LI>Can <EM>re-use</EM> the edit specifiers in the <TT>FORMAT</TT> statement. But if there are more values than edit specifiers then loop back to the first specifier and <STRONG>start a new line</STRONG>. <LISTING> PRINT 15, (MASS(I), I=1, 5) 15 FORMAT(I3, I3) </LISTING> prints <LISTING> 1 4 9 16 25 </LISTING> <LI>Use edit multipliers to avoid starting new lines. <LISTING> 15 FORMAT(5I3) </LISTING> </UL> <HR> <CENTER> <H3>Array Arguments</H3> </CENTER> <UL> <LI>Arrays can be passed into and out of user-defined functions and subroutines as <EM>array arguments</EM>. <LI>As with passing simple variables as arguments, array variables must first be declared in the main program. <LI><STRONG>Important</STRONG>: Pass both the array <EM>name</EM> and the array <EM>size</EM> as arguments. Must re-declare array inside the function or subroutine. <LISTING> PROGRAM TEST INTEGER MAXSIZ PARAMETER (MAXSIZ=10) INTEGER SCORES(MAXSIZ) ... PRINT *, GETMAX(SCORES, MAXSIZ) STOP END INTEGER FUNCTION GETMAX (LIST, SIZE) C Finds the highest value in the list C Declare arguments INTEGER SIZE, LIST(SIZE) ... RETURN END </LISTING> </UL> <H4>Input Array Arguments</H4> <UL> <LI>As with simple variable arguments, <EM>input</EM> array arguments should not be re-assigned.<P> <STRONG>Right:</STRONG> <LISTING> MAX = LIST(1) IF (MAX .GT. LIST(N)) ... </LISTING> <STRONG>Wrong:</STRONG> <LISTING> LIST(N) = ... </LISTING> <LI>Whole arrays can be passed in as input arguments <LISTING> PRINT *, GETMAX(SCORES, MAXSIZ) </LISTING> or individual values can be passed as input arguments, just like simple variables. <LISTING> PRINT *, FACT(SCORES(3)) </LISTING> </UL> <H4>Output Array Arguments</H4> <UL> <LI>If an array is an <EM>output</EM> argument (i.e. passed back to the main program) then the array values can and <STRONG>must</STRONG> be changed. <LISTING> ... CALL ZERO(SCORES, MAXSIZ) ... SUBROUTINE ZERO(LIST, SIZE) C Initialize an list to zero values. C Declare arguments INTEGER SIZE, LIST(SIZE) C Declare local variables INTEGER COUNT DO COUNT = 1, SIZE LIST(COUNT) = 0 END DO RETURN END </LISTING> <LI>Whole arrays can be passed back as output arguments <LISTING> CALL ZERO(SCORES, MAXSIZ) </LISTING> or individual values can be passed as output arguments, just like simple variables. <LISTING> CALL SORT(SCORES(1), SCORES(3)) </LISTING> </UL></DL></BODY><HR><ADDRESS><H5>Copyright © 1996 Modified from<!WA2><!WA2><!WA2><A HREF="http://www.cs.wisc.edu/~bestor/bestor.html">Gareth S. Bestor</A> (<!WA3><!WA3><!WA3><A HREF="mailto:bestor@cs.wisc.edu">bestor@cs.wisc.edu</A>). Last modified October 11, 1996.</H5></ADDRESS>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?