Session 8.2. Finding Winners Using Arrays

Similar documents
D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Introduction to Computer Science Midterm 3 Fall, Points

Chapter 9 Introduction to Arrays. Fundamentals of Java

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Weighted Powers Ranking Method

Tic Tac Toe Game! Day 8

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

(8-1) Arrays I H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 8, 2018) Washington State University

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

EECE.2160: ECE Application Programming Fall 2017

Functions, Arrays & Structs

TOPICS TO COVER:-- Array declaration and use.

Multiple-Subscripted Arrays

More on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class:

C Functions. 5.2 Program Modules in C

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

ECE15: Lab #4. Problem 1. University of California San Diego

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

RANDOM NUMBER GAME PROJECT

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

CS 314 Exam 2 Spring

COMP 110 Introduction to Programming. What did we discuss?

COMP 110 Programming Exercise: Simulation of the Game of Craps

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Chapter 6: Using Arrays

Arrays. Array Basics. Chapter 8 Spring 2017, CSUS. Chapter 8.1

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

CS 101, Spring 2016 March 22nd Exam 2

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

The Simon State Machine Part 1

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

FORM 2 (Please put your name and form # on the scantron!!!!)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Condition Controlled Loops. Introduction to Programming - Python

Pointers, Arrays and Parameters

Programming for Engineers Arrays

CSC 1300 Exam 4 Comprehensive-ish and Structs

Chapter 7 Arrays. One-Dimensional Arrays. Fred Jack. Anna. Sue. Roy

Arrays. Arizona State University 1

IT115: Introduction to Java Programming 1. IT115: Introduction to Java Programming. Tic Tac Toe Application. Trina VanderLouw

CS 223: Data Structures and Programming Techniques. Exam 2. April 19th, 2012

Arrays OBJECTIVES. In this chapter you will learn:

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

Computing and Programming

Computing and Programming. Notes for CSC The Beauty and Joy of Computing The University of North Carolina at Greensboro

AP Computer Science Lists The Array type

2D Array Practice. Lecture 26

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

Procedural Programming

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

NCAA Instructions Nerdy for Sports

Introduction to Computer Science Unit 3. Programs

CS1 Studio Project: Connect Four

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Objective-C. Stanford CS193p Fall 2013

Dim there(2) As Double represents? been changed. You can access the elements of the array in any

Lecture 04 FUNCTIONS AND ARRAYS

GridLang: Grid Based Game Development Language. Programming Language and Translators - Spring 2017 Prof. Stephen Edwards

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

CIS 110 Fall 2016 Introduction to Computer Programming 13 Oct 2016 Midterm Exam Answer Key

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Distributions of Continuous Data

Chapter 7: Arrays and the ArrayList Class

Java Programming: from the Beginning

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

1 Short Answer (2 Points Each)

Cambridge International Examinations Cambridge International Advanced Subsidiary and Advanced Level

Add the backgrounds. Add the font.

Lecture 14. 'for' loops and Arrays

Expressions and Casting

MICROPROCESSORS A (17.383) Fall Lecture Outline

Computer Programming: C++

Computer Programming

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Introduction to Arrays

CS 537: Intro to Operating Systems (Fall 2017) Worksheet 3 - Scheduling & Process API Due: Sep 27 th 2017 (Wed) in-class OR Simmi before 5:30 pm

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Functions and Recursion

An algorithm may be expressed in a number of ways:

ESC101N: Fundamentals of Computing End-sem st semester

CSCI 162 Dr. Stephanie Schwartz Fall 2014 Review Questions for Exam 1 ** adapted from Ms. Katz and Dr. Hutchens **

ECE4530 Fall 2015: The Codesign Challenge I Am Seeing Circles. Application: Bresenham Circle Drawing

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Homework 1. Hadachi&Lind October 25, Deadline for doing homework is 3 weeks starting from now due date is:

BOREDGAMES Language for Board Games

General Certificate of Education Advanced Subsidiary Examination June 2010

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Transcription:

1 Session 8.2 Finding Winners Using Arrays

Chapter 8.2: Finding Winners Using Arrays 2 Session Overview Find out how the C# language makes it easy to create an array that contains multiple values of a particular type Learn how to work with individual values stored as part of an array Use arrays to make a program that automatically displays the winning score from the Reaction Timer game Use an array as a look-up table to identify the winning players of the Reaction Timer game

Chapter 8.2: Finding Winners Using Arrays 3 Reaction Timer Winner Display At the moment the players have to decide who won a Reaction Timer game They have to find the lowest button time and then work out which player had that time It would be nice if the game program could do this

Chapter 8.2: Finding Winners Using Arrays 4 Finding the Winning Score A winning score is one which is greater than 0, and less than any other score This means that the player pressed their button before anyone else We can create a condition that will test if a particular score is the winning one The program just has to compare the value with all the ones that it needs to beat

Chapter 8.2: Finding Winners Using Arrays 5 Finding the Winning Score if ( ascore1 > 0 ) if ( ascore1 < bscore1 && ascore1 < xscore1 && ascore1 < yscore1 ) // if we get here button A of Gamepad 1 has won This code tests to see if button A has beaten all the other buttons on gamepad 1 To make the test for all the gamepads would require another 12 tests

Chapter 8.2: Finding Winners Using Arrays 6 The C# Array To solve this problem we need another way of accessing data We need a way that a program can work through a list of score values and find the smallest one that is the winner In computer language terms we need to use an array We know that computers can work with very large amounts of data, now we are going to find out how

Chapter 8.2: Finding Winners Using Arrays 7 Creating a One Dimensional Array int[] scores = new int[4]; An array is declared like any other variable The [] characters (square brackets) are very important The above array is called scores and it has been created with a capacity of 4 values The array is of type int, i.e. it can hold 4 integers We can create arrays of any type we like

Chapter 8.2: Finding Winners Using Arrays 8 Visualizing the Array You can visualize an array as a row of numbered boxes In the case of the scores array there are four such boxes, each of which can hold a single integer The first box in the row has the number 0, the last box has the number 3

Chapter 8.2: Finding Winners Using Arrays 9 Arrays and Elements Each box in the array is called an element When an integer array is created all the elements are initialized to the value 0 A C# program can read and write the values in the elements in an array

Chapter 8.2: Finding Winners Using Arrays 10 Using a Subscript to Access an Element scores[1] = 99; A program can access a particular element by using a subscript value The subscript is given in square brackets, as shown above

Chapter 8.2: Finding Winners Using Arrays 11 Falling Off the End of the Array scores[4] = 99; An attempt to access an array element that does not exist will cause the program to fail with an exception

Chapter 8.2: Finding Winners Using Arrays 12 Storing Reactions Scores in an Array if (oldpad1.buttons.a == ButtonState.Released && pad1.buttons.a == ButtonState.Pressed && scores[0] == 0) scores[0] = timer; This code replaces the variable ascore1 with the element at the start of the scores array We can do this for all the other score values in the game, so that the time values are all held in the scores array

Chapter 8.2: Finding Winners Using Arrays 13 Storing the Scores in an Array After a game has been played we might have a set of results as shown above We now need to create some C# code that will find the winning score We need to find the smallest score value which is greater than zero

Chapter 8.2: Finding Winners Using Arrays 14 An Algorithm to Find the Winning Score Look at each element in the array in turn. If the element contains a better value than the one you presently have, that is now the new best value This is what you actually did when you worked out the answer If you had to do this for 10,000 score values you would write down the best value you had seen so far, so that you didn t forget it and have to start again

Chapter 8.2: Finding Winners Using Arrays 15 Using a For Loop to Find the winner int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; This loop will find the winning score value It will work for any sized array

Chapter 8.2: Finding Winners Using Arrays 16 Creating Our Highest So Far Winning Value int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; Create a variable to hold the winning score Set it to a very large value which is not a winner

Chapter 8.2: Finding Winners Using Arrays 17 Declaring and Setting Up Our Loop Counter int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; C# lets you declare and initialize a variable to be used as a loop counter in a single statement

Chapter 8.2: Finding Winners Using Arrays 18 Checking the End Condition of the Loop int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; If we try to use the subscript 4 the program will throw an exception, so the loop must stop when i reaches 4

Chapter 8.2: Finding Winners Using Arrays 19 Moving on to the Next Element int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; Once we have tested one element we need to move on to the next one in the array

Chapter 8.2: Finding Winners Using Arrays 20 Test for a Valid Score int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; Only score values greater than 0 are valid Less than 0 means the button was pressed early

Chapter 8.2: Finding Winners Using Arrays 21 Test for a Winning Score int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; If this score value is less than the present winner then we have a new winning value

Chapter 8.2: Finding Winners Using Arrays 22 Updating the Winning Score int winningvalue = 120; for (int i = 0; i < 4; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; If we have a new winner, store it in the winningvalue variable

Chapter 8.2: Finding Winners Using Arrays 23 Identifying the Winning Player At the moment the program just works out the winning score It does not say which button achieved that score The program must display the winner as well This means that it must remember the position in the array of the winning score value We can then use this position to identify the winning player

Chapter 8.2: Finding Winners Using Arrays 24 Storing the Winner Subscript int winningvalue = 120; int winnersubscript = 0; for (int i = 0; i < 16; i++) if (scores[i] > 0) if (scores[i] < winningvalue) winningvalue = scores[i]; winnersubscript = i; The value of i for a high score is stored in winnersubscript

Chapter 8.2: Finding Winners Using Arrays 25 Identifying the Winner Using a Look-Up Table We now have code that will find out the position in the scores array of the winning score We can create a second array which lets the program look up the name of button that generated this score The names array is an array of strings Each element holds the name of a button

Chapter 8.2: Finding Winners Using Arrays 26 Declaring a Look-Up Table string[] names = new string[] "Gamepad 1 A", "Gamepad 1 B", "Gamepad 1 X", "Gamepad 1 Y" ; The names array is an array of strings which are pre-set with the button names The C# compiler can work out how many elements are being created, so there is no need to set the size of the names array The text lines up with the elements in scores

Chapter 8.2: Finding Winners Using Arrays 27 Displaying the Winner if (winningvalue!= 120) winnername = names[winnersubscript]; else winnername = "**NO WINNER**"; This code sets a string variable in the game world called winnername to the name of the winner If there are no winners (nobody pressed their button) the string is set to No Winner The string is displayed by the Draw method

Chapter 8.2: Finding Winners Using Arrays 28 Timing the Game play if (timer == 120) // find the winning score // set the variable winnername to the winner The game will display the winner two seconds after the sound effect was played Code in the Update method can test for the timer value reaching 120 and trigger the code that finds the winning score and sets it for display

Chapter 8.2: Finding Winners Using Arrays 29 1. Reaction Timer with Winner This version of the reaction timer game uses the algorithm described above The name of the winner is displayed

Chapter 8.2: Finding Winners Using Arrays 30 Summary A C# array allows a programmer to create a single variable that holds a large number of items of a particular type Each item in an array is called an element, and particular elements are identified by means of a subscript value In an array of size n, the subscript values range from 0 to n-1 Attempts to use an invalid subscript value will cause a program to throw an exception