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


 Zoe Kennedy
 2 years ago
 Views:
Transcription
1 Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 31 The cin Object Standard input stream object, normally the keyboard, used to read input from outside the computer into the program Like cout, needs to include iostream file #include <iostream> Information retrieved from cin with stream extraction operator >> Input is stored in one or more variables Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 33 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 34
2 The cin Object Displaying a Prompt cin converts data to the type that matches the variable: int height; double degree; cout << "How tall is the room? "; cin >> height; cout << "What is the temperature? "; cin >> degree; Notice that << and >> point in the direction that the data is flowing Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 35 Reading data from the user is normally a twostep process: Use cout to display a prompt on the screen Use cin to read a value from the keyboard Always use a prompt, which is a message that instructs the user to enter data. cout << "How tall is the room? "; cin >> height; Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 36 The cin Object Can be used to input more than one value: cout << "Enter the height and width: "; cin >> height >> width; Multiple values from keyboard must be separated by spaces Order is important: first value entered goes to first variable, etc. Use the correct data type Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 37 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 38
3 Reading Strings with cin Reading Strings with getline Can be used to read in an input and store it as a string Must first define a string cin is mainly used to read numeric data and can only read one word string getline function can read any string: string name; cout << "What is your name? "; cin >> name; string name; cout << "Enter your full name:\n"; getline(cin, name); need to #include <string> Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 39 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 310 Mathematical Expressions 3.2 Mathematical Expressions Various arithmetic calculations can be performed by creating complex expressions using multiple mathematical operators An expression can be a literal, a variable, or a mathematical combination of literals, variables and mathematical operators Examples: 4 x 4*x + 3*y 2*x*y Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 312
4 Mathematical Expressions Order of Operations Can be used in assignment (=), cout, and other statements Ex. Or: circumference = 2 * radius * PI; perimeter = 2*(l+w); cout << "perimeter is: " << perimeter; cout << "perimeter is: " << 2*(l+w); (No need for the assignment statement beforehand) Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 313 In an expression with more than one operator, evaluate in this order:  (unary negation) (highest) * / % +  (lowest) In the expression * 2 2 evaluate second evaluate first evaluate third Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 314 Order of Operations Associativity of Operators  (unary negation) associates right to left *, /, %, +,  associate left to right parentheses ( ) can be used to override the order of operations: * 2 2 = 4 (2 + 2) * 2 2 = * (2 2) = 2 (2 + 2) * (2 2) = 0 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 315 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 316
5 Grouping with Parentheses Algebraic Expressions Multiplication requires an operator: Area=lw is written as Area = l * w; Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 317 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 318 Algebraic Expressions Algebraic Expressions Parentheses may be needed to maintain order of operations: y 2 y m = 1 x 2 x 1 3x a = 4 y m = (y2y1)/(x2x1); m = y2y1/x2x1; //wrong a = (3*x+2)/(4*y1); There is no exponentiation operator: area=s 2 is written as area = pow(s, 2); or area = s*s; area = s^2; // wrong!!! pow() a C++ library function Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 319 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 320
6 When You Mix Apples and Oranges: Type Conversion 3.3 When You Mix Apples and Oranges: Type Conversion Operations are performed between operands of the same type If operands are not of the same type, C++ will convert one to the type of the other Data types are ranked. Lower types will be converted to higher types This can impact the results of calculations Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 322 Hierarchy of Types Type Coercion Highest: long double double float unsigned long long unsigned int Lowest: int One data type outranks another if it can hold a larger number (or it has larger memory size) Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 323 Type Coercion: automatic conversion of an operand to another data type implicit Type Casting: manual conversion of an operand to another data type (covered in 3.5) explicit Promotion: convert to a higher type Demotion: convert to a lower type Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 324
7 Coercion Rules Coercion Rules 1) char, short, unsigned short are automatically promoted to int if they are used in a math expression 2) When operating on values of different data types, the lower one is promoted to the type of the higher one ex: int years=5; double interestrate=0.05, total; total = years * interestrate; 3) When using the = operator, the type of the expression on right will be converted to the type of the variable on left ex: int x, y=4; double z=2.2, parts; x = y*z; //? 8 parts = 5/2; //? 2.0 parts = 5.0/2; //? 2.5 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 325 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 326 Overflow and Underflow 3.4 Overflow and Underflow Occurs when assigning a value that is too large (overflow) or too small (underflow) to be held in a variable Variable contains value that is wrapped around set of possible values Similar to mileage recording in an odometer Different systems may display a warning/error message, stop the program, or continue execution using the incorrect value Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 328
8 Overflow and Underflow // This program demonstrate data overflow & underflow #include <iostream> Using namespace std; void main() { short testvar = 32767; // max short = testvar = testvar + 1; cout << testvar << endl; testvar = testvar 1; } cout << testvar << endl; Program Output: Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley Type Casting Type Casting Used for manual data type conversion General format: static_cast<datatype>(value) Useful when C++ will not perform the required conversion automatically Useful for floating point division using ints: int x1,x2,y1,y2; double m; m = static_cast<double>(y2y1)/(x2x1); Useful to see int value of a char variable: char ch = 'C'; cout << ch << " is " << static_cast<int>(ch); Output: C is 67 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 331 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 332
9 Problems with Constants 3.6 Named Constants Problem 1: Meaning not clear e.g. newamount = balance * 0.02 Problem 2: What if the value changes frequently e.g. newamount = balance * 0.03 Both problems can be solved by using named constants Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 334 Named Constants Advantages of Named Constants Named constant (constant variable): variable whose content cannot be changed during program execution (readonly) Used for representing constant values with descriptive names: const double TAX_RATE = ; const int NUM_STATES = 50; Must be initialized when defined Often named in uppercase letters Make programs selfdocumenting newamount = balance * 0.02; can be changed to newamount = balance * INTEREST_RATE; Widespread changes made easier. Change the initialization value. Then recompile the program const double INTEREST_RATE = 0.3; Useful to define difficult to remember constants const double PI = ; Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 335 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 336
10 3.7 Multiple Assignment and Combined Assignment Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 337 Multiple Assignment and Combined Assignment The = can be used to assign a value to multiple variables: x = y = z = 5; Replace multiple assignment statements with one Associates right to left: x = (y = (z = 5)); Combined Assignment Look at the following statement: sum = sum + 1; This adds 1 to the variable sum. value value value is 5 is 5 is 5 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 339 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 340
11 Other Similar Statements Combined Assignment The combined assignment operators provide a shorthand for these types of statements. The statement sum = sum + 1; is equivalent to sum += 1; Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 341 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 342 Combined Assignment Operators 3.8 Formatting Output Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 343
12 Formatting Output The way a value is displayed on the screen is called its formatting The cout object has a standard way of formatting variables of each data type The size of a float is maximum 7 spaces including the decimal point Use number of spaces needed for each number Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 345 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 346 Stream Manipulators Stream Manipulators The cout object also provides ways to control the formatting through the use of manipulators Requires to include <iomanip> header file setw(x): specifies the field width for the value immediately following it to be at least x spaces wide. Use more spaces if field is not wide enough value = 15; cout << "[" << setw(5) << value << "]"; Output: [ 15] 3 spaces before 15, right adjusted setw() affects the value only once Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 347 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 348
13 Continued Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 349 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 350 Stream Manipulators Others affect values until changed again: fixed: use decimal notation for floatingpoint values (not scientific notation) setprecision(x): when used with fixed, print floatingpoint value using x digits after the decimal. Without fixed, print floatingpoint value using x significant digits showpoint: always print decimal point and trailing zeros for floatingpoint values left/right: left / right adjust the output Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 351 Continued Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 352
14 3.11 More Mathematical Library Functions 2 spaces Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 353 More Mathematical Library Functions Besides the pow function, the C++ runtime library provides numerous other functions Requires to include <cmath> header file Most take double as input, return a double Commonly used functions: sin y = sin(x); sine cos y = cos(x); cosine tan y = tan(x); tangent sqrt y = sqrt(x); square root log y = log(x); natural (e) log abs y = abs(x); absolute value (takes and returns an int) Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 355 Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 356
3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity
Chapter 3: Expressions and Interactivity 3.1 The cin Object Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, AddisonWesley Inc. Publishing as Pearson AddisonWesley
More information3.1. Chapter 3: The cin Object in Program 31. Displaying a Prompt 8/23/2014. The cin Object
Chapter 3: Expressions and Interactivity 3.1 The cin Object The cin Object The cin Object in Program 31 Standard input object Like cout, requires iostream file Used to read input from keyboard Information
More informationChapter 3: Expressions and Interactivity
Chapter 3: Expressions and Interactivity 3.1 The cin Object The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information retrieved from cin with >>
More informationChapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14
Chapter 3: Expressions and Interactivity 3.1 The cin Object The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information retrieved from cin with >>
More informationCopyright 2007 Pearson Education, Inc. Publishing as Pearson AddisonWesley
Slide 71 Copyright 2007 Pearson Education, Inc. Publishing as Pearson AddisonWesley Type Conversation / Casting Name Constant  const, #define X When You Mix Apples and Oranges: Type Conversion Operations
More informationWeek 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.16,3.910,5.
Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.16,3.910,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific
More informationCS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.
REVIEW cout Statement The cout statement invokes an output stream, which is a sequence of characters to be displayed to the screen. cout
More informationThe cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;
The cin Object Short for console input. It is used to read data typed at the keyboard. Must include the iostream library. When this instruction is executed, it waits for the user to type, it reads the
More informationExpressions, Input, Output and Data Type Conversions
L E S S O N S E T 3 Expressions, Input, Output and Data Type Conversions PURPOSE 1. To learn input and formatted output statements 2. To learn data type conversions (coercion and casting) 3. To work with
More informationEngineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input
More informationChapter 2. Outline. Simple C++ Programs
Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Devcpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.
More informationReview: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements
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
More informationIntroduction to C++ (Extensions to C)
Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.
More information! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char
Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated
More informationWindow s Visual Studio Output
1. Explain the output of the program below. Program char str1[11]; char str2[11]; cout > str1; Window s Visual Studio Output Enter a string: 123456789012345678901234567890 Enter
More informationInput and Expressions Chapter 3 Slides #4
Input and Expressions Chapter 3 Slides #4 Topics 1) How can we read data from the keyboard? 2) How can we calculate values? 3) How can we manage the type of a value? 4) How can we round or get random numbers?
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationCHAPTER 3 Expressions, Functions, Output
CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers  Number
More informationChapter 3. Numeric Types, Expressions, and Output
Chapter 3 Numeric Types, Expressions, and Output 1 Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and Explicit Type Conversion Calling a ValueReturning
More informationLesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions  Copyright Denis Hamelin  Ryerson University
Lesson #3 Variables, Operators, and Expressions Variables We already know the three main types of variables in C: int, char, and double. There is also the float type which is similar to double with only
More informationEngineering Problem Solving with C++, 3e Chapter 2 Test Bank
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. integer B 1.427E3 B. double D "Oct" C. character B 63.29 D. string F #Hashtag
More informationThe C++ Language. Arizona State University 1
The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationProgramming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1
300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard
More informationBITG 1233: Introduction to C++
BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token
More information1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.
Engineering Problem Solving With C++ 4th Edition Etter TEST BANK Full clear download (no error formating) at: https://testbankreal.com/download/engineeringproblemsolvingwithc4theditionettertestbank/
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library
More informationl A program is a set of instructions that the l It must be translated l Variable: portion of memory that stores a value char
Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fa 2018 Ji Seaman Programming A program is a set of instructions that the computer foows to perform a task It must be transated from
More informationC++ Programming Lecture 11 Functions Part I
C++ Programming Lecture 11 Functions Part I By Ghada AlMashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs
More informationChapter 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
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More information2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson AddisonWesley 21 Parts of a C++ Program Parts of a C++ Program // sample C++ program
More informationI/O Streams and Standard I/O Devices (cont d.)
Chapter 3: Input/Output Objectives In this chapter, you will: Learn what a stream is and examine input and output streams Explore how to read data from the standard input device Learn how to use predefined
More informationLESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long
LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be
More information3.1 The cin Object. Expressions & I/O. Console Input. Example program using cin. Unit 2. Sections 2.14, , 5.1, CS 1428 Spring 2018
Expressions & I/O Unit 2 Sections 2.14, 3.110, 5.1, 5.11 CS 1428 Spring 2018 Ji Seaman 1 3.1 The cin Object cin: short for consoe input a stream object: represents the contents of the screen that are
More informationObjectives. In this chapter, you will:
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 arithmetic expressions Learn about
More informationC++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh
C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,
More informationLECTURE 02 INTRODUCTION TO C++
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION
More informationChapter 2: Introduction to C++
Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson AddisonWesley Education, Inc. Publishing as Pearson AddisonWesley 2.1 Parts of a C++
More informationIntroduction to Engineering gii
25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB  Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.
More informationOperators and Expressions:
Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence
More informationANSI C Programming Simple Programs
ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double
More informationChapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, AddisonWesley Inc. Publishing as Pearson AddisonWesley
More informationPIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment
PIC 10A Lecture 3: More About Variables, Arithmetic, Casting, Assignment Assigning values to variables Our variables last time did not seem very variable. They always had the same value! Variables stores
More informationChapter 3  Functions
Chapter 3  Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
More informationChapter 1. C++ Basics. Copyright 2010 Pearson AddisonWesley. All rights reserved
Chapter 1 C++ Basics Copyright 2010 Pearson AddisonWesley. All rights reserved Learning Objectives Introduction to C++ Origins, ObjectOriented Programming, Terms Variables, Expressions, and Assignment
More informationChapter 4: Basic C Operators
Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional
More informationOperators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP
Operators and Expression Dr Muhamad Zaini Yunos JKBR, FKMP Arithmetic operators Unary operators Relational operators Logical operators Assignment operators Conditional operators Comma operators Operators
More information1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary
Topic 2 1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary Arithmetic Operators C++ has the same arithmetic operators as a calculator:
More informationCSc 10200! Introduction to Computing. Lecture 45 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 45 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 3 Assignment, Formatting, and Interactive Input
More informationc++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.
Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object
More informationA First Program  Greeting.cpp
C++ Basics A First Program  Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout
More informationUNIT 3 Introduction to C++
UNIT 3 Introduction to C++ C++ Character Sets: Letters AZ, az Digits 09 Special Symbols Space +  * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More information61 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x
(Function) 1.1 Math Library Function!"#! $%&!'(#) preprocessor directive #include !*+!"#!, Function Description Example sqrt(x) square root of x sqrt(900.0) is 30.0 sqrt(9.0) is 3.0 exp(x) log(x)
More informationDefinition Matching (10 Points)
Name SOLUTION Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. There are no syntax errors
More informationChapter Two: Fundamental Data Types
Chapter Two: Fundamental Data Types Slides by Evan Gallagher Chapter Goals To be able to define and initialize variables and constants To understand the properties and limitations of integer and floatingpoint
More informationFunctions and Recursion
Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game
More informationSummary of basic C++commands
Summary of basic C++commands K. VollmayrLee, O. Ippisch April 13, 2010 1 Compiling To compile a C++program, you can use either g++ or c++. g++ o executable_filename.out sourcefilename.cc c++ o executable_filename.out
More informationModule 2  Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1
Module 2  Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions
More informationOperators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur
Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators
More informationExpressions & interactions Conditionals Solving a problem. binary representation (8 bits)
Expressions & interactions Conditionals Solving a problem binary representation (8 bits) positives: first bit=0 0 = 00000000 1 = 00000001 2 = 00000010 3 = 00000011 4 = 00000100... 127 = 01111111 negatives:
More informationOperators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface
Introduction Syntax Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data Introduction Map Layer Mathematics Algebraic statements are used to perform the basic mathematical operations
More informationIntroduction to Programming EC105. Lecture 2
Introduction to Programming EC105 Lecture 2 Input and Output A data stream is a sequence of data  Typically in the form of characters or numbers An input stream is data for the program to use  Typically
More informationString Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting
String Variables and Output/Input Adding Strings and Literals to Your Programming Skills and output/input formatting A group of characters put together to create text is called a string. Strings are one
More informationWARM UP LESSONS BARE BASICS
WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions
More informationWeek 3: File I/O and Formatting 3.7 Formatting Output
Week 3: File I/O and Formatting 3.7 Formatting Output Formatting: the way a value is printed: Gaddis: 3.7, 3.8, 5.11 CS 1428 Fall 2014 Jill Seaman spacing decimal points, fractional values, number of digits
More information2.1First.cpp #include <iostream> // contains information to support input / output using namespace std;
Lafore: ObjectOriented Programming in C++ (4 th ) 2.1First.cpp // contains information to support input / output // entry point for every C and C++ program (console) cout
More informationFundamentals of Programming CS110. Lecture 2
Fundamentals of Programming CS110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout
More informationLecture 4 Tao Wang 1
Lecture 4 Tao Wang 1 Objectives In this chapter, you will learn about: Assignment operations Formatting numbers for program output Using mathematical library functions Symbolic constants Common programming
More informationBy 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
1 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 variables. Apply C++ syntax rules to declare variables, initialize
More informationCSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial
CSI31 Lecture 5 Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial 1 3.1 Numberic Data Types When computers were first developed, they were seen primarily as
More informationProgramming. C++ Basics
Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++
More informationVARIABLES & ASSIGNMENTS
Fall 2018 CS150  Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150  Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need
More information1 Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1 Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationOperations. Making Things Happen
Operations Making Things Happen Object Review and Continue Lecture 1 2 Object Categories There are three kinds of objects: Literals: unnamed objects having a value (0, 3, 2.5, 2.998e8, 'A', "Hello\n",...)
More information엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED
엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline  Function Definitions  Function Prototypes  Data
More informationNumerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University
Numerical Data CS 180 Sunil Prabhakar Department of Computer Science Purdue University Problem Write a program to compute the area and perimeter of a circle given its radius. Requires that we perform operations
More informationFundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University
Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler
More informationSoftware Design & Programming I
Software Design & Programming I Starting Out with C++ (From Control Structures through Objects) 7th Edition Written by: Tony Gaddis Pearson  Addison Wesley ISBN: 139780132576253 Chapter 3 Introduction
More informationThis watermark does not appear in the registered version  Slide 1
Slide 1 Chapter 1 C++ Basics Slide 2 Learning Objectives Introduction to C++ Origins, ObjectOriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output Program Style
More informationIntroduction to Programming
Introduction to Programming Department of Computer Science and Information Systems Tingting Han (afternoon), Steve Maybank (evening) tingting@dcs.bbk.ac.uk sjmaybank@dcs.bbk.ac.uk Autumn 2017 Week 4: More
More informationC++, How to Program. Spring 2016 CISC1600 Yanjun Li 1
Chapter 6 Function C++, How to Program Deitel & Deitel Spring 2016 CISC1600 Yanjun Li 1 Function A function is a collection of statements that performs a specific task  a single, welldefined task. Divide
More informationPART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.
Unit  III CHAPTER  9 INTRODUCTION TO C++ Choose the correct answer. PART I 1. Who developed C++? (a) Charles Babbage (b) Bjarne Stroustrup (c) Bill Gates (d) Sundar Pichai 2. What was the original name
More informationIntroduction. What is function? Multiple functions form a larger program Modular programming
FUNCTION CSC128 Introduction What is function? Module/mini program/subprogram Each function/module/subprogram performs specific task May contains its own variables/statements Can be compiled/tested independently
More informationChapter 2 Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion
More information1.3b Type Conversion
1.3b Type Conversion Type Conversion When we write expressions involved data that involves two different data types, such as multiplying an integer and floating point number, we need to perform a type
More informationProgramming Language. Functions. Eng. Anis Nazer First Semester
Programming Language Functions Eng. Anis Nazer First Semester 20162017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities
More informationOn a 64bit CPU. Size/Range vary by CPU model and Word size.
On a 64bit CPU. Size/Range vary by CPU model and Word size. unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed There are (usually) no unsigned floats or doubles.
More informationCS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS
CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS LOGISTICS Homework due tonight and Thursday Should be able to do after reading textbook  go to TA/CP office hours Do the lab Go to lab
More information! A literal represents a constant value used in a. ! Numbers: 0, 34, , 1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.
Week 1: Introduction to C++ Gaddis: Chapter 2 (excluding 2.1, 2.11, 2.14) CS 1428 Fall 2014 Jill Seaman Literals A literal represents a constant value used in a program statement. Numbers: 0, 34, 3.14159,
More informationComputing and Statistical Data Analysis Lecture 3
Computing and Statistical Data Analysis Lecture 3 Type casting: static_cast, etc. Basic mathematical functions More i/o: formatting tricks Scope, namspaces Functions 1 Type casting Often we need to interpret
More informationCS242 COMPUTER PROGRAMMING
CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Builtin Data Types o o o o bool Data Type char Data Type int Data Type FloatingPoint Data Types Variable Declaration Initializing
More informationReview for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure
Computer Systems Computer System Computer Structure C++ Environment Imperative vs. objectoriented programming in C++ Input / Output Primitive data types Software Banking System Compiler Music Player Text
More informationThe C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.
The C++ Language Input and Output Output! Output is information generated by a program.! Frequently sent the screen or a file.! An output stream is used to send information. Another type supplied by C++
More information9 Using Equation Networks
9 Using Equation Networks In this chapter Introduction to Equation Networks 244 Equation format 247 Using register address lists 254 Setting up an enable contact 255 Equations displayed within the Network
More informationCSCI 1061U Programming Workshop 2. C++ Basics
CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, ObjectOriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total  100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 1 Monday, February 13, 2017 Total  100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCHAPTER 3 BASIC INSTRUCTION OF C++
CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers
More informationChapter 2: Overview of C++
Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman C++ Background Introduced by Bjarne Stroustrup of AT&T s Bell Laboratories in
More information