Module 201 Object Oriented Programming Lecture 10 - Arrays. Len Shand

Similar documents
Module 201 Object Oriented Programming Lecture 11 String Arrays and Collections. Len Shand

Your First Windows Form

Pointers in C/C++ 1 Memory Addresses 2

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

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Reversing. Time to get with the program

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

SIMPLE INPUT and OUTPUT:

For example, let s say we define an array of char of size six:

Lecture (01) Getting started. Dr. Ahmed ElShafee

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

C++ Arrays and Vectors

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

TUTORIAL FOR IMPORTING OTTAWA FIRE HYDRANT PARKING VIOLATION DATA INTO MYSQL

Static Methods. Why use methods?

Text Input and Conditionals

TUTORIAL FOR IMPORTING OTTAWA FIRE HYDRANT PARKING VIOLATION DATA INTO MYSQL

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

CMSC201 Computer Science I for Majors

Computers, Variables and Types. Engineering 1D04, Teaching Session 2

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Ph3 Mathematica Homework: Week 1

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Shorthand for values: variables

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Section 0.3 The Order of Operations

The Dynamic Typing Interlude

Lecture 12 Modular Programming with Functions

Expressions and Casting

Algorithms and Programming I. Lecture#12 Spring 2015

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 1: Overview

Lecture (06) Java Forms

1 Lecture 5: Advanced Data Structures

4. Java language basics: Function. Minhaeng Lee

Lab 6 Vectors and functions

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

More About WHILE Loops

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

printf( Please enter another number: ); scanf( %d, &num2);

Advanced Computer Programming

Procedural Programming

Computer Vision. Matlab

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

Week 3 Classes and Objects

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Access: You will have to

Python Programming: An Introduction to Computer Science

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

INTERFACES IN JAVA. Prof. Chris Jermaine

Advanced Computer Programming

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

IEEE Floating Point Numbers Overview

LOOPS. Repetition using the while statement

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Transcriber(s): Aboelnaga, Eman Verifier(s): Yedman, Madeline Date Transcribed: Fall 2010 Page: 1 of 9

2.4 Choose method names carefully

First Java Program - Output to the Screen

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

CITS 1401 Problem Solving & Programming

CSC148H Week 3. Sadia Sharmin. May 24, /20

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

1 Getting started with Processing

Repetition CSC 121 Fall 2014 Howard Rosenthal

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

(Refer Slide Time 01:41 min)

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

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

APCS Semester #1 Final Exam Practice Problems

Properties and Definitions

Arrays in C++ Instructor: Andy Abreu

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

Lesson 9 Transcript: Backup and Recovery

Activity Guide - Will it Crash?

CS 126 Lecture P1: Introduction to C

Introduction to Computer Programming for Non-Majors

(I m not printing out these notes! Take your own.)

The Big Python Guide

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

Design Patterns: State, Bridge, Visitor

Boolean Expressions. Is Equal and Is Not Equal

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

4. Java Project Design, Input Methods

2.1 Basics of Functions and Their Graphs

Topic 2: Introduction to Programming

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Recursively Enumerable Languages, Turing Machines, and Decidability

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

A.4 Rationalizing the Denominator

Heap Arrays and Linked Lists. Steven R. Bagley

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

Introduction to Computer Programming for Non-Majors

Transcription:

Module 201 Object Oriented Programming Lecture 10 - Arrays Len Shand

Methods

Arrays One dimensional Multi dimensional

The variables you have been working with so far have only been able to hold one value at a time. Your integer variables can only hold one number, and your strings one chunk of text. An array is a way to hold more than one variable at a time. Think of a lottery programme. If you re just using single variables, you d have to set up your lottery numbers like this: lottery_number_1 = 1; lottery_number_2 = 2; lottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that, an array allows you to use just one identifying name that refers to lots of numbers.

You set up an array like this: int[] lottery_numbers;

If your array needs to hold floating point numbers, you d set your array up like this: float[] my_float_values; An array that needs to hold text would be set up like this: string[] my_strings

The next thing you need to do is to tell C# how big your array will be. The size of an array is how many items it is going to hold. You do it like this: lottery_numbers = new int[49];

So far, you have just set up the array, and created an array object. But the array doesn t yet hold any values. (Well it does, because C# assigns some default values for you. In the case of int arrays, this will be just zeros. But they re not your values!)

To assign a value to an array, you use the square brackets again. Here s the syntax: array_name[position_in_array] = array_value;

Here s an example using our lottery numbers: lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4;

Another way to assign values in array is by using curly brackets. If you only have a few values going in to the array, you could set it up like this: }; int[] lottery_numbers = new int[4] { 1, 2, 3, 4

Arrays come into their own with loops. The idea is that you can loop round each position in your array and access the values. We ll try a programming example, this time.

So start your C# software up, if you haven t already, and create a new Windows Application. Add a button and a list box to your form. Double click your button to get at the code.

For the first line, add code to clear the list box: private void button1_click(object sender, EventArgs e) { listbox1.items.clear(); }

private void button1_click(object sender, EventArgs e) { listbox1.items.clear(); } int[] lottery_numbers; lottery_numbers = new int[4];

private void button1_click(object sender, EventArgs e) { listbox1.items.clear(); int[] lottery_numbers; lottery_numbers = new int[4]; //We could also do: lottery_numbers = new int[4] {1.2.3.4}; lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4; }

listbox1.items.add( lottery_numbers[0] ); listbox1.items.add( lottery_numbers[1] ); listbox1.items.add( lottery_numbers[2] ); listbox1.items.add( lottery_numbers[3] );

So to get at the value in an array, you just use the array name and a position number, known as the index number: lottery_numbers[0]; This is enough to display what value is at this position in the array. Try it out. Add the list box code to your programme

private void button1_click(object sender, EventArgs e) { listbox1.items.clear(); int[] lottery_numbers; lottery_numbers = new int[4]; lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4; listbox1.items.add( lottery_numbers[0] ); listbox1.items.add( lottery_numbers[1] ); listbox1.items.add( lottery_numbers[2] ); listbox1.items.add( lottery_numbers[3] ); }

If you had a long list of numbers to display, you don t really want to type them all out by hand! Instead, you can use a loop. Add the following loop to your code: for (int i = 0; i!= (lottery_numbers.length); i++) { listbox1.items.add( lottery_numbers[i] ); } Now delete all of your list box lines.

i = 0 i = 1 i = 2 i = 3 Loop Array lottery_numbers[0] lottery_numbers[1] lottery_numbers[2] lottery_numbers[3] The value in each position is then accessed, which for us was the numbers 1 to 4.

i!= (lottery_numbers.length) Length is a property of arrays that you can use. It refers to the number of items in your array. So we re saying, Keep looping while the value in i does not equal The Length of the array.

You can also use a loop to assign values to your arrays. In the code below, we re using a loop to assign values to our lottery_numbers array: for (int i = 0; i!= (lottery_numbers.length); i++) { lottery_numbers[i] = i + 1; listbox1.items.add(lottery_numbers[i]); }

The only thing that has changed with our for loop is the addition of this line: lottery_numbers[i] = i + 1; The first time round the loop, the value in i will be zero. Which gives us this: lottery_numbers[0] = 0 + 1; The second time round the loop, the value in i will be 1. Which gives us this: lottery_numbers[1] = 1 + 1;

What we are doing is manipulating the index number (the one in square brackets). By using the value of a loop variable, it gives you a powerful way to assign values to arrays. Previously, we did this to assign 4 numbers to our array: lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4;

But if we need 49 numbers, that would be a lot of typing. Contrast that to the following code: private void button1_click(object sender, EventArgs e) { listbox1.items.clear(); int[] lottery_numbers; lottery_numbers = new int[49]; } } for (int i = 0; i!= (lottery_numbers.length); i++) { lottery_numbers[i] = i + 1; listbox1.items.add(lottery_numbers[i]); }

As an exercise, halt your programme and change the index number of the array from 49 to 1000. Run your programme and test it out. What you ve done is to set up an array and fill it with a thousand values!

The size of an array refers to how many items it holds. You ve seen that to set the size of an array, you do this: int[] somenumbers; somenumbers = new int[10];

Sometimes, you just don t know how big the array needs to be. Think of a programme that pulls information from a database. You want to loop round and check how many of your customers still owe you money. You ve decided to hold all the data in an array. But how big does the array need to be? You can t set a fixed size before the programme runs, simply because the number of people owing you money could change. It could be low one month, and high the next.

To solve the problem, you can set the array size at runtime. This would mean, for example, setting the array size after a button is clicked.

To see how it works, add another button to your form, along with a text box. Your form should then look something like this:

Double click the new button and enter the following code: int anumber = int.parse(textbox1.text); int[] arraysize; arraysize = new int[anumber];

Add the following loop to your code, which just assigns values to the array, and places them in the list box on your form: for (int i = 0; i!= (arraysize.length); i++) { arraysize[i] = i + 1; listbox1.items.add(arraysize[i]); } Run your programme and click your button.

Now delete the 5 and type a different number. You should see the number from 1 to whatever number you ve just typed.

This technique can be used whenever your programme needs to get its array size at runtime assign to a variable, and use this variable between the square brackets of your array.

To get some more practice with arrays, add another button to your form. Set the text to be this: Exercise: Times Table

Now double click the button and add the following code: private void button3_click(object sender, EventArgs e) { listbox1.items.clear(); int[] arraytimes; arraytimes = new int[10]; int times = int.parse(textbox1.text); for (int i = 1; i!= (arraytimes.length); i++) { arraytimes[i] = i * times; listbox1.items.add(times + " times " + i + " = " + arraytimes[i]); } }

The first item in the array, arraytimes[0], doesn't get used - why is this? Amend the code so that the times table from 1 to 10 displays in the list box, not 1 to 9

Your arrays don t have to be single lists of items, as though they a were a column in a spreadsheet: you can have arrays with lots of columns and rows. These are called Multi-Dimensional arrays.

Array Position 0 10 Array Position 1 20 Array Position 2 30 Array Position 3 40 Array Position 4 50 Column 1

Column 0 Column 1 Column 2 Array Position 0 10 100 1000 Array Position 1 20 200 2000 Array Position 2 30 300 3000 Array Position 3 40 400 4000 Array Position 4 50 500 5000

So if you wanted to get at the value of 2000 in the table above, this would be at array position 1 in column 2 (1, 2). Likewise, the value 400 is at position 3, 1.

int[,] arraytimes; You then need a number either side of the comma: arraytimes = new int[5, 3]; The first digit is the number of Positions in the array; the second digit is the number of Columns in the array.

Filling up a 2-dimensional array can be quite tricky because you have to use loops inside of loops!

NOW Add This Line: arraytimes[i, j] = mult; mult = mult * 10; listbox1.items.add("arraypos = " + arrayrows + "," + arraycols + " val = " + (mult /10));

Notice the two for loops in the code above, one inside of the other. The first loop is setting the value of the Rows (array Positions), and the second loop is setting the value of the Columns.

For the adventurous, add another button to your form. Enter the code above. Now add a second double for loop, and print out the array values in your list box.

String Arrays and text Collections