16
Working with Arrays Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Embed Size (px)

Citation preview

Page 1: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Working with Arrays

Len KamermanInstructorCS-10001, Mohawk College, Fall 2010

Page 2: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Agenda

What Is An Array?Using Arrays With GMLWhen Are They Useful?A SampleGive Yourself Arrays (sample problem)

Page 3: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

One array can hold MULTIPLE values

What is an array?

An array is a COLLECTION of data itemsMost variables hold a SINGLE value

380True“Apple”

“Apple”

“Cherry”

“Grape”

380

27

60

217

Page 4: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

What is an array?

Arrays follow variable naming rulesBut they also have an INDEXThe INDEX refers to a particular value in the array

“Apple”

“Cherry”

“Grape”

FruitNames

012

Indexes ->

Page 5: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

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

Page 6: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

What is an array?

NOTE: the first INDEX is 0, not 1This is the case is most programming languages!

“Apple”

“Cherry”

“Grape”

FruitNames

012

Indexes ->

Page 7: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Using Arrays with GMLIn GML, you do not need to declare arraysAn array in GML looks like this:

VarName[index]

VarName is the variable nameindex is an integer

Examples:strSubject[0] = “History”;strSubject[1] = “Math”;

Page 8: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Using Arrays with GML – Try This!

Create a script in GMLCreate 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]);

Page 9: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

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”;

Page 10: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

When Are They Useful?

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;

Page 11: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

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 value

x[0] = 1;x[1] = 7;x[2] = 4;for (i = 0; i < 3; i++){

total = total + x[i];}

Page 12: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

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]);}

Page 13: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

A SampleHave the user enter as many numbers as they likeWhen they want to quit entering numbers, they enter -99After they finish entering numbers:

show them all the numbers they entereddetermine which number is the biggest and display it

Page 14: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

A Samplevar 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”);

Page 15: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Give Yourself Arrays – Sample 1

Prompt the user for three numbers and store them in an arrayUse a loop to put the numbers together in a stringE.g., if your numbers are 12, 57, and 42, combine them into a string to give “125742”Display this string in a message box

Page 16: Len Kamerman Instructor CS-10001, Mohawk College, Fall 2010

Give Yourself Arrays – Sample 2

Write a script to ask the user for a lunch orderPrompt them to enter each item, for example:

hotdogchipsCoke

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