Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Similar documents
9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

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

Chapter 2: Basic Elements of C++

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

A First Program - Greeting.cpp

VARIABLES & ASSIGNMENTS

Fundamentals of Programming CS-110. Lecture 2

BASIC ELEMENTS OF A COMPUTER PROGRAM

Introduction to Programming EC-105. Lecture 2

CSCE 110 PROGRAMMING FUNDAMENTALS

REVIEW. The C++ Programming Language. CS 151 Review #2

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

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Objectives. In this chapter, you will:

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

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 2: Overview of C++

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

COMP Primitive and Class Types. Yi Hong May 14, 2015

UNIT- 3 Introduction to C++

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2: Using Data

1.3b Type Conversion

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

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

6.096 Introduction to C++ January (IAP) 2009

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Computer System and programming in C

Chapter 2: Introduction to C++

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Lecture 3 Tao Wang 1

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

Creating a C++ Program

Chapter 2. Outline. Simple C++ Programs

Unit 3. Constants and Expressions

These are reserved words of the C language. For example int, float, if, else, for, while etc.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Engineering Problem Solving with C++, Etter/Ingber

LECTURE 02 INTRODUCTION TO C++

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Integer Data Types. Data Type. Data Types. int, short int, long int

Main Memory Organization

The C++ Language. Arizona State University 1

CS242 COMPUTER PROGRAMMING

BITG 1233: Introduction to C++

Chapter 2 Basic Elements of C++

LECTURE 3 C++ Basics Part 2

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CHAPTER 3 Expressions, Functions, Output

Programming in C++ 6. Floating point data types

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Programming in C++ 5. Integral data types

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Fundamentals of Programming

Getting started with C++ (Part 2)

Visual C# Instructor s Manual Table of Contents

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Numbers. John Perry. Spring 2017

Declaration and Memory

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Outline. Data and Operations. Data Types. Integral Types

3. Java - Language Constructs I

Values, Variables, Types & Arithmetic Expressions. Agenda

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Chapter 3. Numeric Types, Expressions, and Output

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

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

Fundamental of Programming (C)

Lecture 2 Tao Wang 1

Topic 2: C++ Programming fundamentals

DEPARTMENT OF MATHS, MJ COLLEGE

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Introduction to Programming using C++

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

JAVA OPERATORS GENERAL

Programming. C++ Basics

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

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

Introduction to the C++ Programming Language

Reserved Words and Identifiers

Arithmetic Operators. Portability: Printing Numbers

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Operations. Making Things Happen

Transcription:

Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja 4 // Date: 08/23/2006 5 // Purpose: This program displays a welcome message to 6 // the user 7 //*********************************************************** 8 #include <iostream> 9 #include stdafx.h 10 11 using namespace std; 12 13 int main() 14 { 15 string name; 16 17 cout << Type your name, then press enter << endl; 18 19 cin >> name; /* read in the user s name */ 20 21 cout << Hello << name <<! << endl; 22 23 return 0; 24 } What are the important parts of this program? Keywords, variables, libraries, main function, input, output, comments. 9/20/06 CS150 Introduction to Computer Science 1 2 Declaration Statements Tells the compiler o The variable s type o The variable s name Examples of declaration statements const double PI = 3.14; double area; char character = a ; int count; bool bisitasquare; unsigned int bignumber = 4000000000; 9/20/06 CS150 Introduction to Computer Science 1 3 1

Identifiers C++ does place limits on what names you can call your variables Rules 1. Identifiers must begin with a letter or an underscore 2. Identifiers must consist of letters, numbers and underscore, nothing else 3. Identifiers cannot be a reserved keyword 9/20/06 CS150 Introduction to Computer Science 1 4 How to Choose a Data Type Ask yourself the following questions o Is the data a number or character? Is the data a list of characters? o What are the largest and smallest numbers that may be stored? o How much memory does the variable use? o Is the variable signed (positive and negative)? o How many decimal places of precision does the variable need? 9/20/06 CS150 Introduction to Computer Science 1 5 Variable Ranges Type int short int long int Size 2 bytes Values 2,147,483,648 to 2,147,483,647 32,768 to 32,767 2,147,483,648 to 2,147,483,647 unsigned int char float double 1 byte 8 bytes 0 to 4,294,967,295 256 character values 1.2e 38 to 3.4e38 2.2e 308 to 1.8e308 9/20/06 CS150 Introduction to Computer Science 1 6 2

Arithmetic Operators Operators allow us to manipulate data o Unary: operator operand o Binary: operand operator operand (left hand side) (right hand side) Operator Meaning Type Example - Negation Unary - 5 = Assignment Binary rate = 0.05 * Multiplication Binary cost * rate / Division Binary cost / 2 % Modulus Binary cost % 2 + Addition Binary cost + tax - Subtraction Binary total - tax 9/20/06 CS150 Introduction to Computer Science 1 7 Division grade = 100 / 40; grade is 2 o If both operands of the division operator are integers, then integer division is performed. o We say the integer is truncated. Everything after the decimal point is dropped. No rounding. grade = 100.0 / 40; o grade is 2.5 o What data type should grade be declared as? 9/20/06 CS150 Introduction to Computer Science 1 8 Modulus Modulus is the remainder after integer division grade = 100 % 20; o grade =? grade = 100 % 30; o grade =? rem = x % n; o What are the possible values for rem? 9/20/06 CS150 Introduction to Computer Science 1 9 3

Exponents The exponent operator was missing from the list! x 2 y n C++ does not provide an exponent operator as part of the language Use pow() in the cmath library #include <cmath> double area; area = pow(4, 2); // area = 4 2 9/20/06 CS150 Introduction to Computer Science 1 10 Implicit Type Conversion (3.3) What happens when we mix the data types of operands during mathematical operations o What happens when we save a double as an int? o What happens when an int is multiplied by a float? Data types are ranked A data type outranks another if it can hold a larger number 9/20/06 CS150 Introduction to Computer Science 1 11 long double Highest double float unsigned long long unsigned int int Lowest 9/20/06 CS150 Introduction to Computer Science 1 12 4

Rules for Type Conversion Rule 1: char, short, and unsigned short are automatically promoted to int Rule 2: When an operator works with values of different types, the lower ranking value is promoted to the higher ranking Rule 3: When the value of an expression is assigned to a variable, it is converted to the data type of that variable 9/20/06 CS150 Introduction to Computer Science 1 13 Uses of Type Casting Preventing integer division int books = 30, months = 7; double bookspermonth; bookspermonth = static_cast<double>(books) / months; o What about this statement? bookspermonth = static_cast<double>(books / months); Displaying a char from its ASCII value int number = 65; cout << static_cast<char>(number) 9/20/06 CS150 Introduction to Computer Science 1 14 Examples of Combined Assignments Operator += -= *= /= %= Example Usage x += 5; y -= 2; z *= 10; a /= b; c %= 3; Equivalent To x = x + 5; y = y 2; z = z * 10; a = a / b; c = c % 3; 9/20/06 CS150 Introduction to Computer Science 1 15 5

Relational Operators, Explained! Operator > Meaning Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to!= Not equal to o All are binary operators o Left to right associativity 9/20/06 CS150 Introduction to Computer Science 1 16 Relational Expression An expression is a statement that has value Relational expression: an expression that uses a Relational Operator o its value is a Boolean value (True or False) int x = 9, y = 42; x > y y == x // y = x; is the assignment operator x <= (x * y + 99) 9/20/06 CS150 Introduction to Computer Science 1 17 Precedence (page 1125) Precedence Operators (Highest to Lowest) (unary negation) - Arithmetic Operators * / % + - > >= < <= Relational Operators ==!= = += -= *= /= %= Assignment Operators 9/20/06 CS150 Introduction to Computer Science 1 18 6

Grouping! To override precedence we use grouping symbols, ( ) o average = ( a + b +c ) / 3; (3 + 12) * 2-3 4 + 17 % (3 + 9) 6-2 * 9 / ((3 * 4) 9) o Work from the inside ( ) outward 9/20/06 CS150 Introduction to Computer Science 1 19 Boolean value (True or False) How does the computer represent True and False? New data type: bool bool tvalue = true; // 1 bool fvalue = false; // 0 9/20/06 CS150 Introduction to Computer Science 1 20 Formally defined if( expression ) { statement 1; statement 2;... statement n; } What is an expression? Just like a function, start at the top and execute in order to the bottom 9/20/06 CS150 Introduction to Computer Science 1 21 7