School of Electrical Engineering & Computer Science Oregon State University. CS Recitation 3 Spring 2012

Similar documents
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

EL2310 Scientific Programming

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Fundamental of Programming (C)

CSC C69: OPERATING SYSTEMS

Memory Allocation. General Questions

Beginning C Programming for Engineers

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Lecture 3. More About C

Linked List. April 2, 2007 Programming and Data Structure 1


C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Essar Placement Paper

EL2310 Scientific Programming

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CS24 Week 2 Lecture 1

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Pointers (continued), arrays and strings

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CSE 303 Lecture 8. Intro to C programming

Stream Model of I/O. Basic I/O in C

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

PRINCIPLES OF OPERATING SYSTEMS

CSC 1052 Algorithms & Data Structures II: Linked Queues

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Introduction to C. Systems Programming Concepts

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

Fall 2018 Discussion 2: September 3, 2018

Work relative to other classes

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : End-Semester Examination

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

Pointers (continued), arrays and strings

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Queue: Queue Representation: Basic Operations:

COMP 2355 Introduction to Systems Programming

ITI Introduction to Computing II

UNIT-2 Stack & Queue

A Short Course for REU Students Summer Instructor: Ben Ransford

Should you know scanf and printf?

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Pointers and Arrays 1

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Programming refresher and intro to C programming

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Arithmetic and Bitwise Operations on Binary Data

Cpt S 122 Data Structures. Data Structures

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

DS L9: Queues

C Introduction. Comparison w/ Java, Memory Model, and Pointers

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

OBJECT ORIENTED PROGRAMMING

Solution for Data Structure

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

COMP 2001/2401 Test #1 [out of 80 marks]

Introduction to Programming

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

CS 222: Linked Lists, Queues, Stacks

Bit Manipulation in C

Programming. Lists, Stacks, Queues

CSEN 301 Data Structures and Algorithms

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

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Ashish Gupta, Data JUET, Guna

just a ((somewhat) safer) dialect.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

CS 2113 Software Engineering

The Compilation Process

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Section Notes - Week 1 (9/17)

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

More about BOOLEAN issues

Computer Organization & Systems Exam I Example Questions

Introduction to C. Sami Ilvonen Petri Nikunen. Oct 6 8, CSC IT Center for Science Ltd, Espoo. int **b1, **b2;

Console Input and Output

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Hacking in C. The C programming language. Radboud University, Nijmegen, The Netherlands. Spring 2018

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

just a ((somewhat) safer) dialect.

Transcription:

School of Electrical Engineering & Computer Science Oregon State University CS 261 - Recitation 3 Spring 2012

Bitwise Operators AND (&), OR ( ), XOR(^), Complement (~) In all our examples sizeof(int) = 4 bytes. Examples Extract the least significant byte of an integer a & 0x00000ff a & 0xff In general the above operation is called masking. One can mask any bit of an integer with appropriate mask.

Bitwise Operators Examples Set nth lowest bit of an integer a (0x1 << n) Compare the second bytes of two integers!((a & 0xff00) ^ (b & 0xff00)) Toggle nth lowest bit of an integer a ^ (0x1 << n)

Formatted Output & printf printf needs format type & value to print. Format types are available for all builtin data types: int, long, float, double, char, pointers. printf("%d", 20); printf("%x", 20); printf("%s", "Hello CS 261"); printf("%f", 22.0/7); printf("%e", 22.0/7); printf("%lf", area);

Formatted Output & printf What happens if your format type and value type don't match. printf("%f", 20); GCC compiler gives a warning. So look out for warnings. If you ignore the warning, your output may contain junk.

Formatted Output & printf Get complete information about printf using man command. man 3 printf Width, and text justification printf("%-20d", sum);

Formatted Input & scanf scanf needs format type & address of a variable. Examples: int count; char name[100]; int *ip = &count scanf("%d", &count); scanf("%s", name); scanf("%d", ip);

Formatted Input & scanf Get more information on scanf using man command. man 3 scanf

Double Ended Queue (Dequeue) Data for N elements Operations: append_last append_first remove_last remove_first is_empty is_full get_size

Dequeue Using Arrays Why should we look at Dequeue A few interesting observations in the design of data structures. Its a typical example subtle compromises one have to make with implementation ADTs. struct dequeue { int *data; int max; int first, last; }; Pointer for 1-D array to hold dequeue elements. 1-D array size. First and last indices. Also called head & rear in some textbooks.

create_dequeue Create a dequeue structure in a consistent state. struct dequeue* create_dequeue(int queue_size) { struct dequeue *q = (struct dequeue*) malloc(sizeof(struct dequeue)); q->data = (int*) malloc(sizeof(int) * (queue_size + 1)); q->max = queue_size; q->first = 0; q->last = 0; } return q;

create_dequeue Queue Size = 5 Data Array Size = 6 q->first = q->last = 0 Empty in the created state.

is_empty Depends on implementation. We start with first = last in our implementation. int is_empty(struct dequeue *q) { return q->first == q->last; }

append_last Empty first last 100 append_last(q, 100) first last 100 200 append_last(q, 200) first last

append_last int append_last(struct dequeue *q, int elt) { /* Check for full queue. */ if (is_full(q)) { fprintf(stderr, "Queue is full. Cannot add more elements. Queue Size = %d", get_size(q)); return 0; } q->data[q->last] = elt; q->last = (q->last + 1) % q->max; } return 1;

append_first 100 200 Queue state from last slide. first last 100 200 300 append_first(q,300) last first 100 200 400 300 append_first(q,400) last first

append_first int append_first(struct dequeue *q, int elt) { /* Check for full queue */ if (is_full(q)) { fprintf(stderr, "Queue is full. Cannot add more elements. Queue size = %d", get_size(q)); return 0; } q->first = q->first > 0? q->first - 1 : q->max - 1; q->data[q->first] = elt; } return 1;

is_full 100 200 400 300 Queue state from last slide. last first 100 200 500 400 300 append_last(q, 500) last first No more elements can be appended. We designed q->last == q->first as condition for is_empty. To distinguish between full and empty states, we compromise one array location.

is_full int is_full(struct dequeue *q) { return (q->last + 1) % q->max == q->first; }

get_size 100 200 300 400 300 first last size = last first = 3 100 200 500 400 300 last first size = last - first = -1

get_size 100 200 500 400 300 100 200 500 400 300 last first last first int get_size(struct dequeue *q) { max first + last = 5 int sz = q->last - q->first; return (sz >= 0? sz : q->max + sz); }

remove_first int remove_first(struct dequeue *q) { int r; if (is_empty(q)) { printf("queue is empty. Cannot remove elements."); return 0; } } r = q->data[q->first]; q->first = (q->first + 1) % q->max; return r;

remove_last int remove_last(struct dequeue *q) { if (is_empty(q)) { fprintf(stderr, "Queue is empty. Cannot remove elements."); return 0; } } q->last = q->last > 0? q->last - 1 : q->max - 1; return q->data[q->last];

Linked Lists

Single Linked Lists head null struct node { /*data can be anything.*/ struct record *elt; /*pointer to next node.*/ struct node *next; }; struct node *list;

Adding a node at the end null new new null

Adding a node in the middle p q null head new p new q null

Double Linked Lists null null head Circularly Double Linked List

See you next week!