160 likes | 279 Views
Working with Arrays. Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010. Agenda. What Is An Array? Using Arrays With GML When Are They Useful? A Sample Give Yourself Arrays (sample problem). What is an array?. An array is a COLLECTION of data items
E N D
Working with Arrays Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010
Agenda • What Is An Array? • Using Arrays With GML • When Are They Useful? • A Sample • Give Yourself Arrays (sample problem)
What is an array? • An array is a COLLECTION of data items • Most variables hold a SINGLE value “Apple” True 380 • One array can hold MULTIPLE values “Apple” 380 “Cherry” 27 “Grape” 60 217
What is an array? • Arrays follow variable naming rules • But they also have an INDEX • The INDEX refers to a particular value in the array FruitNames 012 “Apple” “Cherry” Indexes -> “Grape”
What is an array? 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) Think of an INDEX like an apartment number
What is an array? • NOTE: the first INDEX is 0, not 1 • This is the case is most programming languages! FruitNames 012 “Apple” “Cherry” Indexes -> “Grape”
Using Arrays with GML • In GML, you do not need to declare arrays • An array in GML looks like this:VarName[index]VarName is the variable nameindex is an integer • Examples:strSubject[0] = “History”;strSubject[1] = “Math”;
Using Arrays with GML – Try This! • 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 Are They Useful? • When you’d like to store similar related valuesstrCookie[0] = “Chocolate Chip”;strCookie[1] = “Oatmeal”;strCookie[2] = “Anchovy”;strCookie[3] = “Peanut Butter”;
When Are They Useful? • When you need to store many valuesvar num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11……num[0] = 7;num[1] = 5;num[2] = 6;…num[10] = 4;
When Are They Useful? • 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 valuex[0] = 1;x[1] = 7;x[2] = 4;for (i = 0; i < 3; i++){ total = total + x[i];}
When Are They Useful? – Try This! • 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]);}
A Sample • 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
A Sample • varuser_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”);
Give Yourself Arrays – Sample 1 • 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
Give Yourself Arrays – Sample 2 • 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