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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Dynamic Arrays Makefiles and Mul3ple File Compiles

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

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

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

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

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

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

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

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

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

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

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

BITG 1113: Function (Part 2) LECTURE 5

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

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

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

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

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

Computer Programming

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

File I/O CS 16: Solving Problems with Computers I Lecture #9

Computer Organization & Systems Exam I Example Questions

Getting started with C++ (Part 2)

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

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

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

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

Introduction to Computer Science Midterm 3 Fall, Points

Array Elements as Function Parameters

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

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

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

BLM2031 Structured Programming. Zeyneb KURT

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Unit 10: Data Structures CS 101, Fall 2018

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

CS 241 Data Organization using C

Introduction to Scientific Computing and Problem Solving

CIS 130 Exam #2 Review Suggestions

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

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

Pointers in C/C++ 1 Memory Addresses 2

Memory, Arrays, and Parameters

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Computer Programming. Dr. Fahad Computer Science Deptt.

Welcome to Solving Problems with Computers I

EECS168 Exam 3 Review

CS 241 Data Organization. August 21, 2018

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

You must pass the final exam to pass the course.

CS31 Discussion 1E Spring 17 : week 08

CS242 COMPUTER PROGRAMMING

a data type is Types

C++ PROGRAMMING SKILLS Part 4: Arrays

Programming Studio #9 ECE 190

CS 3330 Introduction. Daniel and Charles. CS 3330 Computer Architecture 1

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

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

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

MICROPROCESSORS A (17.383) Fall Lecture Outline

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

MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

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

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Central Washington University Department of Computer Science Course Syllabus

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

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

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

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

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

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

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

Announcements. 1. Forms to return today after class:

Transcription:

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB

Thursday, 5/17 in this classroom Starts at 2:00 PM **SHARP** Please start arriving 5-10 minutes before class I may ask you to change seats Please bring your UCSB IDs with you Closed book: no calculators, no phones, no computers Only allowed ONE 8.5 x11 sheet of notes one sided only You have to turn it in with your exam You will write your answers on the exam sheet itself. 5/10/18 Matni, CS16, Sp18 2

What s on the Midterm#2? EVERYTHING From Lectures 7 12, including Makefiles Debug Techniques Numerical Conversions Strings: C++ vs C-strings Strings and Characters: Member Functions & Manipulators File I/O Arrays Pointers 5/10/18 Matni, CS16, Sp18 3

Programming with Arrays Sequential Search of Arrays Multi-Dimensional Arrays Lecture Outline Introduction to C++ Memory Map Introduction to Pointers 5/10/18 Matni, CS16, Sp18 4

Summary Difference void thisfunction(int arr[ ], int size); Array arr gets passed and whatever changes are done inside the function will result in changes to arr where it s called. void thisfunction(const int arr[ ], int size); Array arr gets passed BUT whatever changes are done inside the function will NOT result in changes to arr where it s called. int* thisfunction(int arr[ ], int size); Array arr gets passed and whatever changes are done inside the function will result in changes to arr where it s called. ADDITIONALLY, a new pointer to an array thisfunction is passed back (DON T WORRY ABOUT THIS UNTIL AFTER WE LEARN ABOUT POINTERS!) 5/10/18 Matni, CS16, Sp18 5

Programming With Arrays See demo file: fillinguparray.cpp The size requirement for an array might need to be un-fixed Size is often not known when the program is written A common solution to the size problem (while still using regular arrays): Declare the array size to be the largest that could be needed Decide how to deal with partially filled arrays 5/10/18 Matni, CS16, Sp18 6

Partially Filled Arrays See demo file: fillinguparray.cpp 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 maximum number of elements need to be stored in the array! A parameter - let s call it number_used - may be sufficient to ensure that referenced index values are legal 5/10/18 Matni, CS16, Sp18 7

Searching Arrays A sequential search is one way to search an array for a given value. The algorithm is as follows: 1. Look at each element from first to last to see if the target value is equal to any of the array elements 2. The index of the target value is returned to indicate where the value was found in the array 3. A value of -1 is returned if the value was not found anywhere Pros? Cons? 5/10/18 Matni, CS16, Sp18 8

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] Result: in position 9 5/10/18 Matni, CS16, Sp18 9

int SeqSearch (int arr[], int array_size, int target) { int index(0); bool found(false); while ((!found) && (index < array_size)) { if (arr[index] == target) found = true; else index++; } if (found) return index; else return -1; } Simple Sequential Search Function Example 1. Look for a target value inside of a given array 2. If you find it, return its location (i.e. index) in the array 3. If you don t find it, return -1 5/10/18 Matni, CS16, Sp18 10

Multi-Dimensional Arrays See demo file: multidimensionaldemo.cpp C++ allows arrays with multiple index dimensions (have to be same type, tho ) EXAMPLE: char page[30][100]; declares an array of characters named page page has two index values: The 1 st ranges from 0 to 29 The 2 nd 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 page is actually an array of size 30 page's base type is an array of 100 characters [0][0] [0][1] [0][98] [0][99] [1][0] [1][1] [1][98] [1][99] [28][0] [28][1] [28][98] [28][99] [29][0] [29][1] [29][98] [29][99] 5/10/18 Matni, CS16, Sp18 12

Program Example: Grading Program Grade records for a class can be stored in a two-dimensional array 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 Each student (0 thru 3) has 3 grades (0 thru 2) Your textbook, Ch. 7, Display 7.14 has an example 5/10/18 Matni, CS16, Sp18 13

5/10/18 Matni, CS16, Sp18 14

Use Nested for-loops to Go Through a MDA Example: const int MAX1 = 10, MAX2 = 20; int arr[max1][max2]; for (int i = 0; i < MAX1; i++) for (int j = 0; j < MAX2; j++) cout << arr[i][j]; 5/10/18 Matni, CS16, Sp18 15

Initializing MDAs See demo file: multidimensionaldemo.cpp Recall that you can do this for uni-dimensional arrays and get all elements initialized to zero: double numbers[100] = {0}; For multidimensional arrays, it s similar syntax: double numbers[5][100] = { {0}, {0} }; What would this do? OR: double numbers[5][100] = {0}; double numbers[2][3] = { {6,7}, {8,9} }; 5/10/18 Matni, CS16, Sp18 16

Multidimensional Array Parameters in Functions Recall that the size of an array is not needed when declaring a formal parameter: void display_line(char a[ ], int size); Look! No size! Size is here instead! BUT the base type must be completely specified in the parameter declaration of a multi-dimensional array void display_page(char page[ ][100], int size_dimension1); Base has a size defined! 5/10/18 Matni, CS16, Sp18 17

INTRO TO POINTERS Section 9.1 in book 5/10/18 Matni, CS16, Sp18 18

Passing by Values What does this code print out? void swapvalue(int x, int y) { int tmp = x; x = y; y = tmp; } int main() { int a(30), b(40); cout << a << " " << b << endl; swapvalue(a, b); cout << a << " " << b << endl; } Is It: A. 30 40 30 40 B. 30 40 40 30 C. Something else 5/10/18 Matni, CS16, Sp18 19

Pointers A pointer is the memory address of a variable When a variable is used as a call-by-reference argument, it s the actual address in memory that is passed 5/10/18 Matni, CS16, Sp18 20

Memory Addresses Consider int variable num that holds the int value 42 num is assigned a place in memory (what does that??) In this example the address of that place in memory is 0x001F 1 byte num Generally, memory addresses use hexadecimals (and usually 8 of them, not just 4 but this is ONLY an example ) The 0x at the start is just to indicate the number is a hexadecimal Address Data 0x001D 0 0x001E -25 0x001F 42 0x0020 1332 0x0021-4009 0x0022 7 5/10/18 Matni, CS16, Sp18 21

Memory Addresses The address of a variable can be obtained by putting the ampersand character (&) before the variable name. Address Data 0x001D 0 0x001E -25 0x001F 42 0x0020 1332 0x0021-4009 0x0022 7 & is called the address-of operator num_add 0x98A0 31 Example: int num_add = &num; will result in num_add to hold the value 0x001F (or 31 in decimal) 1 byte num 5/10/18 Matni, CS16, Sp18 22

q Turn in Lab 6 on Monday q Do HW11 by next Tuesday YOUR TO-DOs q Study for your Midterm #2 on Thursday! q Visit Prof s and TAs office hours if you need help! q Enjoy the beautiful outdoors 5/10/18 Matni, CS16, Sp18 23

5/10/18 Matni, CS16, Sp18 24