QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Similar documents
6.096 Introduction to C++ January (IAP) 2009

by Pearson Education, Inc. All Rights Reserved.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

UNIT- 3 Introduction to C++

Computers Programming Course 6. Iulian Năstac

Ch. 3: The C in C++ - Continued -

Introduction to Programming Using Java (98-388)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Expressions and Casting

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Introduction to C++ with content from

QUIZ. What is wrong with this code that uses default arguments?

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Review of the C Programming Language


QUIZ: What value is stored in a after this

Review of the C Programming Language for Principles of Operating Systems

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Creating a C++ Program

LECTURE 3 C++ Basics Part 2

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Unit 3. Operators. School of Science and Technology INTRODUCTION

Introduction to Programming

Flow Control. CSC215 Lecture

Lecture 3. More About C

Lecture 3: C Programm

ARG! Language Reference Manual

CS201 Some Important Definitions

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

A Fast Review of C Essentials Part I

Absolute C++ Walter Savitch

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:

Computers Programming Course 5. Iulian Năstac

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Tokens, Expressions and Control Structures

Lexical Considerations

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

1 Lexical Considerations

Data Types and Variables in C language

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

C: How to Program. Week /Mar/05

Operators. Lecture 3 COP 3014 Spring January 16, 2018

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

QUIZ. Can you find 5 errors in this code?

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Chapter 3: Operators, Expressions and Type Conversion

Basic Types, Variables, Literals, Constants

ET156 Introduction to C Programming

Lexical Considerations

Chapter 2 Basic Elements of C++

CSE 230 Intermediate Programming in C and C++ Functions

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Chapter 2 - Introduction to C Programming

Type Conversion. and. Statements

CSCI 171 Chapter Outlines

Information Science 1

C Programming Multiple. Choice

M1-R4: Programing and Problem Solving using C (JAN 2019)

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

The C++ Language. Arizona State University 1

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:

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Type Checking. Prof. James L. Frankel Harvard University

Expressions and Precedence. Last updated 12/10/18

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Problem Solving with C++

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Fundamentals of Programming

Programming and Data Structures

CS201- Introduction to Programming Current Quizzes

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

ME 461 C review Session Fall 2009 S. Keres

Introduction to C# Applications

Fundamental of Programming (C)

C++ Programming: From Problem Analysis to Program Design, Third Edition

UNIT-2 Introduction to C++

Motivation was to facilitate development of systems software, especially OS development.

CS201 Latest Solved MCQs

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Ch. 12: Operator Overloading

CSc Introduction to Computing

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

C-LANGUAGE CURRICULAM

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

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

Chapter 2: Introduction to C++

Transcription:

QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below:

This is a template, used for generic programming!

QUIZ 2. Why is the vector class called a container? 3. Explain how the C++ vectors and the C/C++ arrays are: a. Similar b. Different 4. How can we add elements to a vector at runtime?

This is a template, used for generic programming!

Ch. 3: The C in C++

Creating functions

Creating functions Argument names are optional in the prototype (declaration).

Function return values

A function can have more than one return statement

Using the C function library

Using the C function library The difference between #include <header> and #include "header"

SKIP Creating your own libraries with the librarian (It is specific to the IDE of programming package we re using)

Controlling execution

QUIZ Write a C++ program that asks the user for an integer, and then displays one of 3 possible messages: Less than 0 Equal to 0 Greater than 0

QUIZ Encapsulate the decision part of the previous program in a function called verdict(). What are the inputs and outputs of this function?

The while loop

Not in text

The while loop

In a loop, the decision (diamond) can be at the beginning or at the end of the loop! Not in text 20

The do while loop

Design decision: while or do while?

The for loop

Not in text

QUIZ: for loop

QUIZ: for loop

Read and take notes: The break and continue keywords switch Using and misusing goto EOL 6

QUIZ Use a while loop to implement the repeated division algorithm. Hint: The algorithm stops when the quotient is zero!

QUIZ: What s wrong in this program?

That s why the example in the text has char instead of string!

Is this switch correct?

It s OK to switch on a char, since char is an 8-bit integer

Would this program compile in C?

Would this program compile in C?

What s wrong in this switch?

switch statement fall-through

QUIZ Give a code example in which goto is acceptable, and explain why!

Recursion base-case, a.k.a. bottoming-out

QUIZ Write a recursive function to implement the repeated division algorithm. Hint: The function should print the digits in correct order!

Introduction to operators Arithmetic operators: + - * / % Unary, binary, ternary Precedence and parentheses Compound assignment: += -= *= /= %= Auto-increment/decrement, pre- and post-

Introduction to data types

Source: http://www.cplusplus.com/reference/climits/

bool, true, & false The Standard C++ bool type can have two states expressed by the built-in constants true (which converts to an integral one) and false (which converts to an integral zero). All three names are keywords. In addition, some language elements have been adapted:

Specifiers Specifiers modify the meanings of the basic built-in types and expand them to a much larger set. There are four specifiers: long, short, signed, and unsigned.

Specifiers long and short modify the maximum and minimum values that a data type will hold. A plain int must be at least the size of a short. The size hierarchy for integral types is: short int int long int long long int All the sizes could conceivably be the same, as long as they satisfy the minimum/maximum value requirements. On a machine with a 64-bit word, for instance, all the data types might be 64 bits. On our machines, the sizes are 16 32 32 64 bit, respectively.

Specifiers The signed and unsigned specifiers tell the compiler how to use the sign bit with integral types and characters (floatingpoint numbers always contain a sign): An unsigned number does not keep track of the sign and thus has an extra bit available, so it can store positive numbers twice as large as the positive numbers that can be stored in a signed number. signed is the default and is only necessary with char; char may or may not default to signed. By specifying signed char, you force the sign bit to be used.

sizeof operator Do not confuse the size of a data type with its limits! sizeof climits and cfloat

Introduction to pointers The cast is done just to avoid displaying the addresses in hex!

Let s figure out which memory areas these addresses are in!

Pointers are variables that store addresses

Declaring and initializing pointers

What can we do with pointers? Access the variables they point to!

QUIZ Create two floats and two pointers to them. Swap the floats using the pointers.

What can we do with pointers? Access the variables they point to! These are the basics of pointers: you can hold an address, and you can use that address to modify the original variable. But the question still remains: why do you want to modify one variable using another variable as a proxy?

Alfred Hitchcock's Saboteur (1942) To be continued

Read and take notes: Specifying storage allocation all subsections Operators and their use Stop before C++ explicit casts EOL 7

What can we do with pointers? Access the variables they point to! These are the basics of pointers: you can hold an address, and you can use that address to modify the original variable. But the question still remains: why do you want to modify one variable using another variable as a proxy?

We want to be able to modify an outside object from inside a function

Passing function arguments by value

Solution: Passing function arguments by reference pointer (address), using pointers (addresses)

QUIZ Write a C++ program with a function that swaps its two arguments, passed by pointer (address).

QUIZ Write a C++ program with a function that swaps its two arguments, passed by pointer (address).

Introduction to C++ references Accessing the value referenced is simpler! Not *r! Passing the parameter is simpler! Not &x!

QUIZ Write a C++ program with a function that swaps its two arguments, passed by reference.

Conclusion: In C++, function arguments can be passed by: Value (copy) Pointer (address) Reference

Pointer to void

Remember that malloc() returns a pointer to void! Not in text

Once you assign to a void* you lose any information about what type it is. This means that before you can use the pointer, you must cast it to the correct type:

Scoping

Useless!

Although it s permitted by C++, declaring variables in while and switch is awkward

The problem is caused by the precedence of C++ operators

C++ syntax does not allow more than one level of parentheses around declarations!

Individual work for next time: Solve end-of-chapter problem #7. EOL 8