Arrays and Pointers: Review and Examples

Similar documents
Pointers and Output Parameters

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

Arrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays

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

Structures. CSE 142 Programming I. Chapter 11. Review: Data Structures. Defining structs. Terminology. Structs: Heterogeneous Structures

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

S-1. Concepts this lecture. CSE 142 Computer Programming I. Chapter 11. Review: Data Structures. Problem: Account Records.

Slides adopted from T. Ferguson Spring 2016

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Computer Programming Lecture 12 Pointers

Sorting and Searching. Sudeshna Sarkar 7 th Feb 2017

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

At the end of this module, the student should be able to:

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Programming Studio #9 ECE 190

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Arrays. Week 4. Assylbek Jumagaliyev

Lab 3. Pointers Programming Lab (Using C) XU Silei

Example Algorithms. CSE 2320 Algorithms and Data Structures Alexandra Stefan. University of Texas at Arlington. Last updated 9/7/2016

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers (part 1)

Lecture 04 Introduction to pointers

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

Goals of this Lecture

Chapter 7 C Pointers

Principles of Programming. Chapter 6: Arrays

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Overview (1A) Young Won Lim 9/25/17

Lecture 6 Sorting and Searching

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

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Structures. What is a structure? It is a collection of possibly different types Name the collection Name the components

First of all, it is a variable, just like other variables you studied

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Arrays and Pointers (part 1)

UNIT V Sub u P b ro r g o r g a r m a s

Display Input and Output (I/O)

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Overview (1A) Young Won Lim 9/14/17

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Arrays and Applications

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Subject: Fundamental of Computer Programming 2068

Computer Science & Engineering 150A Problem Solving Using Computers

CGS 3460 Summer 07 Midterm Exam

Introduction to Scientific Computing and Problem Solving

Overview (1A) Young Won Lim 9/9/17

Searching an Array: Linear and Binary Search. 21 July 2009 Programming and Data Structure 1

Searching Algorithms/Time Analysis

Lecture 04 FUNCTIONS AND ARRAYS

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Intermediate Programming, Spring 2017*

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

SYSC 2006 C Winter 2012

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming Language B

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CSE 3302 Programming Languages Lecture 5: Control

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

Functions. Arash Rafiey. September 26, 2017

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Pointers in C/C++ 1 Memory Addresses 2

BITG 1113: POINTER LECTURE 12

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

A Shotgun Introduction to C

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

INTRODUCTION OF LOOPS

Iteration. CSE / ENGR 142 Programming I. while loops. Chapter 5. Motivating Loops. Loop to Add 5 Numbers 1996 UW CSE H - 1

Pointers and Strings Prentice Hall, Inc. All rights reserved.

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pointers. Introduction

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

More about Binary 9/6/2016

CPSC 427a: Object-Oriented Programming

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

LECTURE 17. Array Searching and Sorting

CSE 307: Principles of Programming Languages

CSE Lecture In Class Example Handout

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Introduction to Arrays

Chapter 16 Pointers and Arrays

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

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

Language comparison. C has pointers. Java has references. C++ has pointers and references

Transcription:

CSE 142 Programming I Arrays and Pointers: Review and Examples 2000 UW CSE Array Type Quiz AFunction (int a1[ ], int *sp) int a2[maxa]; int N; a1 = a2; /*1. */ a1[maxa] = a2[maxa]; /*2. */ N = a1[0]; /*3. */ a1[01] = sp; /*4. */ *sp = a1[0]; /*5. */ printf ( %d, a1); /*6. */ printf ( %d, a2[0]); /*7. */ a1[a2[*sp]] = 1; /*8. */ scanf ( %d%d%d%d%d, a1[1], &a1[1], *a1[1], sp, *sp); /*9. */ K3-1 K3-2 Pointer Type Quiz Shifting Array Elements QFunct (int i, int * ip, double x, double * xp) x = i; /* 1. */ i = x; /* 2. */ /* Shift x[0], x[1],, x[n-1] one position upwards to make space for a new element at x[0]. Insert the value new at x[0]. Update the value of n. */ ip = 30; /* 3. */ ip = i; /* 4. */ ip = &i; /* 5. */ ip = &x; /* 6. */ xp = ip; /* 7. */ &i = ip; /* 8. */ K3-3 for ( k = n ; k >= 1 ; k = k - 1 ) x[k] = x[k-1] ; x[0] = new ; n = n+1 ; K3-4 Shifting Array Elements Searching 1 2 3 4 7 5? 4 7 5 5 4 7 7 5 4 5 n = 3; new = 6; 4 4 7 5 6 4 7 5 Searching = looking for something Searching an array is particularly common Goal: determine if a particular value is in the array If the array is unsorted: start at the beginning and look at each element to see if it matches K3-5 K3-6

Linear Search /* If x appears in a[0..n-1], return its index, i.e., return k so that a[k]==x. If x not found, return -1 */ int search (int a[ ], int n, int x) int index = 0; while (index < n && a[index]!= x) index++; if (index < n) return index; else return -1; K3-7 v Linear Search 3 12-5 6 142 21-17 45 Test: search(v, 8, 12) search(v, 8, 15) Note: Condition in while relies on short-circuit evaluation of && (i.e., a[index] might not be defined if index>=n). K3-8 Can we do better? "Binary search" works if the array is already sorted 1. Look for the target in the middle. 2. If you don t find it, you can ignore half of the array, and repeat the process with the other half. Example: Find first page of Pizza listings in the yellow pages K3-9 Is it worth the trouble? Suppose you had 1000 elements Ordinary search would require maybe 500 comparisons on average Binary search after 1st compare, throw away half, leaving 500 elements to be searched. after 2nd compare, throw away half, leaving 250. Then 125, 63, 32, 16, 8, 4, 2, 1 are left. After at most 10 steps, you re done! What if you had 1,000,000 elements?? K3-10 Whole Arrays as Parameters #define ARRAY_SIZE 200 double average ( int a[array_size] ) int i, total = 0 ; for ( i = 0 ; i < ARRAY_SIZE ; i = i + 1 ) total = total + a[i] ; return ((double) total / (double) ARRAY_SIZE) ; int x[array_size] ; x_avg = average ( x ) ; Arrays as Output Parameters note: no * no & /* Sets vsum to sum of vectors a and b. */ void VectorSum( int a[3], int b[3], int vsum[3]) int i ; for ( i = 0 ; i < 3 ; i = i + 1 ) vsum[i] = a[i] + b[i] ; int (void) int x[3] = 1,2,3, y[3] = 4,5,6, z[3] ; VectorSum( x, y, z ); printf( %d %d %d, z[0], z[1], z[2] ) ; K3-11 K3-12

General Vector Sum Array Parameter Summary void VectorSum( int a[ ], int b[ ], int vsum[ ], int length) int i ; for ( i = 0 ; i < length ; i = i + 1 ) vsum[i] = a[i] + b[i] ; int x[3] = 1,2,3, y[3] = 4,5,6, z[3] ; VectorSum( x, y, z, 3 ); Array elements: Just like simple variables of that type, both input & output parameters Whole arrays: Arrays are not passed by value, i.e. not copied Formal parameter: type array_name [SIZE] Or : type array_name [ ] no * Actual parameter: array_name no [ ], no & K3-13 K3-14 An Array as a Pointer Review: Pointer int A[100]; A memory A pointer contains a reference to another variable; that is, the pointer contains the address of a variable. A[0] equivalent to *A A[i] equivalent to *(A + i) pointer addition xp is a pointer to int 1024: 32 X 1024: xp K3-15 K3-16 Addresses and Pointers Vocabulary Three new types: int * pointer to int double * pointer to double char * pointer to char Two new (unary) operators: & address of & can be applied to any variable (or param) * location pointed to by * can be applied only to a pointer Dereferencing or indirection: following a pointer to a memory location Output parameter: a pointer parameter of a function can be used to provide a value ("input") as usual, and/or store a changed value ("output") Don t confuse with printed output (printf) K3-17 K3-18

Sorting Sort 3 Integers as a Program Problem: Sort 3 integers Three-step algorithm: 1. Read in three integers: x, y, z 2. Put smallest in x: Swap x, y if necessary; then swap x, z, if necessary. 3. Put second smallest in y: Swap y, z, if necessary. K3-19 int (void) int x, y, z, scanstatus ; scanstatus = scanf( %d%d%d, &x, &y, &z) ; if scanstatus == 3 if ( x > y ) swap(&x, &y) ; if ( x > z ) swap(&x, &z) ; if ( y > z ) swap(&y, &z) ; K3-20 x y z x y z swap p1 p2 temp K3-21 K3-22 sort3 as a Function /* interchange values as needed to establish */ /* *xp <= *yp <= *zp */ void sort3 (int *xp, int *yp, int *zp) if ( *xp > *yp ) swap(xp, yp) ; if ( *xp > *zp ) swap(xp, zp) ; NO &s! if ( *yp > *zp ) swap(yp, zp) ; swap p1 p2 temp K3-23 int (void) int x, y, z ; /*scanf the values, then: */ sort3(&x, &y, &z) ; K3-24

Why no & in swap call? Real reason xp and yp are already pointers that point to the variables that we want to swap Alternative explanation using alias idea *xp and *yp are aliases for the variables we want to swap x y z sort3 xp yp zp We want to allow swap to use aliases for *xp and *yp so we should use &(*xp) and &(*yp) in the call swap p1 p2 temp BUT xp==&(*xp) and yp==&(*yp)!!!! K3-25 K3-26 Midpoint Of A Line /* Given 2 endpoints of a line, return coordinates of midpoint */ void set_midpoint( double x1, double y1, /* 1st endpoint */ double x2, double y2, /* 2nd endpoint */ double * midx_p, double * midy_p ) /* Pointers to midpoint */ *midx_p = (x1 + x2) / 2.0; *midy_p = (y1 + y2) / 2.0; (x 1, y 1 ) (x 2, y 2 ) (x 1 +x 2 ) (y 1 +y 2 (, ) 2 2 double x_end, y_end, mx, my; set_midpoint(0.0, 0.0, x_end, y_end, &mx, &my); x_end y_end mx my set_midpoint x1 y1 x2 y2 midx_p midy_p K3-27 K3-28 Example: Coordinates Coordinate Conversion Board Coordinates row, column Screen Coordinates x, y used by graphics package 4 3 2 row 1 y LL_Y 0 (LL_X, LL_Y) (x,y) SQUARE_SIZE x LL_X 0 1 2 3 4 col K3-29 K3-30

Coordinate Conversion Pointers vs. Values #define LL_X 40 #define LL_Y 20 #define SQUARE_SIZE 10 void screen_to_board ( int screenx, int screeny, /* coordinates on screen */ int * row_p, int * col_p) /* position on board */ *row_p = (screeny - LL_Y) / SQUARE_SIZE; *col_p = (screenx - LL_X) / SQUARE_SIZE; in caller in callee Declaration: int x int * x_ptr To get the address of x: &x x_ptr To get the value of x: x *x_ptr x screen_to_board (x, y, &row, &col); x_ptr K3-31 K3-32 Array Type Quiz: Answers AFunction (int a1[ ], int *sp) int a2[maxa]; int N; a1 = a2; /*1. no way*/ a1[maxa] = a2[maxa]; /*2. logic error*/ N = a1[0]; /*3. OK*/ a1[01] = sp; /*4. nope*/ *sp = a1[0]; /*5. OK*/ printf ( %d, a1); /*6. nope*/ printf ( %d, a2[0]); /*7. OK */ a1[a2[*sp]] = 1 /*8. OK*/ scanf ( %d%d%d%d%d, a1[1], &a1[1], *a1[1], sp, *sp);/*9. no, yes, meaningless, yes, no*/ K3-33 Pointer Type Quiz: Answers QFunct (int i, int * ip, double x, double * xp) x = i; /* 1. no problem */ i = x; /* 2. not recommended */ ip = 30; /* 3. No way */ ip = i; /* 4. Nope */ ip = &i; /* 5. just fine */ ip = &x; /* 6. forget it! */ xp = ip; /* 7. bad */ &i = ip; /* 8. meaningless */ K3-34