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

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Chapter 9: Searching, Sorting, and Algorithm Analysis
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 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.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
C++ for Engineers and Scientists Third Edition
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 7: Working with Arrays
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
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.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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 Simple Program Design Third Edition A Step-by-Step Approach 7.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 71 Arrays Creating and Accessing Arrays Using Arrays Some Additional Types of Arrays.
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.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
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.
Chapter 7 - VB.Net by Schneider1 Chapter 7 – Arrays 7.1 Creating and Accessing Arrays 7.2 Using Arrays 7.3 Control Arrays Skip Structures (7.3), 7.4 and.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Selection Using IF THEN ELSE CASE Introducing Loops.
Arrays 1.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Chapter 7 Arrays.
ARRAYS.
Lbound and Ubound Functions
Chapter 8 Arrays Objectives
MSIS 655 Advanced Business Applications Programming
Chapter 8 Arrays Objectives
Introduction to Computer Programming IT-104
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 27

► Read all examples and comments ► Practice on exercise problems for example: Exercise 7.1: 1-5, 7-11, 14,17,18, 25, 27, 31, 37 Exercise 7.2: 5-8, 11,12, 15,17,21,25 Exercise 7.3: 1,5,9,17,19,21,24,25-28,33,35,37 Exercise 7.4: 1-6, 21, 29,31 Exercise 7.5: 1-4, 8-13, Chapter 7: Arrays (Schneider)

► 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/global 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 Employ a counter variable: See example 2 of Section 7.2 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

► Ordered array vsUnordered array E.g:  Ascending order: [each element]<=[next element] Using Arrays: Ordered Arrays

► Ordered array vsUnordered array => Advantage: Efficient Searching Using Arrays: Ordered Arrays

► Example: Request a name and inform if in the ordered list Using Arrays: Ordered Arrays

Dim nom(1 To 5) As String ‘General Declaration Sub Form_Load Rem Place the names in the array in ascending order Let nom(1) =“AKBAR” Let nom(2) =“ASLAM” Let nom(3) =“BUSHRA” Let nom(4) =“TONY” Let nom(5) =“ZAID” End Sub Using Arrays: Ordered Arrays

Sub Command1_Click Dim n As Integer, name2Find As String Let name2Find = Ucase(Trim(Text1.Text)) Let n=0 Do Let n=n+1 Loop Until (nom(n) >= name2Find) Or (n=5) If nom(n) =name2Find Then Picture1.Print “Found” Else Picture1.Print “Not found” End If End Sub

=> Average search was half the array dimension Using Arrays: Ordered Arrays

Passing Arrays Between Procedure

Sub Command1_Click( ) ReDim score(1 To 5) As Integer Call FillArray(score( )) Picture1.Cls Picture1.Print “Average score is”: Sum(score( ))/5 End Sub Sub FillArray (s( ) As Integer) Let s(1)=85 Let s(2)=92 Let s(3)=75 Let s(4)=68 Let s(5)=84 End Sub Passing Arrays

Function Sum (s( ) As Integer) As Integer Dim total As Integer, index As Integer Rem Add up scores Let total=0 For index =1 To 5 Let total = total + s(index) Next index Sum = total End Function Passing Arrays

► What if the dimension of the array is not known before hand? Ans: Passing on both the array as well its dimension as two arguments will be required Passing Arrays

End