Introduction to Computing Dr. Nadeem A Khan. Lecture 9.

Slides:



Advertisements
Similar presentations
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Advertisements

EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
Variables and Constants
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
VBA Modules, Functions, Variables, and Constants
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
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.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 17.
Introduction to Computing Dr. Nadeem A Khan. Lecture 19.
Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 14.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
Chapter 5 - Visual Basic Schneider1 Chapter 5 Decisions.
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 Computing Dr. Nadeem A Khan. Lecture 18.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Chapter 7 - Visual Basic Schneider
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.
IE 212: Computational Methods for Industrial Engineering
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: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Review for Exam 2 School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 10, Friday 3/21/2003) - IF Blocks - Do Loops - Select.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
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.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Arrays. Variable Arrays a group of variables that have the same name and data type each element in a variable array is identified by a subscript refer.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Lecture Set 12 Sequential Files and Structures Part D - Structures.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of 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.
Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
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.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
REEM ALMOTIRI Information Technology Department Majmaah University.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Introduction to Programming Lecture 5
Chapter 7 Arrays.
ARRAYS.
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 9

Select Case Blocks

► Example: Sub Command1_Click ( ) Picture1.Cls Let num = Val(Text1.Text) Select Case num Case 1 Picture1.Print “Buckle my shoe.” Case 2 Picture1.Print “Pick the sticks.” Case 3,4,5,6 Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Select Case Block ► General form Select Case selector Case valueList1 action1 Case valueList2 action2.. Case Else action of last resort End Select

Another example

Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Read Chapter 5 completely!

Chapter 7: Array Variables (Arrays)

► Array:  A list of values can be assigned  Collection of variables of the same type Array Variables (Arrays)

► Array Declaration: Dim arrayName (1 to n) As varType e.g: Dim names(1 To 3) as Strings Dim scores(1 To 10) as Single Array Variables (Arrays)

► Value assignment: Dim names(1 To 3) as Strings Let names(1)=“Aslam” Let names(2)=“Khalid” Let names(3)=“Akbar” Picture1.Print names(3), names(2),names(1) Array Variables (Arrays)

► What is what? Dim scores(1 To 3) as Single scores( ): name of the array scores(1): first element scores(2): second element scores(3): third element 1, 2, 3: subscripts Array Variables (Arrays)

► What are the ranges or dimensions? Dim scores(1 To 3) as Single Dim names(1 To 10) as String Array Variables (Arrays)

► Value assignment: Dim names(1 To 3) as String Let names(1)=“Aslam” Let names(2)=“Khalid” Let names(3)=“Akbar” Picture1.Print names(3), names(2),names(1) Arrays

► Range of an Array: Dim arrayName (1 to n) As varType Dim arrayName (m to n) As varType e.g: Dim names(1 To 3) as String Dim scores(10 To 20) as Single => Range of array need not begin with 1 Note:Dim value(5) As Integer =Dim value (0 To 5) As Integer Arrays

► Example1: Display the name of the student from a class of 5 who got the rank specified by the user Arrays

Dim student(1 To 5) As String Sub Form_Load ( ) Rem Fill data in array Let student(1)= “Samina” Let student(2)=“Akbar” Let student(3)=“Rizwan” Let student(4)=“Aslam” Let student(5)=“Iram” End Sub Arrays

Sub Command1_Click( ) Dim n As Integer Rem Access the student array Let n=Val(Text1.Text) Picture1.Cls If n<=5 Then Picture1.Print student(n); “ got rank”; n Else Picture1.Print “Rank”; n; “does not exist” End If End Sub Arrays

=> For repeated usage it is efficient to declare a form-level array and assign data once in the Form-Load event Arrays

► If the amount of data is not known in advance? Arrays

Solution 1: Declare an array that is large enough and use part of it Arrays

► If the amount of data is not known in advance? Solution 2: Use ReDim (Dynamic Array) Arrays

► ReDim Declaration: ReDim arrayName (1 to n ) As varType e.g: ReDim score(1 to numStudents) As Single => subscripts: variables and expressions => Can be placed only within a procedure Arrays

► Declaration as a Local variable: ReDim arrayName (1 to n ) As varType within a procedure => Created and erased in memory each time the procedure is called procedure is calledArrays

► Declaration as Form-Level variable:  Place as General declaration: Dim arrayName ( ) As varType  Place in one procedure ReDim arrayName (1 to n ) As varType =>only after ReDim has been executed: - its range is established - useable only after it Arrays

► Example 2: Display the name of the student from a class of who got the rank specified by the user Arrays

► Given in example 2:  The name of the students are in a file (CLASS.TXT) arranged according to their ranks  Class strength is given as the first item of the file Arrays

Dim student( ) As String Dim classStrength As Integer Sub Form_Load ( ) Dim i As Integer Rem Fill data in array Open “CLASS.TXT” For Input As #1 Input #1, classStrength ReDim student (1 To classStrength) As String For i=1 To classStrength Input #1, student(i) Next i Close #1 End Sub Arrays

Sub Command1_Click( ) Dim n As Integer Rem Access the student array Let n=Val(Text1.Text) Picture1.Cls If n<=classStrength Then Picture1.Print student(n); “ got rank”; n Else Picture1.Print “Rank”; n; “does not exist” End If End Sub Arrays

► What if the class strength is not there as the first item? Arrays