Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Similar documents
Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Measuring algorithm efficiency

LECTURE 17. Array Searching and Sorting

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

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

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

C++ PROGRAMMING SKILLS Part 4: Arrays

Arrays. Week 4. Assylbek Jumagaliyev

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

Chapter 6 Pointers and Arrays

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Arrays and Linked Lists

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

To become familiar with array manipulation, searching, and sorting.

Introduction to Computer Science Midterm 3 Fall, Points

Pointers: Getting a Handle on Data

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CMPS 221 Sample Final

CSE 373: Data Structures and Algorithms

Chapter 7 : Arrays (pp )

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

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

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 Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Loops / Repetition Statements

Absolute C++ Walter Savitch

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Pointers II. Class 31

CSI33 Data Structures

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

New Concepts. Lab 7 Using Arrays, an Introduction

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Sorting Algorithms. + Analysis of the Sorting Algorithms

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018


Homework #3 CS2255 Fall 2012

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

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

DELHI PUBLIC SCHOOL TAPI

Arrays in C++ Instructor: Andy Abreu

C++ for Java Programmers

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

REPETITION CONTROL STRUCTURE LOGO

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

7.1 Optional Parameters

Help File on C++ and Programming at CSUSM by Rika Yoshii

FORM 2 (Please put your name and form # on the scantron!!!!)

CS201 Latest Solved MCQs

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

6. Pointers, Structs, and Arrays. 1. Juli 2011

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

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:


Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

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

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):

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Abstract Data Type: Stack

UNIT- 3 Introduction to C++

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

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

What Is a Function? Illustration of Program Flow

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

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

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

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

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Sorting Pearson Education, Inc. All rights reserved.

Introduction to Scientific Computing and Problem Solving

Memory and C++ Pointers

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

BITG 1113: Array (Part 1) LECTURE 8

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

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

CS 216 Exam 1 Fall SOLUTION

Transcription:

Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

An array is a sequence of values of the same type Like a list of values Each value is a single element of the list An array is a collection of values The entire collection can be referenced, or Its individual elements

Let s say we want to write a program to input grades for a course Assume that 44 students are registered Input each grade, one at a time, and store it Once the grades are input we can use them to calculate the average grade

In a world without arrays (or similar structures) we need a separate variable for each grade double grade1 = 0; double grade2 = 0; double grade3 = 0; double grade42 = 0; double grade43 = 0; double grade44 = 0; Let's assume that these are global variables declared outside the main function

We need to input the data into each variable We also need to make sure that grades are entered in the correct variables if (count == 1) cin >> grade1; else if (count == 2) cin >> grade2; else if (count == 44) cin >> grade44; Assume that input is from the keyboard This big, ugly IF statement should go in an input function And could be written as a switch statement

Assume that all of the grades have been entered We can now calculate the average Let s make a separate function to do this double getaverage( ) Ideally, functions should be self contained Input is passed using a parameter list And the desired output is returned But we would need 44 parameters for this function! So, let s just use the previously defined global variables Which is bad, because global variables

double getaverage (){ double sum = 0; sum += grade1; sum += grade2; sum += grade3; sum += grade4; sum += grade5; sum += grade6; sum += grade7; sum += grade8; sum += grade9; This is what the getaverage function would look like

// sum += grade10; sum += grade11; sum += grade12; sum += grade13; sum += grade14; sum += grade15; sum += grade16; sum += grade17; sum += grade18; sum += grade19; You can t put this in a loop because the variable that is added is different each time

// sum += grade20; sum += grade21; sum += grade22; sum += grade23; sum += grade24; sum += grade25; sum += grade26; sum += grade27; sum += grade28; sum += grade29; Good thing it s not a class with 250 students in it or this would be really boring

// sum += grade30; sum += grade31; sum += grade32; sum += grade33; sum += grade34; sum += grade35; sum += grade36; sum += grade37; sum += grade38; sum += grade39; Are we there yet?

// sum += grade40; sum += grade41; sum += grade42; sum += grade43; sum += grade44; return sum / 44; Right, done, that s it!

Here is the same function, this time assuming that the grade data was stored in an array double getaverage(double arr[], int size){ double sum = 0; for (int i = 0; i < size; ++i){ sum += arr[i]; return sum / size;

An array variable is a collection of other variables You can think of an array as something that contains variables This is important because an integer array is not an integer, it is a collection of integers The items stored in an array (elements) are stored sequentially in main memory This is an important implementation issue

An array is declared with a type, and []s to indicate that the variable is an array The type is the type of the contents of the array size of the array int score[10]; type of the data stored in the array brackets declare the variable as an array

The elements of the array are accessed using an index Indexes are the addresses of the elements The first element always has an index of 0 The last index is always array size 1 Array indexes follow the name of the array and are enclosed in []s Individual array elements are used in exactly the same way as variables

int arr[4]; int x; for(int i = 0; i < 4; ++i){ arr[i] = i + 1; x = arr[2]; //access 3 rd. element represents stack memory, where each cell represent 4 bytes 2048 2052 2056 2060 2064 2068 1 2 3 4 3 4 arr[0] arr[1] arr[2] arr[3] x i result of the statements shown above

Array elements have to be given values individually Although this can be performed in a loop There is a special shorthand notation for initializing arrays when they are declared int arr[] = {1, 3, 7, 9; This shorthand notation is only allowed on the same line as the declaration

Array size must be specified when the array is declared It must be a literal (a number) or A constant The size can not be given using a variable Therefore an array's size cannot change during the life of a program It is specified at compile time

Arrays are often processed using loops Each element in an array is accessed in turn The loop control variable is used as an index into the array The loop terminates once every element of the array has been processed When the loop control variable is equal to the size of the array

// Assume an int array named arr, // and a constant called ARR_SIZE // The loop prints the contents of arr for (int i = 0; i < ARR_SIZE; i++){ cout << arr[i] << endl; The condition is i < ARR_SIZE because the last legal index is ARR_SIZE 1

// Assume an int array named arr, // and a constant called ARR_SIZE // The loop prints the contents of arr for (int x : arr){ cout << x << endl; This alternate form of the for loop is designed to iterate through containers Introduced in C++ 11, so may require a flag to compile Only works with static arrays i.e. arrays discussed to date in this presentation

// Assume an int array named arr, // and a constant called ARR_SIZE // The loop prints the contents of arr int i = 0; while (i < ARR_SIZE){ cout << arr[i] << endl; i++; A very similar loop to the original for loop, don't forget to increment the index, i

It is much better to define array size as a constant than to use a literal value If the programmer wants to change the array size this only needs to be done once Use the constant whenever the array size is referenced, and Avoid using magic numbers!

What happens if a program attempts to access a non-legal index? Some sort of error occurs, either An illegal attempt to access a memory location has been made ( stack is corrupted ), or Something less predictable Always ensure that an index is legal Between 0 and array size 1

Make sure that you distinguish between an array and its contents int arr[4]; arr is the entire array arr[1] is one element of the array The array is a container of integers, one element is an integer They are not interchangeable

It is often important to know how much of an array is used Unassigned elements shouldn't be processed e.g. summing or calculating the average of an array Consider what input a function requires The array, and its maximum size The array, and current size i.e. the number of elements actually used The array, its maximum size, and its current size

Arrays arguments can be passed to functions So parameter lists can include arrays The array type, and the fact that it is an array must be specified in the parameter list An array is specified just like any declaration A function header for an array sum function int sumarray(int arr[], int size){

// Sums the array, returning the sum int sumarray(int arr[], int size){ int sum = 0; for (int i = 0; i < size; i++){ sum += arr[i]; return sum; size is used to specify the size of the array To use this function give it the appropriate arguments like any other function x = sumarrray(myarr, ARR_SIZE); notice no []s, it's the name of the array variable, not its declaration

A common array task is to search an array for a particular value int search(int arr[], int size, int x){ for (int i = 0; i < size; i++){ return -1; if (arr[i] == x) return i; Return the index of the target as soon as it is found Return -1 if the target is not found, as it is not a valid array index

What happens if you pass an array to a function that changes the array parameter? void doublearray(int arr[], int size){ for (int i = 0; i < size; i++){ arr[i] = arr[i] * 2; The function doubles each of the values stored in the array parameter

int main() { int arr[] = {1, 2, 3; doublearray(arr, ARR_SIZE); for(int i = 0; i < 3; ++i){ cout << arr[i] << endl; return 0; What has happened? The elements of the array passed to the function have changed This normally doesn't happen with arguments unless they are pass by reference

To understand what is going on in the previous example we need to know more It's easy to think of an array as a container, like a bookcase But a bookcase is a structure in its own right An array is just a collection of values All of the same type, and Stored in sequence in main memory

An array is a sequence of bytes in main memory reserved for the array contents e.g. int arr[10]; Reserves 40 contiguous bytes (4 byte ints, 4 * 10 = 40) Each element can be referenced using indexes And arr stores the address of the first array element These two statements print the same address cout << arr << endl; cout << &arr[0] << endl;

An array variable stores the address of the array's first element Referred to as a pointer to the array The index operator ([]) allows access to the element of the array stored at the given index Passing an array variable to the function gives the function the address of the array In a similar way to pass by reference Although the mechanism is somewhat different

Consider this assignment statement: arr[8] = 23; To find this array element Look up the address stored in arr Multiply type size (4 for an int) by the index Add this to the address to find the element Known as an offset calculation

Be careful when specifying loop conditions Particularly when working with arrays Being off by one with an index value may result in a runtime error Or worse... Where possible use <, >, <=, or >= as conditions rather than == or!= As an example of this we will look at a function to reverse the contents of an array

// Function to print a small integer array on one line void printarray(int arr[], int size) { cout << "{"; for(int i=0; i < size; i++) Note i!= size is not used here { cout << arr[i]; if(i!= size-1){ cout << ","; Commas separate array elements cout << "";

Now let's write the function to reverse an array // Function to reverse the contents of an array void reverse(int arr[], int size) Comma operator { int lo, hi; for(lo=0, hi=size-1; lo!=hi; lo++, hi--){ int temp = arr[lo]; arr[lo] = arr[hi]; arr[hi] = temp; This version is broken!

int arr[] = {1,2,3,4,5,6,7; int arr_size = sizeof(arr)/sizeof(int); printarray(arr, arr_size); reverse(arr, arr_size); cout << endl << endl; printarray(arr, arr_size); so far, so good... int arr[] = {1,2,3,4,5,6,7,8; int arr_size = sizeof(arr)/sizeof(int); printarray(arr, arr_size); reverse(arr, arr_size); cout << endl << endl; printarray(arr, arr_size); the for condition should have been lo < hi they didn`t...

As noted previously array size must be given a constant value And the size of an array cannot be changed while the program is running (during run-time) This is to support the organization of stack memory It's possible to allocate memory at run-time From a free store of memory, called dynamic memory more on this later...

Arrays can be returned from a function However the obvious way of specifying the return type is illegal This doesn't work int[] getanarray() { In addition, returning an array raises the question of how big it is more on this later...

Assume we want to store monthly rainfall in Vancouver for five years So sixty data points, in an array of size 60 What is the index for September of year 3? It would be more convenient to store the data in a table With years as columns and months as rows This is possible using a multidimensional array

To declare a multidimensional array you declare an array of arrays float rainfall[5][12]; It is an array of 5 arrays of size 12 Where rainfall[0] is the first array of size 12 To access elements of the inner array, use two indices So September of year 3 is rainfall[2][8] Remember 0 based indexing

To initialize a multidimensional array use nested for loops Or the same initialization syntax as regular arrays Note that multidimensional arrays are not limited to two dimensions

void testmatrix() { int matrix[3][5] = { {1,2,3,4,5, {11,12,13,14,15, {21,22,23,24,25 ; don t need the inner brackets but they make it easier to read int row, column; for(row = 0; row < 3; ++row){ for(column = 0; column < 5; ++column){ cout << setw(4) << matrix[row][column]; cout << endl;

It is sometimes useful that a function changes an array passed to it A common example of this is sorting an array There are many different sorting algorithms Selection sort, Insertion sort, Bubble sort,... Quicksort, Mergesort, Radix sort,... The algorithms differ in how fast they are And how much main memory space they require And how they are affected by the input organization

Selection sort is a simple sorting algorithm that repeatedly finds the smallest item The array is divided into a sorted part and an unsorted part Repeatedly swap the first unsorted item with the smallest unsorted item Starting with the element with index 0, and Ending with last but one element (index n 1)

23 41 33 81 07 19 11 45 find smallest unsorted - 7 comparisons, swap with 23 07 41 33 81 23 19 11 45 find smallest unsorted - 6 comparisons, swap with 41 07 11 33 81 23 19 41 45 find smallest unsorted - 5 comparisons, swap with 33 07 11 19 81 23 33 41 45 find smallest unsorted - 4 comparisons, swap with 81 07 11 19 23 81 33 41 45 find smallest unsorted - 3 comparisons, swap with 81 07 11 19 23 33 81 41 45 find smallest unsorted - 2 comparisons, swap with 81 07 11 19 23 33 41 81 45 find smallest unsorted - 1 comparison, swap with 81 07 11 19 23 33 41 45 81

void selectionsort(int arr[], int size){ for(int i = 0; i < size -1; ++i){ int smallest = i; // Find the index of the smallest element for(int j = i + 1; j < size; ++j){ if(arr[j] < arr[smallest]){ smallest = j; // Swap the smallest with the current item int temp = arr[i]; arr[i] = arr[smallest]; arr[smallest] = temp; outer loop inner loop

int getsmall(int arr[], int start, int end){ int smallest = start; for(int i = start + 1; i < end; ++i){ if(arr[i] < arr[smallest]){ smallest = i; return smallest; void swap(int arr[], int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; void selectionsort(int arr[], int size){ for(int i = 0; i < size -1; ++i){ int smallest = getsmall(arr, i, size); swap(arr, i, smallest);

When designing an algorithm its efficiency is an important consideration i.e. How fast does it run? The speed of an algorithm is affected by a number of factors The computer The algorithm The Input

The computer that an algorithm is run on will affect its speed How fast is the processor? How much main memory does it have? What else is it doing at the same time? Does the algorithm require specialized hardware? Such as a graphics card (GPU) If so, how powerful is this hardware?

The input to the algorithm will also affect its speed What is its size? What kind of container is it stored in? How is that container implemented? What is its organization?

When assessing algorithm efficiency it is often useful to ignore The computer it is run on and Any one actual input size The efficiency of an algorithm can be expressed as a formula That is a function Which may be affected by input size, which is referred to by a variable, n

Let's examine some examples Linear search Reverse Selection sort We'll start by running them on a computer with varying sizes of input Making some preliminary observations And then reasoning about them