Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Similar documents
Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Computer Programming: C++

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

BITG 1113: Array (Part 1) LECTURE 8

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

C++ PROGRAMMING SKILLS Part 4: Arrays

Introduction to Computer Science Midterm 3 Fall, Points

Computer Science & Engineering 150A Problem Solving Using Computers

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Spring 2010 Java Programming

6. User Defined Functions I

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Chapter 7 Array. Array. C++, How to Program

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

How to declare an array in C?

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Arrays. Week 4. Assylbek Jumagaliyev

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Java Programming: Program Design Including Data Structures. Chapter Objectives

Programming for Engineers Arrays

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Lecture 04 FUNCTIONS AND ARRAYS

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Chapter 6 Pointers and Arrays

Lecture 6 Sorting and Searching

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Procedural Programming

Review of Important Topics in CS1600. Functions Arrays C-strings

Chapter 9 Introduction to Arrays. Fundamentals of Java

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Programming for Electrical and Computer Engineers. Pointers and Arrays

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Instructor: Eng.Omar Al-Nahal

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Arrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.

by Pearson Education, Inc. All Rights Reserved.

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Arrays and Applications

Multiple-Subscripted Arrays

Principles of Programming. Chapter 6: Arrays

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Review of the C Programming Language for Principles of Operating Systems

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Chapter 10 - Notes Applications of Arrays

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

UNIT- 3 Introduction to C++

Subject: Computer Science

Introduction to Arrays

C Arrays Pearson Education, Inc. All rights reserved.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Module 6: Array in C

Chapter 8 Algorithms 1

Exam 3 Chapters 7 & 9

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Ar r ays and Pointer s

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Lecture #8-10 Arrays

Chapter 6: Using Arrays

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

type arrayname [ size type arrayname [ size ] = { item ,, item size size

Chapter 2: Basic Elements of C++

Lecture 3 Tao Wang 1

Chapter 7 : Arrays (pp )

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Objectives. In this chapter, you will:

Functions. Lecture 6 COP 3014 Spring February 11, 2018

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter 12 Two-Dimensional Arrays

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

CS201 Some Important Definitions

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Review of the C Programming Language

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Transcription:

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of array index out of bounds Become familiar with the restrictions on array processing Discover how to pass an array as a parameter to a function Learn how to implement dynamic array Discover how to manipulate data in a two-dimensional array, multidimensional arrays Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 2

In this lecture, you will: Learn how to implement the sequential search algorithm Explore how to sort an array using the bubble sort Explore how to sort an array using the selection sort, and insertion sort algorithms Learn how to implement the binary search algorithm Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 3

A data type is called simple if variables of that type can store only one value at a time A structured data type is one in which each data item is a collection of other data items Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 4

Array: a collection of a fixed number of components wherein all of the components have the same data type In a one-dimensional array, the components are arranged in a list form Syntax for declaring a one-dimensional array: intexp evaluates to a positive integer call Index Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 5

Example: int num[5]; Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 6

General syntax: where indexexp, called an index, is any expression whose value is a nonnegative integer Index value specifies the position of the component in the array [] is the array subscripting operator The array index always starts at 0 Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 7

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 8

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 9

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 10

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 11

Some basic operations performed on a onedimensional array are: Initializing Inputting data Outputting data stored in an array Finding the largest and/or smallest element Each operation requires ability to step through the elements of the array Easily accomplished by a loop Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 12

Consider the declaration int list[100]; //array of size 100 int i; Using for loops to access array elements: for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2 Example: for (i = 0; i < 100; i++) //Line 1 cin >> list[i]; //Line 2 Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 13

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 14

If we have the statements: double num[10]; int i; The component num[i] is valid if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 The index of an array is in bounds if the index >=0 and the index <= ARRAY_SIZE-1 Otherwise, we say the index is out of bounds In C++, there is no guard against indices that are out of bounds Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 15

Arrays can be initialized during declaration In this case, it is not necessary to specify the size of the array Size determined by the number of initial values in the braces Example: double sales[] = {12.25, 32.50, 16.90, 23, 45.68}; Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 16

The statement: int list[10] = {0}; declares list to be an array of 10 components and initializes all of them to zero The statement: int list[10] = {8, 5, 12}; declares list to be an array of 10 components, initializes list[0] to 8, list[1] to 5, list[2] to 12 and all other components are initialized to 0 Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 17

The statement: int list[] = {5, 6, 3}; declares list to be an array of 3 components and initializes list[0] to 5, list[1] to 6, and list[2] to 3 The statement: double sales[25]= {4.5, 7.2}; declares an array of 25 components; initializes list[0] to 4 and list[1] to 7; all other components are initialized to 0 Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 18

Consider the following statements: C++ does not allow aggregate operations on an array: Solution: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 19

The following is illegal too: Solution: The following statements are legal, but do not give the desired results: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 20

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 21

Arrays are passed by reference only The symbol & is not used when declaring an array as a formal parameter The size of the array is usually omitted If provided, it is ignored by the compiler Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 22

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 23

The base address of an array is the address, or memory location of the first array component If list is a one-dimensional array, its base address is the address of list[0] When we pass an array as a parameter, the base address of the actual array is passed to the formal parameter Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 24

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 25

C++ does not allow functions to return a value of the type array Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 26

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 27

Dynamic array: array created during the execution of a program Example: int *p; p = new int[10]; *p = 25; p++; //to point to next array component *p = 35; stores 25 into the first memory location stores 35 into the first memory location C++ Programming: From Problem Analysis to Program Design, Fourth Edition 28

C++ allows us to use array notation to access these memory locations The statements: p[0] = 25; p[1] = 35; store 25 and 35 into the first and second array components, respectively C++ Programming: From Problem Analysis to Program Design, Fourth Edition 29

The value of list (1000) is constant Cannot be altered during program execution The increment and decrement operations cannot be applied to list If p is a pointer variable of type int, then: p = list; copies the value of list, the base address of the array, into p We can perform ++ and -- operations on p An array name is a constant pointer C++ Programming: From Problem Analysis to Program Design, Fourth Edition 31

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 33

Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two dimensions Sometimes called matrices or tables Declaration syntax: where intexp1 and intexp2 are expressions yielding positive integer values, and specify the number of rows and the number of columns, respectively, in the array Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 34

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 35

Syntax: where indexexp1 and indexexp2 are expressions yielding nonnegative integer values, and specify the row and column position Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 36

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 37

Two-dimensional arrays can be initialized when they are declared: Elements of each row are enclosed within braces and separated by commas All rows are enclosed within braces For number arrays, if all components of a row aren t specified, unspecified ones are set to 0 Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 38

Ways to process a two-dimensional array: Process the entire array Process a particular row of the array, called row processing Process a particular column of the array, called column processing Each row and each column of a twodimensional array is a one-dimensional array To process, use algorithms similar to processing one-dimensional arrays Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 39

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 40

To initialize row number 4 (i.e., fifth row) to 0 To initialize the entire matrix to 0: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 41

To output the components of matrix: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 42

To input data into each component of matrix: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 43

To find the sum of row number 4 of matrix: To find the sum of each individual row: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 44

To find the sum of each individual column: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 45

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 46

Before: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 47

To reverse both the diagonals: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 48

After: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 49

Two-dimensional arrays can be passed as parameters to a function Pass by reference Base address (address of first component of the actual parameter) is passed to formal parameter Two-dimensional arrays are stored in row order When declaring a two-dimensional array as a formal parameter, can omit size of first dimension, but not the second Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 50

Function Prototype: void PrintArray (int a[] [3]); Function Calling: Int x[5] [3]; PrintArray (x); Function definiation: void PrintArray (int m[] [3]){ } Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 51

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 52

Multidimensional array: collection of a fixed number of elements (called components) arranged in n dimensions (n >= 1) Also called an n-dimensional array Declaration syntax: To access a component: Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 53

When declaring a multidimensional array as a formal parameter in a function Can omit size of first dimension but not other dimensions As parameters, multidimensional arrays are passed by reference only A function cannot return a value of the type array There is no check if the array indices are within bounds Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 54

Malik, D.S, C++ Programming: From Problem Analysis to Program Design, Third Edition 55

To search a list, you need: The list (array) containing the list List length Item to be found After the search is completed If found: Report success Location where the item was found If not found, report failure C++ Programming: From Problem Analysis to Program Design, Fourth Edition 56

Sequential search: search a list for an item Compare search item with other elements until either: Item is found List has no more elements left Average number of comparisons made by the sequential search equals half the list size Good only for very short lists C++ Programming: From Problem Analysis to Program Design, Fourth Edition 57

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 58

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 59

Suppose list is a list of n elements In n-1 iterations compare elements list[index] and list[index + 1] If list[index] > list[index + 1], then swap them C++ Programming: From Problem Analysis to Program Design, Fourth Edition 60

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 61

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 62

For a list of length n, on average, a bubble sort makes n(n 1)/2 key comparisons C++ Programming: From Problem Analysis to Program Design, Fourth Edition 63

Selection sort: rearrange list by selecting an element and moving it to its proper position Find the smallest (or largest) element and move it to the beginning (end) of the list C++ Programming: From Problem Analysis to Program Design, Fourth Edition 64

On successive passes, locate the smallest item in the list starting from the next element C++ Programming: From Problem Analysis to Program Design, Fourth Edition 65

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 66

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 67

The insertion sort algorithm sorts the list by moving each element to its proper place C++ Programming: From Problem Analysis to Program Design, Fourth Edition 68

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 69

Average key comparisons: (n 2 + 3n 4)/4 C++ Programming: From Problem Analysis to Program Design, Fourth Edition 71

Binary search can be applied to sorted lists Uses the divide and conquer technique Compare search item to middle element If search item is less than middle element, restrict the search to the lower half of the list Otherwise search the upper half of the list C++ Programming: From Problem Analysis to Program Design, Fourth Edition 72

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 73

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 74

C++ Programming: From Problem Analysis to Program Design, Fourth Edition 75

Every iteration cuts size of search list in half If list L has 1000 items At most 11 iterations are needed to search for x Every iteration makes two key comparisons Binary search makes at most 22 key comparisons to determine if x is in L Sequential search makes 500 key comparisons (average) to determine if x is in L for the same size list C++ Programming: From Problem Analysis to Program Design, Fourth Edition 76