ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp. 473-498, and pp. 506-513.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

An Introduction to Programming with C++ Fifth Edition
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Chapter 8 Arrays and Strings
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
IE 212: Computational Methods for Industrial Engineering
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Multi-Dimensional Arrays
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Array Processing.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 6: Arrays: Lists and Tables
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
One-Dimensional Array Introduction Lesson xx
Starting Out with Programming Logic & Design
Tutorial 11 Arrays Tutorial 11: Arrays.
Introduction to Computer Programming IT-104
Presentation transcript:

ISAT 252 Introduction to Arrays

Should have read 2 Chapter 8 –pp , and pp

Learning Objectives Based on this lecture and your own study, you must be able to…. Define the following with respect to arrays Element Subscript or Index Lower bound and Upper bound Properly declare arrays for use in VB Distinguish between 1D and 2D arrays Use single and nested loops to access and process data stored in 1D and 2D arrays Write VB code to process data stored in arrays Utilize variable scope 3

Overview/Review of topics 4 Concepts from previous Lectures Software development process Decisions and Loops Coming up later in semester Procedures and functions - ByRef and ByVal Other Modularization - Multiple forms and Classes

Problem 5 If we are averaging 3 grades, we will have a variable for each grade: Dim dblGrade1 As Double Dim dblGrade2 As Double Dim dblGrade3 As Double What if we need to average 10 grades? Or 100 grades? PROBLEM!!! – How do we handle it?

Examples 6 We can have arrays Array of Students’ Grades Array of Months Arrays of States Students’ Grades

Definitions 7 Array An indexed series of individual elements of the same data type, all referenced by the same name plus its index Element An individual element or entry in an array Subscript or index An integer number that identifies an element’s position in the array

Scope of Variables Local Level – what we have done so far Declared within Sub/Function Available only within Sub/Function Class/Form Level – what we will use for Lab 10 Declared below Public Class frmName Available to all Subs/Functions in the class/form Project Level – only needed if a multiform project Declared in separate.vb file Available to all Subs/Functions in the project

Declaring Arrays 9 Arrays can be declared at form-level scope (in class) or at local level (in sub) Declared the same way as single variables (scalar variables) DIM statement for local or form-level scope In the declaration specify name, type, and the subscript range for the array Example: Dim sngCost(10) As Single allocates 11 memory locations sngCost(0)sngCost(10)

Subscripts 10 The subscript range can be specified using integer constants, integer variables, integer expressions Const intArraySize As Integer = 10 Dim sngCost(10) As Single Dim dblProfit( intArraySize ) As Double

Subscripts 11 Each element in an array is identified by its subscript Dim sngGrade(5) As Single sngGrade sngGrade(0) sngGrade(1) sngGrade(2) sngGrade(3) sngGrade(4) sngGrade(5) Name of the Array Subscript: Identifies the element in the array

Subscripts 12 Subscripts can be referenced as constants, variables, or numeric expressions intVar = 4 intIndex1 = 3 intIndex2 = 1 sngTotal(intVar) = 26.3 ‘Sets sngTotal(4) equal to 26.3 sngTotal(4) = 26.3 ‘ Does the same thing sngTotal(intIndex1+intIndex2) =26.3 ‘Also does same thing sngVar=sngTotal(intIndex1+intIndex2) + sngTotal(intVar) + _ sngTotal(4)

Example 13 Dim sngGrade(5) as Single sngGrade(1) = 85 sngGrade(5) = 92 For intIndex = 2 To 4 sngGrade(intIndex) = 100 Next intIndex sngGrade(0) = (sngGrade(1) + sngGrade(2) )/2 0 intIndex sGrade

Why use arrays? 14 To store and process LISTS or TABLES of data To provide easy access via loops To allow processing of groups of data, where the groups must be “remembered”

Example: computing average grade 15 Dim sngGrade(29) as Single‘space for 30 students Dim sngAverage as Single Dim sngTotal as Single Dim intI as Integer ‘read in student’s grades (sngGrade) from a file sngAverage=0.0 For intI =0 to 29 sngTotal += sngGrade(intI) Next intI sngAverage = sngTotal/30

Example: computing wages 16 Dim strName(9) as String Dim sngHours(9) as Single Dim sngPay(9) as Single Dim intPerson as Integer ‘read in employee names (strName) and hours worked (sngHours) code to read in names and hours goes here. ‘Calculate wages for each person sngWage = 8.75 For intPerson = 0 To 9 sngPay(intPerson) = sngWage * sngHours(intPerson) txtOutput.text += strName(intPerson) + _ formatCurrency(sngPay(intPerson)) + _ vbNewLine Next intPerson

Example: Tracking Your GPA 17 Two arrays intCreditHrs: an integer array storing the credit hours for each course strGrade: a string array storing the letter grade you received in each course (no +’s or –’s to keep it simple) Task: Calculate your GPA

Algorithm: Pseudocode 18 For each course… Convert the letter grade to a numeric grade (1-4 scale) Multiply the numeric grade * the number of credit hrs Add the product into a running total of grade points Add the course credit hrs into a running credit hr total Calculate final GPA as the total grade points divided by the credit hr total

Calculate GPA Case statement 19 Dim strGrade(19) as String‘Array of course letter grades Dim dblGrade (19) as Double ‘Array of point scales Dim intCounter as Integer‘ Index for looping thru arrays ‘First enter all grades into strGrade array ‘intNumGrades counts as grades are entered ‘Section to convert the letter grade to the 4-point scale For intCounter = 0 To intNumGrades - 1 Select Case strGrade(intCounter) Case “A” dblGrade(intCounter) = 4 Case “B” dblGrade(intCounter) = 3 Case “C” dblGrade(intCounter) = 2 Case “D” dblGrade(intCounter) = 1 Case Else dblGrade(intCounter) = 0 End Select Next intCounter

The Rest of the Sub 20 Dim intCrHrs(19) as Integer‘Array of course credit hrs Dim strGrade(19) as String‘Array of course letter grades Dim sngGPA as Single‘Computed GPA Dim intTotalCrHrs as Integer‘Total Credit Hrs taken Dim intCounter as Integer‘ Index for looping thru arrays ‘Code for inputing the entries in strCName, intCrHrs, and strGrade arrays goes here ‘Add the credit hrs and calculate the points toward ‘the GPA for each course sngGPA = 0 intTotalCrHrs = 0 For intCounter = 0 to intNumGrades - 1 sngGPA += (dblGrade(intCounter)) * intCrHrs(intCounter) intTotalCrHrs+=intCrHrs(intCounter) Next intCounter sngGPA /= intTotalCrHrs‘Final GPA calculation

Multi-Dimensional Arrays

One-Dimensional vs Two-Dimensional Arrays 22 1-Dimensional array – has only one subscript 2-Dimensional arrays – have two subscripts Analogous to a table 1 st subscript represents the “row” 2 nd subscript represents the “column” All elements in a given array must be of the same data type, regardless of dimension

Two-Dimensional Arrays 23 Correspond to a matrix all elements must be of the same data type use two subscripts to access an element 1st: row number 2nd: column number 1 st dimension (row) 2 nd dimension (col)

Declaring 2-D arrays 24 Dim intArray (1, 4) as Integer Dim sngGrades (29, 9) as Single Either, an array of numeric grades on 10 items for each of 30 students OR An array of grades on 30 items for each of 10 students (it’s up to you to decide!)

Accessing Array Elements 25 Dim intArray (1, 4) As Integer 1 intArray intArray(0,0) intArray(0,1) intArray(0,2) intArray(0,3) intArray(0,4) intArray(1,0) intArray(1,1) intArray(1,2) intArray(1,3) intArray(1,4) Name of the Array Row Subscript Column Subscript

Accessing Array Elements 26 intArray (0, 1) = 10 intArray (1, 4) = intArray(1, 3) + intArray(1, 4) intI = 2 intJ = 1 sngGrades (intI, intJ) = 93 intX = 1 intY = 2 intArray (1, intX + intY) = 15

2D Array Declaration 27 Dim sngGrades (29, 9) as Single array of grades for 30 students in 10 tests 1 st Dimension: Students 2 nd Dimension: Test scores …………………… sngGrades(i,j) : score of student i in test j sngGrades(1,6) Row i : all scores for student i …………………… Row 1 : scores for student #1 Column j : Scores of all students in test j …………………… Column 6: Scores of all students in test 6

Using Nested Loops to do work in 2-D arrays 28 For intRow = 0 To 1 For intCol = 0 To 4 intArray(intRow,intCol) = intRow + intCol Next intCol Next intRow For intRow = 0 To 1 intRowSum = 0 For intCol = 0 To 4 intRowSum = intRowSum + intArray(intRow,intCol) Next intCol txtOutput.text += intRowSum.toString()+ vbNewLine Next intRow

Using Single Loops to access a single row or column an a 2-D array 29 For intRow = 0 to 29 txtOutput.text += sngGrade(intRow, 9).ToString() +_ vbNewLine Next intRow sngTotal = 0 For intRow = 0 to 29 sngTotal = sngTotal + sngGrade(intRow, 9) Next intRow sngAverage = sngTotal/30 sngColTotal = 0 For intCol = 0 to 9 sngColTotal = sngColTotal + sngGrade(2, intCol) Next intCol

Example: Calculate and print gas mileage for 50 different cars Model names stored in a 1D string array file Miles and gasoline consumed stored in 2D single precision array 1 st Col = miles driven 2 nd Col = gasoline consumed (in gallons) ….. 30 strName sngDrivenData 0 XX Data for Car #2 Miles Driven by Car #2 : sngDriven(2,0) Car #2’s Name : strName(2) Gas consumed by Car #2 : sngDriven(2,1) X

Assignment 31 Do Lab 10 Read Chapter 6 on Sub and Function procedures for next week