Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 10 Introduction to Arrays
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Programming with Microsoft Visual Basic 2005, Third Edition
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Arrays.
Chapter seven review. What is the output of: Private Sub cmdButton_Click() Dim i As Integer, a(1 To 4) As integer Open "DATA.TXT" For Input As #1 For.
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.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 71 Using Part of an Array Sometimes we do not know how many elements will be needed in an array. We can declare a large array, say of 100 elements,
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 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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
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.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using LINQ with Arrays 7.3 Arrays of Structures 7.4 Two-Dimensional Arrays 7.5 A Case Study:
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 6: Arrays: Lists and Tables
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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
1.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Two Forms Please use speaker notes for additional information!
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Controlling Program Flow with Looping Structures
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
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.
Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Chapter 6 Controlling Program Flow with Looping Structures.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Chapter 9: Sorting and Searching Arrays
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Department Array in Visual Basic
Python Basics with Jupyter Notebook
Presentation transcript:

Introduction to Arrays Chapter 7

Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread data

The kinds of problems that benefit from array representation of data Roll a die (6 sides) 1000 times & record the frequency of each outcome. Display it. Shuffle a deck of 52 cards & then choose 1 card. Record the frequency of each outcome. Read in 100 integers (e.g., test scores, prices). Sort them in ascending (or descending) order

Details & terminology To use an array, we must establish its subscript range - this must agree with the number of elements the array will have. Subscript = position in the array Element = the contents of the array at a particular position Ex: num(5) is the 5th element in the array called num

Array declaration All arrays must be declared using a Dim statement: Dim num (1 To 25) As Single Array name Subscript range Element type

Where to declare arrays Most of the time, arrays are declared as form-level variables This means that we Dim them in the “Declarations” section of (General) –this is done in code view Any form-level variable can be used by any procedure or function without being declared as a parameter.

Declaring & referencing Dim x (1 To 8) As Single x: Dim y (1 To 5) As String y: “abc” “3” “you” “hi” “#” x(3), x(7), x(2) + x(5), x(2 + 5) y(1), y(4), y(2) & y(3), len(y(5)) if k = 3, what is x(k)? y(k)? x(k) + 1? x(k+1)? y(k-1) & y(k+1)? y(x(8))?

Common array activities Read data values from a user or an input file into an array and process them –some processing requires visiting a data value more than once, so arrays are recommended or required Search an array Sort array elements –there are many sorting algorithms to use

Array processing requires iteration (looping) If we must process every element of an array, then we use a For…Next loop If we will process the array while or until a certain condition is met, then we use a While or Until loop Some of this iterative processing requires doubly nested loops –Sorts

Fill an array with user’s data Dim a (1 To 10) As Integer …... For j = 1 To 10 a(j) = Val (InputBox(“Enter an integer.”) Next j Because we must process every element, we use For...Next

Fill an array with data from an input file Dim a (1 To 10) As Integer …... Open “Integers.txt” for input as #1 For j = 1 To 10 Input #1, a(j) Next j Close #1 Because we must process every element, we use For...Next

Find the largest element in an array Dim num (1 To 100) of Any_Type ………... max = num(1) For k = 2 to 100 if num(k) > max then max = num(k) endif next k Because we must process every element, we use For...Next

Filling arrays at the time that the program is loaded Since the array has been declared as a form- level variable, we can use the Form_Load event procedure to fill an array when the program is first loaded to be run: Private Sub Form_Load() See p. 307, Example 2

To recap... We have several ways to enter data into an array: –from an input file –from interactive user input –from the form_load event procedure

Searching & Sorting

Searching There is a variety of searching algorithms. The fastest ones require that the array elements be sorted (usually in ascending order) first. We study the sequential search, which makes no assumptions about the order of elements.

Find the position of the first occurrence of “$$$” Dim ch (1 To 50) of String … Let pos = 0 Let found = “no” Do While pos < 50 and found = “no” Let pos = pos + 1 if ch(pos) = “$$$” then Let found = “yes” Loop If found = “yes” then Picture1.Print “$$$ was found in position ”; pos Because we need to process only until we find what we’re looking for, we use a While (or Until) loop

Convert the previous code to a function Write a function that receives an array and an object to search for. Return the position of the first occurrence of the item; if the search is unsuccessful, return 0. The function header: Private Function find (object As String) As Integer The function call: picBox.Print find (x) (where x = “$$$” from our previous example)

Using & searching parallel arrays An Inventory File: Dim product_name (1 To 30) As String Dim product_id (1 To 30) As Integer Dim product_cost (1 To 30) As Single Dim num_in_stock (1 To 30) As Integer Dim num_on_order (1 To 30) As Integer We can ask: “How many widgets do we have in stock?”

Sorting There are many sorting algorithms from which to choose. The most straightforward ones are not usually the fastest, but they get the job done & they’re relatively easy to program. Bubble Sort is in this category. –More to come on this

Control arrays (Sec. 7.3) We can organize form objects such as text boxes & labels into arrays. –See Example 1, p. 339: Suppose we have five departments - each one gets its own text box with its own label