Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix.

Slides:



Advertisements
Similar presentations
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Advertisements

Introduction to arrays
Chapter 9: Advanced Array Manipulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Maths for Computer Graphics
Tirgul no. 6 Topics covered : b Constructors – types of constructors b Two-dimensional and Multi-dimensional arrays in java. b Sorting an array of numbers.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Arrays A variable: a named position of memory For example: x1, x2, x3……..x10 An array: a collection of variables of the same type that are referenced by.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
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.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
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.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
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
Multi-Dimensional Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Matrices Jordi Cortadella Department of Computer Science.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Chapter 8 Arrays and Strings
Lecture 28: Mathematical Insight and Engineering.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Working with Arrays in MATLAB
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.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Multidimensional Arrays Computer and Programming.
Two-Dimensional Arrays. Two-dimensional arrays variables store the contents of tables or matrices. Example: Dim arrTable(1 to 5, 1 to 5) As Integer first.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
1.8 Multidimensional Arrays academy.zariba.com 1.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Chapter 6: Using Arrays.
13.4 Product of Two Matrices
Linear Algebra review (optional)
Lecture 7 Arrays 1. Concept of arrays Array and pointers
MULTI-DIMENSIONAL ARRAY
Chapter 7: Array.
Chapter 7 Arrays.
Chapter 5: Arrays: Lists and Tables
Multidimensional array
Ch 9-10 Subsetting Ordering Array operations Iteration
CIS16 Application Development and Programming using Visual Basic.net
Lets Play with arrays Singh Tripty
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

Arrays

What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix

Why an Array? Read a matrix from a file and write its transpose in another file 1,-3,4 -7,2,9 6,8,5 Need to declare nine variables Write 3 Input & 3 Output statements Lot of complication in re-arrangement Program will fail for 4x4 matrix

Why an Array? An array allows us to use the same name for the data, but distinguish its elements by indices Dim A(1 To 3, 1 To 3) As Integer Row # 1 Row # 2 Row # 3 Column # 1Column # 2Column # 3

Why an Array? For Row = 1 To 3 Input #10, A(Row,1),A(Row,2),A(Row,3) Next Row For Colm = 1 To 3 Write #20, A(1,Colm),A(2,Colm),A(3,Colm) Next Colm

Array Storage A(1,1)A(1,2)A(1,3) A(2,1)A(2,2)A(2,3) A(3,1)A(3,2)A(3,3) A(1,1) A(1,2) A(1,3) A(2,1) A(2,2) A(2,3) A(3,1) A(3,2) A(3,3) A(1,1) A(2,1) A(3,1) A(1,2) A(2,2) A(3,2) …

Using Arrays For Row = 1 To 3 For Column = 1 To 3 Input #10, A(Row,Column) Next Column Next Row For Row = 1 To 3 For Column = 1 To 3 Write #20, A(Column,Row) Next Column Next Row Order of Loops is same

Using Arrays For Row = 1 To 3 For Column = 1 To 3 Input #10, A(Row,Column) Next Column Next Row For Column = 1 To 3 For Row = 1 To 3 Write #20, A(Row,Column) Next Column Next Row Order of Loops is reversed

Arrays Arrays can have multiple dimensions Matrix is a 2-diemnsional array One dimensional arrays »Row Vector »Column Vector Three-dimensional Arrays Multi-dimensional arrays »60 dimensions possible ???????? Storage requirements »A(10,10,10,10,10) uses 100,000 x 2 bytes for integer type data

Array Declaration Dim ArrayOne(1 To 3, 1 To 7) –Two dimensional array of size 3 x 7 Lower bound = 1, Upper bound = 3 and 7 Dim ArrayTwo(10,10) –Two dimensional array of size 11 x 11 Lower bound = 0, Upper bound = 10 Option Base 1 Dim Array3 (10,10) 10 x 10 Array

Matrix Addition C = A + B –C, A, and B are the same size matrices (mxn) For row = 1 To m For column = 1 To n C(row,column) = A(row,column) + B(row,column) Next column Next row

Matrix Multiplication # Columns of A = # Rows of B For j=1 To 3 P(1, j) = P(1, j)+A(1,1)*B(1, j)+A(1, 2)*B(2, j) Next j

Matrix Multiplication For j=1 To 3 P(1, j) = P(1, j) + A(1,1)*B(1, j) + A(1, 2)*B(2, j) Next j For j=1 To 3 P(2, j) = P(2, j) + A(2,1)*B(1, j) + A(2, 2)*B(2, j) Next j For j=1 To 3 P(3, j) = P(3, j) + A(3,1)*B(1, j) + A(3, 2)*B(2, j) Next j

Matrix Multiplication For i = 1 To 3 For j = 1 To 3 P(i, j) = P(i, j) + A(i,1)*B(1, j) + A(i, 2)*B(2, j) Next j Next i For i = 1 To 3 For j=1 To 3 For k=1 To 2 P(i, j) = P(i, j) + A(i,k)*B(k, j) Next k Next j Next i

Matrix Multiplication Assume that A is mxn matrix and B is nxp matrix  P is mxp matrix For i = 1 To m For j = 1 To p For k = 1 To n P(i,j) = P(i,j) + A(i,k)*B(k,j) Next k Next j Next i

Sorting: Bubble Sort Sort the elements of an array A shown below in ascending order. Use the bubble sort method. A=[ ] Solution Step 1: –Compare A(1)=7 and A(2)=3. Swap the numbers as they are not in ascending order.  A=[ ]. –Now compare A(2)=7 and A(3)=2 and swap A=[ ]. –Compare A(3)=7 and A(4)=4  A=[ ]. –A(4)=7 and A(5)=10  No swapping. –Compare A(5)=10 and A(6)=9  A=[ ]. –Compare A(6)=10 and A(7)=0  A=[ ]. –Overall 6 comparisons were needed.  10 in correct place

Sorting: Bubble Sort Step 2: –The highest number in the list is already in its correct place. Exclude this number from any comparison –Repeat step 1 but avoid any comparison with the last entry. This would result in A=[ ].  9 in correct place –We needed 5 comparisons. Step 3: –Repeating 1 on the initial 5 numbers of the array gives A=[ ].  7 in correct place –4 comparisons were needed

Sorting: Bubble Sort Step 4: –A=[ ].  4 in correct place. –3 comparisons Step 5: –A=[ ].  3 in correct place. –2 comparisons. Step 6: –A=[ ].  2 and 0 in correct place. –1 comparison.

Bubble Sort For step = 1 To N-1 For index = 1 To N-run If x(index) > x(index+1) Then Temp = x(index) x(index) = x(index+1) x(index+1) = Temp End If Next index Next step

Bubble Sort Private Sub Command1_Click() Dim X(1 To 1000) As Integer Dim Count As Integer, UserData As String Dim N As Integer, Temp As Integer Picture1.Cls Picture2.Cls For Index = 1 To 1000 UserData = InputBox("Number or End: ") If Left(UCase(UserData), 1) = "E" Then Exit For End If X(Index) = Val(UserData) Next Index

Bubble Sort N = Index - 1 For i = 1 To N Picture1.Print X(i) Next i

Bubble Sort For step = 1 To N - 1 For Index = 1 To N - step If X(Index) > X(Index + 1) Then Temp = X(Index) X(Index) = X(Index + 1) X(Index + 1) = Temp End If Next Index Do While UCase(Left(InputBox("Enter Next for next step"), 1)) <> "N" Picture2.Print "Enter Next" Loop Picture2.Cls For k = 1 To N Picture2.Print X(k) Next k Next step

Bubble Sort Picture2.Cls For j = 1 To N Picture2.Print X(j) Next j End Sub