CS1020E: DATA STRUCTURES AND ALGORITHMS I

Save this PDF as:

Size: px
Start display at page:

Transcription

1 CS1020E: DATA STRUCTURES AND ALGORITHMS I Tutorial 1 Basic C++, OOP Problem Solving (Week 3, starting 22 August 2016) 1. Evaluation Order (Note: You can use any other C++ code editor/compiler). Examine the code snippet. What is the output, and why? Tip: Check your answer! Create a program in vim, paste main method within. Compile and run in sunfire. int main() { int a = -1, b = 1, c = 1, d = 0, e = 2, f = 2, g = 0; int h = f-- && e++ && d++ && c-- b++ a++; if (g = 9) { cout << a << b << c << d << e << f << g << h << endl; else { cout << h << g << f << e << d << c << b << a << endl; return 0; a b c d e f g h Answer Related Concepts Operator precedence vs evaluation order Conditions, logical operator short circuiting Assignment vs equality The most important lesson here is to write readable code! Write code first for humans to read, then for the computer to understand. Complex conditions and poor variable names make life difficult for humans. Separate complex conditions into simpler statements, using descriptive variable names. C++ s operator precedence can be found at ++ and -- here have highest precedence, followed by &&, then, and finally the assignment operator =. However, this does NOT mean that ALL ++ and -- in a statement are executed first. Rather, the expression is evaluated left to right in general, taking into account operator precedence. Because of logical operators && and, short circuiting may take place, i.e. part of a statement may be skipped because the preceding part of the statement already determines the result. If no short circuiting takes place, the expression (before assignment to h) is evaluated as: (((f-- && e++) && d++) && c--) b++ a++ A condition is logically true if an expression evaluates to true (bool) or a non zero value, false otherwise. f-- (post decrement) is not to be confused with --f (pre decrement). f-- means: use the current value of f in evaluating the expression, then decrease the value of f in memory by 1. Pre decrement works the other way round. Page 1 of 8

2 ? (((f-- && e++ ) && d++) && c-- ) b++ a++ f, holding the value of 2, evaluates to true. It is then decremented. No short circuiting occurs because the result of the expression within the innermost brackets is not known yet ? (((true && e++ ) && d++) && c-- ) b++ a++ e, holding the value of 2, evaluates to true. It is then incremented ? ((true && d++) && c-- ) b++ a++ d, holding the value of 0, evaluates to false. It is then incremented ? (false && c-- ) b++ a++ It is certain that the brackets now evaluate to false, so c-- is NOT executed (short circuiting). false b++ a++ b, holding the value of 1, evaluates to true. It is then incremented ? true a++ It is certain that the expression now evaluate to true, so a++ is NOT executed (short circuiting) true true is converted to an int value of 1 and assigned to h. Next, if the condition were (g == 9), the else block would be executed. However, (g = 9) assigns the value of 9 to g, and uses the new value of g as the result of the expression. As g is now non zero, the if block is executed. Lesson: Avoid using the wrong operator in your code! Differentiate between = and ==. In subsequent tutorials, you are expected to trace through code by yourself, as in Q1. Draw diagrams to help you understand what happens in memory, as in Q2. Page 2 of 8

3 2. Understanding Pointers In OOP languages, pointers (so named in C++; may be called references in other languages) are widely used to locate one object from another. It is necessary to have a firm understanding of them. For each of these cases, independent of one another, draw how the variables may appear in memory, and what output you would expect. You do not need to worry about the exact memory addresses, but you should find out how different expressions are related to each other.,,, and represent memory addresses. (a) has been done for you. (d) to (g) may be more difficult. Remember to check your answer. (a) int i = 3; cout << &i; (b) int* p = new int(3); cout << &p << p << *p; (c) int* ap = new int[3]; for (int i = 0; i < 3; i++) ap[i] = i - 1; cout << &ap << ap << *ap << ap[0]; (d) int i = 3; cout << *&i; (e) int* p = new int(3); cout << *&p << &*p << **&p; (g) int* p = new int(3); int** dp = &p; int*** tp = &dp; cout << *tp << &**tp << *&*tp << **&tp; (f) int** dp = new int* [3]; for (int i = 0; i < 3; i++) dp[i] = new int(i-1); cout << &dp << dp << *dp << dp[0] << **dp << *dp[0]; (a) &i The output &i is. stack i (b) Contents The output is heap stack p (c) Contents The output is heap stack ap Page 3 of 8

4 (d) The output is stack i (e) Contents The output is heap stack p (f) Contents The output is heap stack dp (g) Contents The output is heap stack tp dp p Also, when the new keyword is used, the system dynamically allocates memory for the newly created object. Therefore, the object ends up in a portion of memory called the heap. Otherwise, when new is not used, objects assigned to variables declared within functions reside on the stack. Why do we need to bother about heap vs stack memory? What other keyword and syntax is/are involved? Answer (a) The ampersand & in the second line is the address of operator. It is used to read the memory address the variable is located in. Therefore, the result of &i is. (b) &p The output is 3. *p heap stack p The asterisk * in the first line is part of the datatype, i.e. p is an integer pointer variable. The asterisk in the second line is the indirection (dereferencing) operator. It is used to read the contents of the memory address pointed to by p, which has the value of 3. Page 4 of 8

5 (c) &ap Contents The output is 1 1. *ap heap stack ap ap[0] ap[1] The first line creates an integer array of 3 elements on the heap, and assigns the address to an integer pointer variable. *ap (indirection) and ap[0] (array subscript) are equivalent, both dereferencing operators. They are both equivalent to *(ap + 0). (d) &i The output is 3. stack i *&i The * and & (indirection and address of) operators have right to left associativity. This means that in *&i, the address of i is first taken (&i), following which, the contents of the memory address pointed to by &i are read. (e) &*p &p The output is 3. *p heap stack p *&p **&p Remember, what is printed out is always the contents of some memory location. The contents may be a value (as when printing **&p), or another memory location (as when printing *&p). (f) &dp Contents *dp **dp heap stack dp dp[0] dp[1] *dp[0] *dp[1] The output is 1 1. new int* [3] creates an array of 3 elements. Each element is an integer pointer, i.e. each element contains the memory address of a location that stores an int. [] has a higher precedence than *, so *dp[0] moves to the 0 th element of the array, and then to the location of the integer containing 1. Page 5 of 8

6 Just a note: dp[0] to dp[2] are in a contiguous block of memory because they are the elements of an array. However, *dp[0], *dp[1] and *dp[2] are not guaranteed to be adjacent to each other. If any of (b) (g) is part of your program, after you are done with the integers and the array, do not forget to release them, as they were created on the heap using the new keyword. Objects on the heap need to be released, as the system does not take back the memory they are located at, even when the function exits. For example, in this part: First, use a for loop to delete each pointer for (int i = 0; i < 3; i++) delete dp[i]; Remember, the memory at dp[i] does not get freed, but what is pointed to does, i.e. *dp[i]. Then delete [] dp to delete the array that dp points to. (g) &tp heap stack tp &dp dp *tp &p p *dp **tp By now, you may have observed that & and * are somewhat the opposite of each other. &tp The output is. heap stack tp *&tp dp *tp p **tp 3. Object-Oriented Programming You want to print out the lyrics of this song 1, to teach (or confuse) kids about the sounds animals make: Related Concepts (Q2) & results in a pointer * and [ ] dereferences Array pointers, double pointers Heap vs stack Freeing resources delete pointer Dog goes woof Cat goes meow Bird goes tweet Mouse goes squeak Cow goes moo The lyrics can be generalized for different animals, each having a different name and sound. With knowledge of object oriented programming, you want to demonstrate that it is possible to write a program that displays the song. To show that your program works, add the 5 animals above and test your program. 1 Adapted from The Fox by Ylvis, 2013 Page 6 of 8

7 Don t forget to include the necessary system header, and use the appropriate namespace. Use the skeleton on the next page to solve the problem. class Animal { /* TODO: Implement data and functionality of an Animal here */ ; class Song { private: Animal** _animals; const int _size; public: Song() { /* TODO: Create your zoo, an Animal* array */ ~Song() { /* TODO: Cleanup the 5 animals and the array... */ void display() { for (int i = 0; i < _size; i++) cout << endl; /* TODO: Add the lyrics here... */ ; Related Concepts int main() { Class vs object Song song; Access modifiers song.display(); return 0; Member variables & encapsulation Constructor & destructor Member functions, accessors, mutators Invoking member Answer Animal has 2 attributes: name and sound. These are modelled as string member variables each Animal object has a name and sound. The data (member variables) is encapsulated by declaring them with the private access modifier. This means, outside the animal class, we cannot directly access or modify any Animal object s data. getname() and getsound() are member functions serving as accessors. getname() returns the value of the _name member variable belonging to the Animal object pointed to by this. As we do not need to modify an animal s attributes, there are no mutators here. #include <iostream> #include <string> using namespace std; class Animal { private: string _name; // e.g. Cow string _sound; // e.g. moo public: Animal(string name, string sound) { _name = name; _sound = sound; string getname() { return _name; string getsound() { return _sound; ; Page 7 of 8

8 A constant const must be initialized, and it cannot be subsequently assigned a value. Therefore, we use the initializer list to provide a value to the constant _size. class Animal { /* TODO: Implement data and functionality of an Animal here */ ; class Song { private: Animal** _animals; const int _size; public: Song() : _size(5) { _animals = new Animal* [_size]; _animals[0] = new Animal("Dog", "woof"); _animals[1] = new Animal("Cat", "meow"); _animals[2] = new Animal("Bird", "tweet"); _animals[3] = new Animal("Mouse", "squeak"); _animals[4] = new Animal("Cow", "moo"); ~Song() { for (int i = 0; i < _size; i++) delete _animals[i]; delete [] _animals; void display() { for (int i = 0; i < _size; i++) cout << _animals[i]->getname() << " goes " << _animals[i]->getsound() << endl; ; Remember to return memory allocated on the heap, and only memory allocated on the heap! Use the delete keyword on the pointers to those objects, and NOT on the objects themselves. _animals[i]->getname() is equivalent to (*_animals[i]).getname(). First, _animals[i] moves from the (Animal pointer) array pointer to the (Animal pointer) array, i spaces from element 0. The result is an animal pointer. *_animals[i] moves from the Animal pointer to an Animal object. We then read the name of the Animal object using the getname() accessor member. As we have mentioned in Q1 and Q2, the first 100 times you encounter some OOP code, draw out what the code does in memory! Don t view code as mere text. Hope you had fun, prepare well for tutorial 2 Draw diagrams Attempt tutorials Test your solution Page 8 of 8

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Data types define the way you use the storage (memory) in the programs. By specifying a data type, you tell the sompiler how to create a particular piece

Quiz 3 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 3 S o l u t i o n s. Q1: C++ functions other than main are executed:

Quiz 3 Solutions Q1: C++ functions other than main are executed: a. Before main executes. b. After main completes execution. c. When they are explicitly called by another function. d. Never. ANS c. When

The C Programming Language course syllabus associate level

TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ 2016 Fall Exam 3 Friday, November 18, 2016 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++ 2016 Fall Exam 3 Friday, November 18, 2016 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,

BHARATHIAR UNIVERSITY: COIMBATORE CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++ LANGUAGE

Certificate in Advanced Programming - C++ Language Page 1 of 7 BHARATHIAR UNIVERSITY: COIMBATORE 641046 CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 2: Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with the basic components of a C++ program,

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

SOFTWARE ENGINEERING 2: OBJECT ORIENTED SOFTWARE ENGINEERING

SOFTWARE ENGINEERING 2: OBJECT ORIENTED SOFTWARE ENGINEERING 1. This is a general question about Object Oriented Software Engineering. a) Compare and contrast how software complexity is handled in Structured

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members

Example 7. na20.nada.kth.se 883)./a.out y = y = y = y = #include <iostream.h> #include <math.

In the example above two functions are defined. Both increments the value of i by one, but fcn1 uses call-by-value as parameter passing mechanism and fcn2 uses call-by-reference. call-by-value means that

Adjusted/Modified by Nicole Tobias. Chapter 2: Basic Elements of C++

Adjusted/Modified by Nicole Tobias Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types

Which of the following is the correct operator to compare two variables? A. := B. = C. equal D. ==

Group 1 Question 1. What is the correct value to return to the operating system upon the successful completion of a program? A. -1 B. 1 C. 0 D. Programs do not return a value. Question 2. What is the only

Chapter 7 Linked Lists In order to use linked lists, you need to learn how to allocate memory dynamically on the heap and you need to learn about objects. Recall that local variables are temporary which

Summary. Pre requisition. Content Details: 1. Basics in C++

Summary C++ Language is one of the approaches to provide object-oriented functionality with C like syntax. C++ adds greater typing strength, scoping and other tools useful in object-oriented programming

Programming Fundamental. Instructor Name: Lecture-2

Programming Fundamental Instructor Name: Lecture-2 Today s Lecture What is Programming? First C++ Program Programming Errors Variables in C++ Primitive Data Types in C++ Operators in C++ Operators Precedence

Introduction to C++ Programming Vahid Kazemi

Introduction to C++ Programming Vahid Kazemi Overview An overview of C/C++ - Basic types, Pointers, Arrays, Program control, Functions, Arguments, Structures, Operator overloading, Namespaces, Classes,

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to

CS201- Introduction to Programming Latest Solved Mcqs from Final term Papers July 11,2011

CS201- Introduction to Programming Latest Solved Mcqs from Final term Papers July 11,2011 Mc100401285 moaaz.pk@gmail.com Moaaz Siddiq Bc100400662 bc100400662asad@gmail.com Asad Ali Latest Mcqs FINALTERM

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

C++ Mini-Course Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion C Rulez! C++ Rulez! C++ Mini-Course Part 1: Mechanics C++ is a

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Chapter 3 Control Structures 1 Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring

Comp Arrays and Pointers. Class Notes. T h e G r o u p o f T h r e e

Comp 2401 Arrays and Pointers Class Notes 2013 T h e G r o u p o f T h r e e Introduction To Arrays: In C programming, one of the frequently problem is to handle similar types of data. For example: if

Pointers and Strings. Objectives

5 Pointers and Strings Objectives To be able to use pointers. To be able to use pointers to pass arguments to functions by reference. To understand the close relationships among pointers, arrays and strings.

Chapter 2: Problem Solving Using C++

Chapter 2: Problem Solving Using C++ 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common

Final Exam Review. CS 1428 Fall Jill Seaman. Final Exam

Final Exam Review CS 1428 Fall 2011 Jill Seaman 1 Final Exam Friday, December 9, 11:00am to 1:30pm Derr 241 (here) Closed book, closed notes, clean desk Comprehensive (covers entire course) 25% of your

Object Oriented Software Design II

Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

OBJECT ORIENTED PROGRAMMING IN C++

OBJECT ORIENTED PROGRAMMING IN C++ For Off Campus BSc Computer Science Programme UNIT 1 1. The goal of programmers is to develop software that are. A. Correct B. Reliable and maintainable C. Satisfy all

Tutorial 16 Shopping List Application: Introducing Pointers, References and Dynamic Data Structures

Tutorial 16 Shopping List Application: Introducing Pointers, References and Dynamic Data Structures Outline 16.1 Test-Driving the Shopping List Application 16.2 Introducing Pointers 16.3 Pointer Operators

Common Syntax and Semantic Errors

C H A P T E R 2 Common Syntax and Semantic Errors 2.1 CHAPTER OBJECTIVES To understand the fundamental characteristics of syntax and semantic errors To be able to identify specific common syntax and semantic

Advanced C++ Exception Handling Topic #5

Advanced C++ Exception Handling Topic #5 CS202 5-1 CS202 5-2 Exception Handling Throwing an Exception Detecting an Exception Catching an Exception Examine an Example using Classes and Operator Overloading

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

PROBLEM SOLVING WITH SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON Addison Wesley Boston San Francisco New York London

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

Tutorial-3a: First steps with C++ programming

HTTP://WWW.HEP.LU.SE/COURSES/MNXB01 Introduction to Programming and Computing for Scientists (2015 HT) Tutorial-3a: First steps with C++ programming Programming for Scientists Tutorial 3a 1 / 17 Quick

CS193D Handout 04 Winter 2005/2006 January 11, 2006 A Crash Course in C++

CS193D Handout 04 Winter 2005/2006 January 11, 2006 A Crash Course in C++ Most people taking CS193D will already have some experience with C++. This handout is a quick refresher in case it s been a while.

Ch 7-1. Object-Oriented Programming and Classes

2014-1 Ch 7-1. Object-Oriented Programming and Classes May 10, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

Review: arrays, pointers, structures (Chapter 1) Data Types. Review: Arrays. Data Types (C/C++) Data Type: CS 3358 Summer I 2012.

Review: arrays, pointers, structures (Chapter 1) Data Types Data Type: CS 3358 Summer I 2012 Jill Seaman set of values set of operations over those values example: Integer whole numbers, -32768 to 32767

! instance variable: a member variable in a class. ! static variable: one variable shared among all. ! static member function:

Ch 14: More About Classes 14.1 Instance and Static Members instance variable: a member variable in a class. Each object (instance) has its own copy. CS 2308 Spring 2014 Jill Seaman 1 static variable: one

FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I:

FORM 1 (Please put your name, section # (10am- 001/2pm-002, and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The following statement is legal: cout >> Hello, my name is

Bhakta Kavi Narsinh Mehta University, Junagadh

Bhakta Kavi Narsinh Mehta University, Junagadh Draft Syllabus for B.Sc. (Computer Science) Bachelor of Science (Computer Science) (Semester - 1) Effective From June - 2016 B.Sc. (C.S.) (Semester - 1) CS-101:

An Incomplete C++ Primer. University of Wyoming MA 5310

An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

6.096 Lab 1. Due: 7 January 12:30:00. September 20, 2011

6.096 Lab 1 Due: 7 January 1:30:00 September 0, 011 1 Additional Material 1.1 Constants A constant is an expressions with a fixed value. Kinds of constants: Literals: used to express particular values

7 Introduction to C++

7 Introduction to C++ 7.1 Introduction C++ is an extension to C Programming language. It was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup. It is a deviation from traditional

CS2 Final Study Guide

CS2 Final Study Guide - Know how to write an assignment operator, copy constructor and destructor for a simple class that has dynamically allocated memory. Trees 1. Define the following terms o binary

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Simple C++ Programs Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input and Output Basic Functions from

Glossary of Object Oriented Terms

Appendix E Glossary of Object Oriented Terms abstract class: A class primarily intended to define an instance, but can not be instantiated without additional methods. abstract data type: An abstraction

Control Structure. Pseudocode: Input number If number is even Then Print even Else print odd. Flowchart: Begin. End. Print Odd. number even?

Control Structure Normally, a program is executed in a sequential manner.however, in many cases, a program has to choose among alternative statements C++ provides constructs that enable the programmer

Pointer. Accessing address of a variable POINTER.

www.cppforschool.com Pointer Accessing address of a variable Computer s memory is organized as a linear collection of bytes. Every byte in the computer s memory has an address. Each variable in program

Introduction to C++ Programming

Introduction to C++ Programming 1 Outline Introduction to C++ Programming A Simple Program: Printing a Line of Text Another Simple Program: Adding Two Integers Memory Concepts Arithmetic Decision Making:

()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, / 47

A Crash Course in Programming with C++ and the Ubuntu O.S. Dr. Daniel A. Ray MCS Dept UVa-Wise June 8, 2010 ()A Crash Course in Programming with C++ and the Ubuntu O.S. June 8, 2010 1 / 47 Outline What

Basic C Syntax. Comp-206 : Introduction to Software Systems Lecture 10. Alexandre Denault Computer Science McGill University Fall 2006

Basic C Syntax Comp-206 : Introduction to Software Systems Lecture 10 Alexandre Denault Computer Science McGill University Fall 2006 Next Week I'm away for the week. I'll still check my mails though. No

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is preceded by an equal sign d. its name has undereline 2. Associations

Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement

Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement Outline 9.1 Test-Driving the Income Tax Calculator Application 9.2 Introducing the switch Multiple-Selection

11 Operator Overloading; String and Array Objects The whole difference between construction and creation is exactly this: that a thing constructed can only be loved after it is constructed; but a thing

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

Dynamic Memory Allocation using a Custom Implemented Single-Linked List

Dynamic Memory Allocation using a Custom Implemented Single-Linked List Anders Eiler April 8, 2013 1 Introduction The objective of this paper is to describe how to dynamically allocate memory at runtime

Java Memory and Intro to OOP

+ Java Memory and Intro to OOP + What is Java? Programming language Standard libraries ex. Math.random() Tools: compiler, runtime, others + What is the compiler? AKA javac A compiler is a program that

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Boolean Expressions, Conditions, Loops, and Enumerations Relational Operators == // true if two values are equivalent!= // true if two values are not equivalent < // true if left value is less than the

C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

2. Place the following computers in order from oldest to most recent. _middle ABC _oldest Analytical Engine _newest ENIAC

Chapter 1 1. Match the following people with their contribution to the field of Computer Science, where a name can be used more than once. A. John Atanasoff and Cilfford Berry B. Augusta Ada Byron C. Charles

Chapter 6 Repetition

Chapter 6 Repetition 6-1 Repetition in C++ The final control/logic structure is repetition: Repetition repeating a block of code until a condition is met There are three repetition statements available

Introduction to the C++ Programming Language

L E S S O N S E T 2 Introduction to the C++ Programming Language PURPOSE PROCEDURE 1. To briefly introduce the C++ programming language 2. To show the use of memory in programming 3. To introduce variables

CSCI-1200 Data Structures Fall 2015 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Fall 2015 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

Python to C/C++ Fall 2011

Python to C/C++ Fall 2011 1. Main Program Python: Program code is indented after colon : def main(): body of program C/C++: Have more setup overhead. C: Both require #include directives to access libraries

Chapter 4 Operators & Arithmetic

Chapter 4 Operators & Arithmetic 4-1 Arithmetic in C++ Arithmetic expressions can be made up of constants, variables, operators and parentheses. The arithmetic operators in C++ are as follows + (addition)

CS11 Introduction to C++ Spring Lecture 1

CS11 Introduction to C++ Spring 2011-2012 Lecture 1 Welcome! n Introduction to C++ Assumes general familiarity with C syntax and semantics n Loops, functions, pointers, memory allocation, structs, etc.

Introduction to C Programming

Introduction to C Programming C HOW TO PROGRAM, 6/E 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2.1 Introduction The C language facilitates a structured and disciplined approach to computer

C++ How to Program, 8/e 1992-2012 by Pearson Education, Inc. 1992-2012 by Pearson Education, Inc. 1992-2012 by Pearson Education, Inc. We ve studied fixed-size data structures such as one-dimensional arrays

C++ Introduction to class and data abstraction

C++ Introduction to class and data abstraction 1 Data abstraction A data abstraction is a simplified view of an object by specifying what can be done with the object while hiding unnecessary details In

IS0020 Program Design and Software Tools Midterm, Feb 24, 2004. Instruction

IS0020 Program Design and Software Tools Midterm, Feb 24, 2004 Name: Instruction There are two parts in this test. The first part contains 50 questions worth 80 points. The second part constitutes 20 points

Comp181 Midterm Examination, Fall Name Surname: Number: 90 min. 18/11/09

Comp181 Midterm Examination, Fall 09 10 Name Surname: Number: 90 min. 18/11/09 Answer ALL off the following questions. 1.What does the term hardware refer to? a. The relative difficulty of programming

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays

A First Book of C++ Chapter 2 Data Types, Declarations, and Displays Objectives In this chapter, you will learn about: Data Types Arithmetic Operators Variables and Declarations Common Programming Errors

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Functions in C++ Let s take a look at an example declaration: Lecture 2 long factorial(int n) Functions The declaration above has the following meaning: The return type is long That means the function

Curriculum Map. Discipline: Computer Science Course: C++

Curriculum Map Discipline: Computer Science Course: C++ August/September: How can computer programs make problem solving easier and more efficient? In what order does a computer execute the lines of code

Comp151. Definitions & Declarations

Comp151 Definitions & Declarations Example: Definition /* reverse_printcpp */ #include #include using namespace std; int global_var = 23; // global variable definition void reverse_print(const

Introduction to the MinGW C++ Compiler and the wxdev-c++ IDE

APPENDIX L Introduction to the MinGW C++ Compiler and the wxdev-c++ IDE This appendix is an introduction to the use of the wxdev-c++ development environment to create C++ programs. WxDev-C++ regards a

Case Study: Intersection of Sets

Case Study: Intersection of Sets In algebra, the intersection of two sets is defined as a new set that contains all the values common to both sets. For instance, consider the following two sets, A and

C++ Keywords. If/else Selection Structure. Looping Control Structures. Switch Statements. Example Program

C++ Keywords There are many keywords in C++ that are not used in other languages. bool, const_cast, delete, dynamic_cast, const, enum, extern, register, sizeof, typedef, explicit, friend, inline, mutable,

1 CS304 Final Term Solved MCQs Mega File (Latest All in One) User can make virtual table explicitly. True False In order to define a class template the first line of definition must be: template

Lecture 5. Function Pointers

Lecture 5 Complicated declarations Pointers to functions Templates Classes const member functions Constructors and destructor Allocating and deallocating memory Common pitfalls TNCG18 (C++): Lec 5 1 Function

Searching and Sorting Arrays

L E S S O N S E T 8 Searching and Sorting Arrays PURPOSE PROCEDURE 1. To introduce the concept of a search routine 2. To introduce the linear and binary searches 3. To introduce the concept of a sorting

int X[SIZE]; int Y[SIZE]; X == Y; // comparing two arrays with a relational operator return X; // using an array as the return value from a function

Aggregate Operations 1 An aggregate operation is an operation that is performed on a data structure, such as an array, as a whole rather than performed on an individual element. Assume the following declarations:

Assignment #1 (Due 11:59 P.M.; must be sent by the due date at 11:59 P.M.)

Assignment #1 (Due 9/26/2016 @ 11:59 P.M.; e-mail must be sent by the due date at 11:59 P.M.) This project is designed to help you master pointers. To that end, you'll get the most out of it by working

Chapter 13. Pointers and Linked Lists

Chapter 13 Pointers and Linked Lists Overview 13.1 Nodes and Linked Lists 13.2 Stacks and Queues Slide 13-2 13.1 Nodes and Linked Lists Nodes and Linked Lists n A linked list is a list that can grow and

SYSTEMS PROGRAMMING C++ INTRODUCTION

Faculty of Computer Science / Institute of Systems Architecture / Operating Systems SYSTEMS PROGRAMMING C++ INTRODUCTION Alexander Warg WHY C++? C++ is the language that allows to express ideas from the

Pointers and dynamic memory management in C

Pointers and dynamic memory management in C Jakob Rieck Proseminar C - 2014 Contents 1 Introduction 2 2 Pointers 4 2.1 Declaration & Initialization................... 5 2.2 Using pointers..........................

Proposal to add an absolute difference function to the C++ Standard Library

Proposal to add an absolute difference function to the C++ Standard Library Document number: N4318 Date: 2014-09-21 Project: Programming Language C++, Library Evolution Working Group Reply-to: Jeremy Turnbull

More class design with C++ Member or non-member function? l Class operations are typically implemented as member functions Declared inside class

1 2 3 4 More class design with C++ Member or non-member function? l Class operations are typically implemented as member functions Declared inside class definition Can directly access private members Usually

4.2 The while Loop. ***Same as Fig 3.6 in intro5e p91

Chapter 4 Loops Objectives To use while, do-while, and for loop statements to control the repetition of statements ( 4.2-4.4). To understand the flow of control in loop statements ( 4.2-4.4). To use Boolean

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

CpSc212 Goddard Notes Chapter 6 Yet More on Classes We discuss the problems of comparing, copying, passing, outputting, and destructing objects. 6.1 Object Storage, Allocation and Destructors Some objects

Object Oriented Software Design II

Object Oriented Software Design II C++ intro Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 26, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February 26,

an array of 10 characters would require 10 bytes of storage for data. On the other hand, would require 10*sizeof(int) amount of storage.

Lecture 05 C Arrays & pointers In this lecture Introduction to 1D arrays Array representation, access and updates Passing arrays to functions Array as a const pointer Dynamic arrays and resizing Introduction

Java Review (Essentials of Java for Hadoop)

Java Review (Essentials of Java for Hadoop) Have You Joined Our LinkedIn Group? What is Java? Java JRE - Java is not just a programming language but it is a complete platform for object oriented programming.

Java (12 Weeks) Introduction to Java Programming Language

Java (12 Weeks) Topic Lecture No. Introduction to Java Programming Language 1 An Introduction to Java o Java as a Programming Platform, The Java "White Paper" Buzzwords, Java and the Internet, A Short

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Classes and Objects Week 5 Gaddis:.2-.12 14.3-14.4 CS 5301 Fall 2015 Jill Seaman The Class! A class in C++ is similar to a structure.! A class contains members: - variables AND - functions (often called

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing

C Programming, Chapter 1: C vs. Java, Types, Reading and Writing T. Karvi August 2013 T. Karvi C Programming, Chapter 1: C vs. Java, Types, Reading and Writing August 2013 1 / 1 C and Java I Although the

13 Classes & Objects with Constructors/Destructors

13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.