COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Introduction to Systems Programming - Recitation Omer Kotlicki Two instances: 1.Tuesdays 15:00-16:00; Kitot Wednesdays.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
Practice 1 Seoul National University Graphics & Media Lab.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
C++ Programming Lecture 10 Functions – Part II
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 15 - C++ As A "Better C"
LESSON 06.
ECE Application Programming
GC211 Data Structure Lecture 1 Sara Alhajjam.
Student Book An Introduction
Arrays in C.
void Pointers Lesson xx
Pointers, Dynamic Data, and Reference Types
Review for Final Exam.
Seoul National University
Review for Final Exam.
C++ Pointers and Strings
C++ Programming Lecture 17 Pointers – Part I
C++ Programming Lecture 18 Pointers – Part II
C++ Programming Lecture 20 Strings
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Seoul National University
Presentation transcript:

COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney

About Me Room: Madsen G61 Consultation: 20 mins before each lecture to arrange an appointment during consultation times

Textbook C++ Primer by Lippman 3rd Edition The C++ Programming Language by Stroustrup 3rd Edition Thinking in C++ by Eckel 2nd Edition

Course Information 'Practice of Programming' so expect a some programming Development Tools Library Usage

Lecture Schedule The Basics of C++ Basics of development under Unix The less basic Basics of C++

Assessment Assignment Zero0% Assignment One10% Assignment Two10% Assignment Three20% Final Exam60%

Assignment Policy Machine and Hand marked Individual work, no groups or copying Follow output instructions exactly Must work on department software No late assignments accepted without a medical certificate

The C++ Language C++ is what we will be using C will not be covered (except the parts which are part of C++)

C++ at Basser Using the GNU C++ compiler Usage: –g++ -Wall -g -o hello hello.cc hello.cc is the C++ file The executable will be hello -g adds debugging information -Wall turns on all warnings

A Simple C++ Program C++ starts in a special function called main #include int main() { std::cout << "They killed Kenny\n"; } The above is a complete C++ program

Variables C++ is strongly typed Every variable has a type and must be declared #include int main() { int result = 5 + 4; std::cout<<result<<std::endl; }

Some Built-in Types

Constants Constants are declared with the keyword const const int life = 42; const double pi = ; Constants are just like variables (except not variable)

Operators C++ has a lot of operators There are math operators like +, -, *, /, % Comparison operators like ==, >=,, != Logical operators like ||, &&, ! Bitwise operators like &, |, ~

More Operators a++ and ++a mean a =a+1 (sort of, see your text book for the real story) a-- and --a are similar a+=10 means a=a+10 There are a?=b versions for all the math operators

Enumerated Types An enumeration is a type that can hold a set of values They act lie integer types They are very useful for anything with a restricted domain enum day_of_week { SUN, MON, TUE, WED, THU, FRI, SAT }; day_of_week day; day = WED; day = day_of_week(5); //set to FRI

Writing Functions A function looks like this: double half(int num) { double result; result = num/2.0; return result; } half returns a double It takes a single int argument

Writing Functions II If a function doesn’t return anything it is declared as returning void void display_square(int num) { std::cout << num*num; }

If Statements C++ uses an if statement for selection int main() { char c; std::cin >> c; if (c == 'y') { std::cout << "You typed a y"; }

If Statements II There is also an else if (c == 'y') std::cout << "You typed a y"; else std::cout "You didn't type a y"; Can join an else and an if: if (c == 'y') std::cout <<"You typed a y"; else if (c == 'n') std::cout <<"You typed an n"; else std::cout <<"Only y/n are accepted";

Switch Statements A replacement for multiple ifs: char c; … switch (c) { case 'y' : std::cout << ”A y"; break; case 'n' : std::cout << ”A n"; break; default : std::cout << "y/n only"; } Works with any integral type

Switch Statements II Cases fall thorugh by default char c; … switch (c) { case 'y' : std::cout << ”A y"; case 'n' : std::cout << ”A n"; default : std::cout << "y/n only"; } The above wrong

For Statements for(int i=0;i<10;i++) std::cout << "i is " << i << std::endl; Initialisation : int i=0 Test : i<10 Increment : i++ Body : std::cout <<...

While Statements int i=0; while(i<10) { std::cout << "i is " << i << std::endl; i++; } Test : i<10 Body : between {}s

Do Statements int i=0; do { std::cout << "i is " << i << std::endl; i++; } while (i<10); Similar to while Always executes one Not often used

Arrays Combine well with for int scores[5] = { 1,2,3,4,5}; for (int i=0;i<5;i++) std::cout "Score ” << i << " : ” <<scores[i]; First element has index zero Arrays do not keep track of their length Do not automatically grow

Pointers A hard concept int i = 27; int* p = &i; *p=26; std::cout << i << endl; std::cout << p << endl; std::cout << *p << endl; & takes the address of a variable *p dereferences p source of many bugs

Pointers and Arrays closely related in C++ Can traverse an array with pointers char s[] ={'h','e','l','l','o','\0'}; for (int i=0;s[i]<5;i++) cout << s[i] << endl; for (char *p = s;*p!=0;p++) cout << *p << end; A nul terminated array of chars is a C-style string Note we assigned an array to a pointer and incremented a pointer

C-Style Strings Double quoted strings are C-Style strings We can initialise arrays with them We can assign them to pointers to char We can print them with std::cout char s1[] = "hello"; const char *s2 = "hello”; std::cout << s1 << s2;

Pointers and Const A pointer has two parts –the pointer –what it points to Which one does const apply to? Both are allowed

Pointers and Const Example char s[] = "hello"; char s2[] = "bye-bye"; const char* pc = s; pc[1] = 'a'; //error pc = s2; //ok char* const cp = s; cp[1] = 'a'; //ok cp = s2; //error const char* const cpc = s; cpc[1] = 'a'; //error cpc = s2; //error

References An alternative name for an item Must always reference a valid item Can not switch items Must be initialised when declared Like a pointer that automatically gets dereferenced int i = 42; int &r = i; r++; std::cout << i << std::endl;

Pointers, References and Const References are often used as arguments Pointers are as well If the argument is not modified it should be const char* strcpy(char* dest, const char* src) { char *result = dest; while(*dest++ = *src++) /*nothing*/; return dest; }