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

Slides:



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

Variables and Constants
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
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 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 11.
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.
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.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
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
© 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.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
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.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Arrays 1.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Arrays and Collections
VBA - Excel VBA is Visual Basic for Applications
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 7: Working with Arrays
Introduction to Programming Lecture 5
Chapter 8 Arrays Objectives
Chapter 7 Arrays.
2. Understanding VB Variables
JavaScript: Functions.
ARRAYS.
Department Array in Visual Basic
Visual Basic 6 Programming.
Introduction To Programming Information Technology , 1’st Semester
MSIS 655 Advanced Business Applications Programming
Arrays Topics Definition of a Data Structure Definition of an Array
Building Java Programs
Review for Test1.
Intro to Programming Concepts
To understand what arrays are and how to use them
CS149D Elements of Computer Science
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Topics Definition of a Data Structure Definition of an Array
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Introduction to Computer Programming IT-104
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 24

Assignment 5 has been issued

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(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

► What if the class strength is not there as the first item in the data file? Ans: Count the number of items in the file first and use it to dimension (ReDim) the array Arrays