More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Similar documents
Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

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

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Pre- Defined Func-ons in C++ Review for Midterm #1

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Search,Sort,Recursion

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

Dynamic Arrays Makefiles and Mul3ple File Compiles

CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Wednesday, 11/18/2009

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

CS & IT Conversions. Magnitude 10,000 1,

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Welcome to Solving Problems with Computers I

Class Note #02. [Overall Information] [During the Lecture]

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

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

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division. SECOND SEMESTER Course Handout Part II

Computer Programming: C++

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Getting started with C++ (Part 2)

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

You must pass the final exam to pass the course.

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Central Washington University Department of Computer Science Course Syllabus

CS3: Introduction to Symbolic Programming. Lecture 5:

Announcements. 1. Forms to return today after class:

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

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

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Beijing Jiaotong University CS-23: C++ Programming Summer, 2019 Course Syllabus

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

THE TEN COMANDMENTS OF ALGEBRA

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Unit 3 Fill Series, Functions, Sorting

Unit 3 Functions Review, Fill Series, Sorting, Merge & Center

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

BLM2031 Structured Programming. Zeyneb KURT

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

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

LECTURE 1-2. Introduction and Number Systems

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

EECS168 Exam 3 Review

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

CSCE 206: Structured Programming in C

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

CS 221 Lecture. Tuesday, 11 October 2011

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division. SECOND SEMESTER Course Handout Part II

CprE 281: Digital Logic

ECE 20B, Winter Purpose of Course. Introduction to Electrical Engineering, II. Administration

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Microsoft Access 2016

Introduction to Computer Science Midterm 3 Fall, Points

Microsoft Access 2016

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

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

Declaring a 2D Array

CS1 Lecture 13 Feb. 13, 2019

Welcome to Computer Organization and Design Logic

CS 101 Fall 2006 Midterm 1 Name: ID:

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

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

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Transcription:

More on Arrays CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB

Announcements Homework #12 due today No homework assigned today!! Lab #7 is due on Monday, 11/14 at 8 AM Midterm #2 is this Thursday in class 11/9/2016 Matni, CS16, Fa16 2

Midterm 2 Material to Review: Lectures: Start at lecture #7, end at lecture #13 Book Sections: Chapters 5, 6, 7, 8.1, 8.2, 8.3 (maybe?) Homework: Start at HW6, end at HW13 Labs: Lab4, Lab5, Lab6 Covers: Functions I/O Streams (including command line inputs) Binary, Decimal, Octal, Hexadecimal Conversions Characters and Strings Arrays Vectors (if we get to it in time) 11/9/2016 Matni, CS16, Fa16 3

Thursday, 11/10 in this classroom Starts at 2:00pm **SHARP** I will chose where you sit! Duration: 1 hour long Closed book: no calculators, no phones, no computers Only 1 sheet (single-sided) of written notes Must be no bigger than 8.5 x 11 You have to turn it in with the exam You will write your answers on the exam sheet itself. 11/9/2016 Matni, CS16, Fa16 4

Lecture Outline Programming with Arrays Multidimensional Arrays Lab 7 Questions 11/9/2016 Matni, CS16, Fa16 5

Programming With Arrays The size needed for an array is changeable Often varies from one run of a program to another Size is often not known when the program is written A common solution to the size problem: Declare the array size to be the largest that could be needed Decide how to deal with partially filled arrays Example forthcoming

Partially Filled Arrays When using arrays that are partially filled Functions dealing with the array may not need to know the declared size of the array, only how many elements are stored in the array A parameter, number_used, may be sufficient to ensure that referenced index values are legal

Your textbook, Ch. 7 Display 7.9

Constants as Arguments When function fill_array (Display 7.9) is called, MAX_NUMBER_SCORES is used as an argument Can't MAX_NUMBER_SCORES be used directly without making it an argument? Using MAX_NUMBER_SCORES as an argument makes it clear that fill_array requires the array's declared size This makes fill_array easier to be used in other programs

11/9/2016 Matni, CS16, Fa16 12

Searching Arrays A sequential search is one way to search an array for a given value Look at each element from first to last to see if the target value is equal to any of the array elements The index of the target value can be returned to indicate where the value was found in the array A value of -1 can be returned if the value was not found

Sequential Search Task: Search the array for ff ARRAY a[]: a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12] 11/9/2016 Matni, CS16, Fa16 14

Example search Function (See Display 7.10 in the textbook) Uses a while loop to compare array elements to the target value Sets a variable of type bool to true if the target value is found, ending the loop Checks the bool variable when the loop ends to see if the target value was found Returns the index of the target value if found, otherwise returns -1

11/9/2016 Matni, CS16, Fa16 17

Program Example: Sorting an Array Sorting a list of values is very common task Create an alphabetical listing Create a list of values in ascending order Create a list of values in descending order Many sorting algorithms exist Some are very efficient Some are easier to understand

Program Example: The Selection Sort Algorithm When the sort is complete, the elements of the array are ordered such that a[0] < a[1] < < a [ number_used -1] This leads to an outline of an algorithm: for (int index = 0; index < number_used; index++) place the index th smallest element in a[index]

Program Example: Sort Algorithm Development (See Display 7.10 in the textbook) One array is sufficient to do our sorting Search for the smallest value in the array Place this value in a[0], and place the value that was in a[0] in the location where the smallest was found i.e. swap them Starting at a[1], find the smallest remaining value swap it with the value currently in a[1] Starting at a[2], continue the process until the array is sorted

Sort from smallest to largest

Multi-Dimensional Arrays C++ allows arrays with multiple index values char page [30] [100]; declares an array of characters named page page has two index values: The first ranges from 0 to 29 The second ranges from 0 to 99 Each index in enclosed in its own brackets Page can be visualized as an array of 30 rows and 100 columns

Index Values of page The indexed variables for array page are page[0][0], page[0][1],, page[0][99] page[1][0], page[1][1],, page[1][99] page[29][0], page[29][1],, page[29][99] page is actually an array of size 30 page's base type is an array of 100 characters

Multidimensional Array Parameters Recall that the size of an array is not needed when declaring a formal parameter: void display_line(char a[ ], int size); The base type of a multi-dimensional array must be completely specified in the parameter declaration void display_page(char page[ ] [100], int size_dimension_1);

Program Example: Grading Program Grade records for a class can be stored in a two-dimensional array For a class with 4 students and 3 quizzes the array could be declared as int grade[4][3]; The first array index refers to the number of a student The second array index refers to a quiz number Since student and quiz numbers start with one, we subtract one to obtain the correct index Each student (1 thru 4) has 3 grades (1 thru 3) Your textbook, Ch. 7, Display 7.14 has an example

11/9/2016 Matni, CS16, Fa16 28

11/9/2016 Matni, CS16, Fa16 29

Lab 7 Partner-up (optional) Both exercises deal with 2-D arrays 11/9/2016 Matni, CS16, Fa16 30