RAPTOR Syntax and Semantics - Arrays Array variable - Array variables are used to store many values (of the same type) without having to have many variable names. Instead of many variables names a count-controlled loop is used to gain access (index) the individual elements (values) of an array variable. RAPTOR has one and two dimensional arrays of numbers. A one dimensional array can be thought of as a sequence (or a list). A two dimensional array can be thought of as a table (grid or matrix). To create an array variable in RAPTOR, use it like an array variable. i.e. have an index, ex. Score[1], Values[x], Matrix[3,4], etc. All array variables are indexed starting with 1 and go up to the largest index used so far. RAPTOR array variables grow in size as needed. The assignment statement GPAs[24] ← 4.0 assigns the value 4.0 to the 24th element of the array GPAs. If the array variable GPAs had not been used before then the other 23 elements of the GPAs array are initialized to 0 at the same time. i.e. The array variable GPAs would have the following values: 1 2 3 4… Array variables in action- Arrays and count-controlled loop statements were made for each other. Notice in each example below the connection between the Loop Control Variable and the array index! Notice how the Length_Of function can be used in the count-controlled loop test! Notice that each example below is a count-controlled loop and has an Initialize, Test, Execute, and Modify part (I.T.E.M)! Assigning values to an array variable Reading values into an array variable Writing out an array variable’s values Computing the total and average of an array variable’s values Index ← 1 Index ← 1 Index ← 1 Total ← 0 Loop Loop Loop Index ← 1 PUT “The value of the array at position “ + Index + “ is “ + GPAs[Index] Loop GPAs[Index] ← 4.0 “Enter the GPA of student “” + Index + “: “ GET GPAs[Index] Index >= 24 Index >= 24 Index >= Length_Of (GPAs) Index ← Index + 1 Index ← Index + 1 Index ← Index + 1 Total ← Total + GPAs[Index] Index >= Length_Of(GPAs) Index ← Index + 1 … 23 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4.0 The initialization of previous elements to 0 happens only when the array variable is created. Successive assignment statements to the GPAs variable affect only the individual element listed. For example, the following successive assignment statements GPAs[20] GPAs[11] ← ← 1.7 3.2 would place the value 1.7 into the 20th position of the array, and would place the value 3.2 into the 11th position of the array. i.e. GPAs[20] ← 1.7 GPAs[11] ← 3.2 1 2 3 4… … 23 24 Initialize the elements of a two dimensional array (A two dimensional array requires two loops) Row ← 1 Loop Average ← Total / Length_Of(GPAs) Find the largest value of all the values in an array variable Find the INDEX of the largest value of all the values in an array variable Highest_GPA ← GPAs[1] Highest_GPA_Index ←1 Index ← 1 Index ← 1 Loop Loop GPAs[Index] > Highest_GPA GPAs[Index] >= GPAs[Highest_GPA_Index] Column ← 1 Loop 0 0 0 0 0 0 0 0 0 0 3.2 0 0 0 0 0 0 0 0 1.7 0 0 0 4.0 An array variable name, like GPAs, refers to ALL elements of the array. Adding an index (position) to the array variable enables you to refer to any specific element of the array variable. Two dimensional arrays work similarly. i.e. Table[7,2] refers to the element in the 7 th row and 2nd column. Individual elements of an array can be used exactly like any other variable. E.g. the array element GPAs[5] can be used anywhere the number variable X can be used. The Length_Of function can be used to determine (and return) the number of elements that are associated with a particular array variable. For example, after all the above, Length_Of(GPAs) is 24. Matrix[Row, Column] ← 1 Column >= 20 Column ← Column + 1 Highest_GPA ← GPAs[Index] Highest_GPA_Index ← Index Index >= Length_Of(GPAs) Index >= Length_Of(GPAs) Index ← Index + 1 Index ← Index + 1 PUT “The highest GPA is “ + Highest_GPA¶ PUT “The highest GPA is “ + GPAs[Highest_GPA_Index] + “ it is at position “ + Highest_GPA_Index¶ Row >= 20 Row ← Row + 1

Jagged arrays Implemented as arrays of arrays , 4 index type index lower bound index upper bound address address , 3 , 3 index type index type index lower bound index lower bound index upper bound index upper bound address address , 7 , 7 index type index type index lower bound index lower bound index upper bound index upper bound address address , 4 , 4 index type index type index lower bound index lower bound index upper bound index upper bound address address , 5 , 5 index type index type index lower bound index lower bound index upper bound index upper bound address address 24

Reading List 1. 2. 3. 4. 5. 6. 7. 8. 9 10 11. 12 13 Rivers E, Nguyen B, Havstad S, Ressler J, Muzzin A, Knoblich B, Peterson E, Tomlanovich M;Early Goal-Directed Therapy Collaborative Group. Nugyen HB, Rivers EP, Havstad S, Knoblich B, Ressler JA, Muzzin AM, Tomlanovich MC:Critical care in the emergency department:Aphysiologic assissment and outcome evaluation.Acad Emerg Med. 2000 Dec;7(12):1354-61. Shoemaker WC, Wo CC, Yu S, Farjam F, Thangathurai D:Invasive and noinvasive haemodynamic monitoring of acutely ill sepsis and septic shock patients in the emergency department. Eur J Emerg Med. 2000 Sep;7(3):169-75. Truog RD, Cist AF, Brackett SE, Burns JP, Curley MA, Danis M, DeVita MA, Rosenbaum SH, Rothenberg DM, Sprung CL, Webb SA, Wlody GS, Hurford WE. Recommendations for end-of-life in the intensive care unit:The Ethics Committee of the Society of Critical Care Medicine. Crit Care Med. 2001 Dec;29(12):2332-48. Sprung CL, Geber D, Eidelman LA, Baras M, Pizov R, Nimrod A, Oppenheim A, Epstein L, Cotev S. Evaluation of triage decisions for intensive care admission.Crit Care Med. 1999 June;27(6):1073-9. Metcalfe MA, Sloggett A, McPherson K. Mortality among appropriately referred patients refused admission to intensive care units. Lancet. 1997 July 5;350(9070):7-11. Morales IJ, Peters SG, Afessa B. Hospital mortality rate and length of stay in patients admitted at night to the intensive care unit. Crit Care Med. 2003 Marcgl31(3)L858-63, Diringer MN, Edwards DF. Admission to a neurologic/neurosurgical intensive care unit is associated with reduced mortality rate after intracerebral hemorrhage. Crit Care Med. 2001 Mar;29(3):635-40. Afessa B. Triage of patients with acute gastroinestional bleeding for intensive care unit admission based on risk factors for poor outcome. J Clin Gastroenterol. 2000 Apr;30(3):281-5. .Morales IJ, Peters SG, Afessa B. Hospital mortality rate and length of stay in patients admitted a night to the intensive car unit. Crit Care Med. 2003 Mar;31(3):858-63. Rosenberg AL, Hofer TP, Strachan C, Watts CM, Hayward RA. Accepting critically ill transfer patients:adverse effect on a referral cneter’s outcome and benchmark measures. Ann Intern Med. 2003 June3;138(11):882-90. Higgins TL, McGee WT, Steingrub JS, Rapoport J, Lemeshow S, Teres. Early indicators of prolonged intensive care unit stay:impact of illness severity, physician staffing, and pre-intensive care unit length of stay. Crit Care Med. 2003 Jan;31(1):45-51. Rocker G, Cook D, Sjokvist P, Weaver B, Finfer S, McDonald E, Marsh J, Kirby A, Levy M, Dodek P, Heyland D, Guyatt G; Level of Care Study Investigators;Canadian Critical Care Trials Group. Crit Care Med. 2004 May;32(5):114954. Nasraway SA, Button GJ, Rand WM, Hudson-Jinks T, Gustafson M. Surviors of catastrophic illness:outcome after direct transfer from intensive care to extended care facilities. Crit Care Med. 2000Jan;28(1):19-25.

Reading Array Values Using for Loop #1 #include int main () { /* main */ const int z_length = 6; const int program_success_code = 0; float z[z_length], z_squared[z_length]; int index; for (index = 0; index < z_length; index++) { printf("Input z[%d]:\n", index); scanf("%f", &z[index]); } /* for index */ for (index = 0; index < z_length; index++) { z_squared[index] = z[index] * z[index]; } /* for index */ for (index = 0; index < z_length; index++) { printf("%19.7f^2 = %19.7f\n", z[index], z_squared[index]); } /* for index */ return program_success_code; “Use at least 19 spaces total, } /* main */ 7 of which are to the right of the decimal point.” Array Lesson 2 2 CS1313 Spring 2019

Named Constants as Loop Bounds #1 #include int main () { /* main */ const int z_length = 6; const int lower_bound = 0; const int program_success_code = 0; float z[z_length], z_squared[z_length]; int index; for (index = lower_bound; index < z_length; index++) { printf("Input z[%d]:\n", index); scanf("%f", &z[index]); } /* for index */ for (index = lower_bound; index < z_length; index++) { z_squared[index] = z[index] * z[index]; } /* for index */ for (index = lower_bound; index < z_length; index++) { printf("%19.7f^2 = %19.7f\n", z[index], z_squared[index]); } /* for index */ return program_success_code; } /* main */ Array Lesson 2 CS1313 Spring 2019 11

Slightly Less Simple Loops (F90) DO index = 1, length dst(index) = src1(index) ** src2(index) !! src1 ^ src2 END DO DO index = 1, length dst(index) = MOD(src1(index), src2(index)) END DO DO index = 1, length dst(index) = SQRT(src(index)) END DO DO index = 1, length dst(index) = COS(src(index)) END DO DO index = 1, length dst(index) = EXP(src(index)) END DO DO index = 1, length dst(index) = LOG(src(index)) END DO Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 47

Some Complicated Loops DO index = 1, length madd (or FMA): dst(index) = src1(index) + 5.0 * src2(index) mult then add END DO (2 ops) dot = 0 DO index = 1, length dot product dot = dot + src1(index) * src2(index) (2 ops) END DO DO index = 1, length dst(index) = src1(index) * src2(index) + & & src3(index) * src4(index) END DO from our example (3 ops) DO index = 1, length diff12 = src1(index) - src2(index) Euclidean distance (6 ops) diff34 = src3(index) - src4(index) dst(index) = SQRT(diff12 * diff12 + diff34 * diff34) END DO Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 62

Reading Array on One Line of Input #2 #include int main () { /* main */ const int z_length = 6; const int program_success_code = 0; float z[z_length], z_squared[z_length]; int index; printf("Input all %d values of z:\n", z_length); for (index = 0; index < 6; index++) { scanf("%f", &z[index]); } /* for index */ for (index = 0; index < 6; index++) { z_squared[index] = z[index] * z[index]; } /* for index */ for (index = 0; index < 6; index++) { printf("%19.7f^2 = %19.7f\n", z[index], z_squared[index]); } /* for index */ return program_success_code; } /* main */ Array Lesson 2 CS1313 Spring 2019 8

Frequency Distribution Example: Marada Inn Guests staying at Marada Inn were asked to rate the quality of their accommodations as being excellent, above average, average, below average, or poor. The ratings provided by a sample of 20 guests are: Above Average Below Average Average Above Average Above Average Above Average Below Average Above Average Below Average Poor Average Poor Above Average Above Average Excellent Average Above Average Average Above Average Average 5

Washing and waxing a car for 45-60 minutes Vigorous, Washing windows or floors for 45-60 minutes Less More Time Playing volleyball for 45 minutes Playing touch football for 30-45 minutes Gardening for 30-45 minutes Wheeling self in wheelchair for 30-40 minutes Walking 1 3/4 miles in 35 minutes (20 min/mile) Basketball (shooting baskets) for 30 minutes Bicycling 5 miles in 30 minutes Dancing fast (social) for 30 minutes Pushing a stroller 1 1/2 miles in 30 minutes Raking leaves for 30 minutes Walking 2 miles in 30 minutes (15 min/mile) Water aerobics for 30 minutes Swimming laps for 20 minutes Wheelchair basketball for 20 minutes Basketball )playing a game) for 15-20 minutes Bicycling 4 miles in 15 minutes Jumping rope for 15 minutes Running 1 1/2 miles in 15 minutes (10 min/mile) 2.2 Shoveling snow for 15 minutes Vigorous, Stairwalking for 15 minutes More Less Time

Some Simple Loops (F90) DO index = 1, length dst(index) = src1(index) + src2(index) END DO DO index = 1, length dst(index) = src1(index) - src2(index) END DO DO index = 1, length dst(index) = src1(index) * src2(index) END DO DO index = 1, length dst(index) = src1(index) / src2(index) END DO DO index = 1, length sum = sum + src(index) END DO Reduction: convert array to scalar Supercomputing in Plain English: Instruct Lev Par Tue Feb 13 2018 45