Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales.

Slides:



Advertisements
Similar presentations
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Advertisements

Practical Programming COMP153-08S Lecture: Repetition Continued.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
COPYRIGHT 2003: Dr. David Scanlan, CSUS OBJECTIVES: Explain the need for arrays. Coding an array. Basic algorithms: Largest, Smallest, Sum, Standard Deviation,
Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Programming Based on Events
Control structures Part 2 iteration control To enable repetition of a statement block.
Odds and Ends Component Tray Menu and contextmenu Splash Screen.
An array of controls Not particularly convenient in VB Examples: array of pictureboxes Array of textboxes Notes on Concentration game (a possible final.
Two dimensional arrays. A two dim array is doubly subscripted… It has rows and columns Tables of data are examples of two-dim arrays. As with one-dim.
Arrays & Strings part 2 More sophisticated algorithms, including sorting.
Additional loop presentation
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
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.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Using Arrays and File Handling
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Lecture 8 Visual Basic (2).
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Visit to download CodeAwesomeness.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Some graphics. Projects included A slideshow a dark moon moving phases of the moon billiards Your own icons and bitmaps Pie chart.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Tutorial 6 The Repetition Structure
CS0004: Introduction to Programming Project 1 – Lessons Learned.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
1 Advanced Computer Programming Lab Calculator Project.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
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.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Pay Example (PFirst98) Please use speaker notes for additional information!
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Directory and File. Access Files in a Directory Name space: System.IO The Directory and File classes contain only shared methods that set or return information.
Arrays 1.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
Single Dimensional Arrays
للمزيد زورونا على موقعنا الإلكتروني:
Visual Basic..
GUI Programming in Visual Studio .NET
Presentation transcript:

Arrays One dimensional arrays

Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales

arrays Arrays are multi-entry data structures. One dimensional arrays have a fixed length associated with the array name, once they have been built. –For this reason (one-dim) arrays are called “linear” data storage –For this reason arrays are called “static” data storage – the allocation may not be changed later. One dimensional arrays have a datatype associated with them, the type they may hold. For this reason arrays are called “homogeneous”.

Arrays Arrays are declared specifying a size. Examples might be: Dim calendarEntries(365) as Date Dim bestFriends(3) as String Dim list(5) as integer Dim values(25) as Double Arrays can be allocated at compile time, in which case their size is calculated based on how many entries are provided, as in Dim numbers as double()={1.1,2.2,3.3}

arrays Because arrays have fixed size, once allocated, loops are useful for processing them. Array indices are zero-based (count subscripts starting at zero) just as String indices are in VB. There are a number of fields/methods useful for arrays such as: length, getType, initialize(), reverse(), lastIndexOf

A loop is generally used to process an array ‘ if we have built an array: Dim list(5),i as integer ‘ then later…. For i=0 to list.length-1 ‘ process elements here… for example List(i)=0 ‘a typical initialization Next

We could “glue together” names Dim longstring as string=“ “ Dim picnames As String() = {"lisa", "bart", "dollars", "robot“_,"booper", "shakespeare", "nerd"} For i=0 to picnames.length-1 Longstring = longstring & “ “ & picnames(i) Next

We could search for a name Dim picnames As String() = {"lisa", "bart", "dollars", "robot“_,"booper", "shakespeare", "nerd"} Dim name as string Dim found as boolean=false Name=txtinput.text For i=0 to picnames.length-1 If name= picnames(i) then Found=true End if Next

A form which displays random pictures Uses our array of names since I found those gif files. Uses techniques we used in our “flags-of- the-world” application to display pictures

A form which displays random pictures

Clicking random button

This form uses A string array declared as a field value and initialized at time of declaration A random object to pick random entries – remember parens are used when allocating an object with the new keyword A picture box to display pictures Gets the image from a file using system.io Dim randomguy As Random = New Random() Dim picnames As String() = {"lisa", "bart", "dollars", "robot", "booper", "shakespeare", "nerd"}

Random class The next (or nextint()) function gets the next random value. You can give the next a start and final value and it will pick a random int from start to final-1 If you use random class to generate reals, it gives reals in the range (0,1). To get other reals multiply and shift.

Code for btnrandom Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click Dim val As Integer val = randomguy.Next(0, picnames.Length) picture.Image = Image.FromFile(directory.GetCurrentDirectory & picnames(val) & ".gif") End Sub

Debugger note In VB 2003 the debugger expects files to be named “bin”&name.gif since the debug version of your project is in a bin subdirectory We noticed in VB2005 it didn’t work the same.

More on arrays and the debug window

For each item in array…next For each is a useful mechanism when the number of items in an array is not known: Private Sub Btndisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisplay.Click Dim Name As String For Each Name In names Debug.WriteLine(Name) ’write to debug window Next End Sub

Now display to listbox

Array allocation and button code Dim names As String() = {"bob", "sue", "mary", "jeremiah", "opie", "jezebel"} ‘put on one line or use line continue ‘this is compile time array allocation Private Sub Btndisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisplay.Click Dim Name As String For Each Name In names Debug.WriteLine(Name) lstdisplay.Items.Add(Name) Next End Sub

Common activites Arrays often need to be searched for a specific value Largest or smallest value may be sought. Arrays may need to be summed (if contents are numeric)

Allocate an array and sum its contents Dim values As Double() = {1.2, 2.3, 3.4, 5.6, 6.7, 7.8, 8.9, 9.0} Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sum As Double = 0 'set sum to zero…additive identity element Dim guy As Double For Each guy In values sum += guy Next MessageBox.Show("sum is" & sum, "The sum", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub

Find the largest (or smallest)

Revised loadform code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim big As Double = values(0) 'set big to first one Dim guy As Double For Each guy In values If guy > big Then big = guy End If Next MessageBox.Show("largest is" & big, "findbiggest", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub

Look for mary

Mary found

Look for bozo

Can’t find him

One version of search code: remember, the user may need a reminder about case-sensitivity Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click Dim search, guy As String Dim found As Boolean = False search = txtguy.Text ‘value in textbox For Each guy In names If guy = search Then MessageBox.Show(search & "is found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) found = True End If Next 'after loop you'll know if he's not there If Not found Then MessageBox.Show(search & "is not found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) End If End Sub

Another version of search code Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click Dim search, guy As String Dim found As Boolean = False search = txtguy.Text ‘value in textbox For Each guy In names If guy = search Then found = True End If Next 'after loop you'll know if he's not there If found Then MessageBox.Show(search & "is found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) else MessageBox.Show(search & "is not found", "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) End If End Sub

Yet another version of search code Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click Dim search, guy As String Dim message as String = “Not there” ‘ must assume won’t be found search = txtguy.Text ‘value in textbox For Each guy In names If guy = search Then Message=“Is there” End If Next 'after loop you'll know if he's not there…no if needed MessageBox.Show(search & message, "results", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub

Car sales

We have several employees and car models This application uses a combobox You can populate it from the properties window by clicking the ellipsis and entering strings into the dialog box

combobox

Arrays needed for car sales ‘which one? Dim list As Integer() = {0, 0, 0, 0} ‘ name of car sold Dim values(4) As String ’car costs Dim prices as double={50000,200000,30000,12000} ‘worker names Dim employees() as String={“names”,”go”,”here”}

Keep track of individual employee sales