Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010 What Is An Array? Using Arrays With GML When Are They Useful? A Sample Give Yourself Arrays (sample problem) An array is a COLLECTION of data items Most variables hold a SINGLE value “Apple” True One array can hold MULTIPLE values “Apple” 380 “Cherry” 27 “Grape” 60 217 380 Arrays follow variable naming rules But they also have an INDEX The INDEX refers to a particular value in the array FruitNames Indexes -> 0 1 2 “Apple” “Cherry” “Grape” If a regular variable is like a house… Then an array is like a condo… They all share one common address (variable name) but they each have their own apartment number (an INDEX) NOTE: the first INDEX is 0, not 1 This is the case is most programming languages! FruitNames Indexes -> 0 1 2 “Apple” “Cherry” “Grape” In GML, you do not need to declare arrays An array in GML looks like this: VarName[index] VarName is the variable name index is an integer Examples: strSubject[0] = “History”; strSubject[1] = “Math”; Create a script in GML Create an array with three values: strPop[0] = “Pepsi”; strPop[1] = “7-Up”; strPop[2] = “Dr. Pepper”; Print each to the screen with a message box: show_message(strPop[0]); show_message(strPop[1]); show_message(strPop[2]); When you’d like to store similar related values strCookie[0] = “Chocolate Chip”; strCookie[1] = “Oatmeal”; strCookie[2] = “Anchovy”; strCookie[3] = “Peanut Butter”; When you need to store many values var num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11…… num[0] = 7; num[1] = 5; num[2] = 6; … num[10] = 4; Process multiple values within a REPITITION structure (if you like loops, you’ll love arrays!) With the use of a counter, each iteration processes the next value x[0] = 1; x[1] = 7; x[2] = 4; for (i = 0; i < 3; i++) { total = total + x[i]; } Using our same array of brands/kinds of pop: strPop[0] = “Pepsi”; strPop[1] = “7-Up”; strPop[2] = “Dr. Pepper”; for (i = 0; i < 3; i = i + 1) { show_message(strPop[i]); } Have the user enter as many numbers as they like When they want to quit entering numbers, they enter -99 After they finish entering numbers: show them all the numbers they entered determine which number is the biggest and display it var user_number, numbers, counter, biggest; user_number = get_integer(“Enter a number”, 0); biggest = 0; counter = 0; while (user_number != -99) { numbers[counter] = user_number; counter = counter + 1; user_number = get_integer(“Enter a number”, 0); } for (i = 0; I < counter; I = I + 1) { show_message(string(numbers[i])); if (numbers[i] > biggest) biggest = numbers[i]; } show_message(string(biggest) + “ was the largest number”); Prompt the user for three numbers and store them in an array Use a loop to put the numbers together in a string E.g., if your numbers are 12, 57, and 42, combine them into a string to give “125742” Display this string in a message box Write a script to ask the user for a lunch order Prompt them to enter each item, for example: hotdog chips Coke When they are done entering items, they should enter the word “nothing” Display each item in their lunch order in the reverse order they entered them