Computer Memory 21 oneGrade integer We can declare, assign and manipulate individual variables with ease…

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Class 7.
Dale/Weems/Headington
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s name obeys the same rules as when declaring a variable:
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Sorting an Array. Computer Memory shtGrades (short) Index0123 Values3291 Computer Memory shtGrades (short) Index0123 Values1239 We have… We want…
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Instructor - C. BoyleFall Semester
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Chapter 8: Arrays.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
ITM © Port, KazmanVariables - 1 ITM 352 Data types, Variables.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
22/11/ Selection If selection construct.
Copyright Curt Hill Variables What are they? Why do we need them?
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
ITM © Port, KazmanVariables - 1 ITM 352 Data types, Variables Class #4.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
New Structure Recall “average.cpp” program
Variables ICS2O.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
CIS16 Application Development and Programming using Visual Basic.net
Programming Logic and Design Fifth Edition, Comprehensive
COMPUTING.
Presentation transcript:

Computer Memory 21 oneGrade integer We can declare, assign and manipulate individual variables with ease…

Computer Memory oneGrade integer twoGrade ThreeGrade integer 23 If we have a “list” of variables, we don’t currently have an elegant way to handle this… 21 fourGrade integer

Computer Memory 21 Grades integer What we need is a single variable that can handle multiple values (of the same type) This is an Array

Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s name obeys the same rules as when declaring a variable: it begins with a letter and may be followed by any number of letters, underscores, or digits. An array name can be as small as one letter or as large as 255 letters, underscores, and digits. Individual values within an array are selected by using an index. The lowest index of an array is 0, while the upper bound of the array is the highest index in the array. An index must be a Short, an Integer, or a Long data type. The data type is any valid variable like a Short, an Integer, or a String.Arrays

Computer Memory shtGrades (short) Index0123 Values0000 Dim shtGrades(3) As Short

Computer Memory shtGrades (short) Index0123 Values21000 shtGrades(0) = 21 We can assign an individual variable by using its name, and index. Index and subscript are synonymous

Computer Memory shtGrades (short) Index0123 Values shtGrades(0) = 21 shtGrades(1) = 12 shtGrades(2) = 23 shtGrades(3) = 21 shtGrades(0) = 21 shtGrades(1) = 12 shtGrades(2) = 23 shtGrades(3) = 21

Computer Memory shtGrades (short) Index0123 Values shtGrades(4) = 21 It is our responsibility to make sure that we are always addressing a legal location in the array. shtGrades(-1) = 1 would also crash our program.

strSimpsonNames (String) Index01234 Values“Homer”“Marge”“Bart”“Lisa”“Maggie” Arrays can contain any variable type Dim strSimpsonNames(4) As String strSimpsonNames(0) = “Homer" strSimpsonNames(1) = “Marge" strSimpsonNames(2) = “Bart" strSimpsonNames(3) = “Lisa" strSimpsonNames(4) = “Maggie" Dim strSimpsonNames(4) As String strSimpsonNames(0) = “Homer" strSimpsonNames(1) = “Marge" strSimpsonNames(2) = “Bart" strSimpsonNames(3) = “Lisa" strSimpsonNames(4) = “Maggie"

strSimpsonNames (Boolean) Index01234 ValuesTrueFalseTrueFalse Arrays can contain any variable type Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False

strSimpsonNames (String) Index01234 Values“Homer”“Marge”“Bart”“Lisa”“Maggie” We can use array variables just like regular variables bntRedDemo.Text = strSimpsonNames(0) & “ Simpson” If (strSimpsonNames(3) = “Lisa”) Then bntRedDemo.Text = “Oldest of the Simpson girls” End If bntRedDemo.Text = strSimpsonNames(0) & “ Simpson” If (strSimpsonNames(3) = “Lisa”) Then bntRedDemo.Text = “Oldest of the Simpson girls” End If But the real power of arrays comes from using them with loops

Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False Dim intLoopCount As Integer Dim strSex As String = "" For intLoopCount = 0 To 4 If blnSimpsonSexIsMale(intLoopCount) Then strSex = strSex & "M" Else strSex = strSex & "F" End If Next intLoopCount bntRedDemo.Text = strSex

Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} It is often convenient to initialize an array in the same line of code as you declare the array. You can initialize an array by setting it equal to the values you wish to initialize the array to enclosed within curly braces and separated by commas. Note that there is no upper bound used. This value is inferred by counting the number of items in the list

Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} Dim intLoopCount As Integer Dim strSex As String = "" For intLoopCount = 0 To Ubound(blnSimpsonSexIsMale) If blnSimpsonSexIsMale(intLoopCount) Then strSex = strSex & "M" Else strSex = strSex & "F" End If Next intLoopCount bntRedDemo.Text = strSex It is convenient to use the UBound function to determine the upper bound of an array. By passing an array to UBound, the upper bound of the array is returned.

Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"} Dim intLoopCount As Integer For intLoopCount = 0 To UBound(strSimpsonNames) bntRedDemo.Text = strSimpsonNames(intLoopCount) & " Simpson" Next intLoopCount …. Note: Without a call to the Sleep function, this happens so fast all we see is “Maggie Simpson”

Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"} Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} Dim intLoopCount As Integer For intLoopCount = 0 To UBound(strSimpsonNames) If blnSimpsonSexIsMale(intLoopCount) Then bntRedDemo.Text = "Male : " & strSimpsonNames(intLoopCount) & " Simpson" Else bntRedDemo.Text = "Female : " & strSimpsonNames(intLoopCount) & " Simpson" End If Next intLoopCount ….

Useful Exercises Can you extend the code above to count the number of males in the Simpson family? Can you add a new Array variable to hold the Simpson's ages, then find the average age of a Simpson family member? Can you produce this output… Homer Simpson Male: At 36 he is consider old Maggie Simpson Female: At 2 she is consider young ….

Homer Simpson Male: At 36 he is consider old Maggie Simpson Female: At 2 she is consider young ….