ECE250: Algorithms and Data Structures C++ Language Tutorial

Similar documents
pointers + memory double x; string a; int x; main overhead int y; main overhead

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

ECE250: Algorithms and Data Structures Midterm Review

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

ECE250: Algorithms and Data Structures AVL Trees (Part A)

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

Lab 8 The Stack (LIFO) Structure

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Dynamic Data Structures

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Pointers, Arrays and C-Strings

Computer Programming

Homework Assignment #2 (revised)

Implementing an ADT with a Class

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part B

Getting Started with Visual Studio

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

Introduction to C++ Introduction to C++ 1

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Lab Instructor : Jean Lai

Exercise 1.1 Hello world

Chapter 17 - Notes Recursion

ECE250: Algorithms and Data Structures Final Review Course

Linked List using a Sentinel

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

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

Security Coding Module - Buffer Overflow Data Gone Wild CS1

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Pointers II. Class 31

Exam 3 Chapters 7 & 9

Lab # 02. Basic Elements of C++ _ Part1

Week 3: Pointers (Part 2)

Introduction to the C programming language

Linked lists Tutorial 5b

Introduction to the C programming language

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CSC1322 Object-Oriented Programming Concepts

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS103L SPRING 2018 UNIT 7: FILE I/O

1 Unit 8 'for' Loops

ECE250: Algorithms and Data Structures B-Trees (Part A)

CMSC 202 Midterm Exam 1 Fall 2015

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

a data type is Types

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

LAB 4.1 Relational Operators and the if Statement

ECE250: Algorithms and Data Structures Elementary Graph Algorithms Part A

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

C++ Lab 04 - File I/O, Arrays and Vectors

Programming Language. Functions. Eng. Anis Nazer First Semester

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Lab 1: First Steps in C++ - Eclipse

Chapter 1: Object-Oriented Programming Using C++

COMP322 - Introduction to C++ Lecture 01 - Introduction

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

2 is type double 3 auto i = 1 + 2; // evaluates to an 4 integer, so it is int. 1 auto d = 5.0; // 5.0 is a double literal, so it

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Computer Programming. Dr. Fahad Computer Science Deptt.

Lecture on pointers, references, and arrays and vectors

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Partha Sarathi Mandal

Lab 2: ADT Design & Implementation

Computer Programming

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

C++ Structures Programming Workshop 2 (CSCI 1061U)

Midterm Practice TA: Brian Choi Section Webpage:

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

University of Toronto

CSC 1052 Algorithms & Data Structures II: Linked Queues

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

377 Student Guide to C++

Component-Based Software Engineering

Memory and Pointers written by Cathy Saxton

CS2141 Software Development using C/C++ C++ Basics

Chapter 2 Basic Elements of C++

assembler Machine Code Object Files linker Executable File

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

C++ Scope Resolution Operator ::

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

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

内存管理. Memory management

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Computational Physics

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

What is CBSE and Why? Component-Based Software Engineering. But why not in Software engineering? Component Everywhere

Component-Based Software Engineering

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Transcription:

ECE250: Algorithms and Data Structures C++ Language Tutorial Ladan Tahvildari, PEng Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University of Waterloo Tiuley Alguindigue Lab Instructor. M. Eng. University of Waterloo.

Tutorial Overview Part 1 v Vector Class Exercise in Quiz v Compiling and Executing in the ECEUNIX server Ø Accessing ECE UNIX server Ø UNIX some basic commands ECE250 2

Tutorial Part 2 v Pointers v Pointers and Arrays v Dynamic Allocation v ECE250 Projects Structure and Testing ECE250 3

Question 1a - on Pointers #include <iostream> using namespace std; int main() { int a_number = 10 ; // declaring a pointer int *address_of_a_number; 0x6ffe44 int *address_of_another_number; // Changing the value of number // & - address off or reference operator address_of_a_number = &a_number ; // * dereference operator *address_of_a_number = 20 ; 10 } cout << a_number << endl; cout << address_of_a_number << endl ECE250 4

Answer to Q1a - on Pointers 20 0x6ffe44 ECE250 5

Question 1b - Pointer Arithmethics #include <iostream> using namespace std; int main() { int a_number = 10 ; int *address_of_a_number; int *address_of_another_number; 0x6ffe44 10 address_of_a_number = &a_number ; // changing the address - adding 1 address_of_a_number = address_of_a_number + 1; cout << address_of_a_number << endl; } ECE250 6

Answer to Q1b - Pointer Arithmethics 0x6ffe48 0x6ffe44 + 4, 4 is the length of an integer ECE250 7

More on C++ Pointers http://www.cplusplus.com/doc/tutorial/pointers/ ECE250 8

Question 2a - Arrays and Pointers int numbers[5]= {0,10,20,30,40}; int *ptr_numbers = numbers ; for( int i = 0; i < 5; i++){ cout << *(ptr_numbers + i) << endl; } for( int i = 0; i < 5; i++){ cout << numbers[i] << endl; } What is the output of this program? ECE250 9

Answer Q2a - Arrays and Pointers What is the output of this program? 0 10 20 30 40 0 10 20 30 40 ECE250 10

Question 2b - Arrays and Pointers int numbers[5]= {0,10,20,30,40}; int *ptr_numbers = numbers ; // Assigning values to the elements of the array two ways numbers[2] = 2000; *(ptr_numbers + 4)= 4000; for( int i = 0; i < 5; i++){ cout << numbers[i] << endl; } What is the output of this program? ECE250 11

Answer Q2b - Arrays and Pointers What is the output of this program? 0 10 2000 30 4000 ECE250 12

Static vs Dynamic Memory // statically allocated (compilation time) int sa_number = 200; // dynamically allocated (run time) int *ptr_number; ptr_number = new int(200);.. do something with *ptr_number.. delete ptr_number; ECE250 13

Arrays - Static vs Dynamic Memory // statically allocated (compilation time) int sa_number = 200; // dynamically allocated (run time) int *ptr_numbers = new int[5]; delete [] ptr_number; ECE250 14

Q3 - Arrays and Dynamic Memory Allocation // dynamically allocated (run time) int *ptr_numbers = new int[5]; // Write the code to assign values to // elements in the array delete [] ptr_number; ECE250 15

Answer to Q3 - Arrays and Dynamic Memory Allocation ptr_numbers[0] = 50; ptr_numbers[1] = 10; ptr_numbers[2] = 20; ptr_numbers[3] = 30; ptr_numbers[4] = 40; ECE250 16

ECE250 Projects v Project 1 is the implementation of some variation of a stack or queue data structure. v You will be writing the implementation of the class, ie. For a Dequeue (Double ended Queue) you will write the class DequeAsArray.h ECE250 17

ECE250 Projects v For the Deque example, we will provide you with: Ø a driver (main() invoking test files) Ø tester files (Tester.h and DequeAsArrayTester.h), Ø exceptions (Exception.h) Ø definitions and some utilities (ece250.h) ECE250 18

ECE250 Projects v You will be able to enter commands at the prompt that will allow you to test each method in your program. You can save a series of commands in a text file and repeat your testing as needed (or share with others! this is allowed). ECE250 19

ECE250 Projects Example of test file: new enqueue_tail 10 enqueue_tail 20 enqueue_tail 30 size 3 head 10 tail 30 dequeue_head 10 size 2 empty 0 head 20 tail 30 delete summary ECE250 20

ECE250 Projects If everything is working, you will see OK for each line, otherwise you will see an error: Starting Test Run 1 % new Okay 2 % enqueue_tail 10 Okay 3 % enqueue_tail 20 Okay 4 % enqueue_tail 30 Okay 5 % size 3 Okay 6 % head 10 Failure in head(): expecting the value '10' but got '0' 7 % tail 30 Failure in tail(): expecting the value '30' but got '2' 8 % dequeue_head 10 Okay ECE250 21

ECE250 Projects v Your IDE Project must contain Ø Your solution file ie. DequeueAsArray.h Ø Driver file Ø Tester files (2) Ø Exeception.h Ø ece250.h v Use this list as a reference when defining all your projects in your IDE. ECE250 22

Tutorial 2 Summary v Pointers v Pointers and Arrays v Dynamic Allocation v ECE250 Projects Structure and Testing ECE250 23