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

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

LECTURE 02 INTRODUCTION TO C++

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

The C++ Language. Arizona State University 1

Chapter 2: Introduction to C++

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

UNIT- 3 Introduction to C++

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

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

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.

CHAPTER 3 BASIC INSTRUCTION OF C++

A First Program - Greeting.cpp

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

2 nd Week Lecture Notes

BITG 1233: Introduction to C++

Introduction to C++ (Extensions to C)

Objectives. In this chapter, you will:

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Week 3: File I/O and Formatting 3.7 Formatting Output

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

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

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

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

CSI33 Data Structures

Computer Programming : C++

Fundamentals of Programming CS-110. Lecture 2

Chapter 2: Overview of C++

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

CS242 COMPUTER PROGRAMMING

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

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

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

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

Chapter 2. C++ Basics

CHAPTER 3 Expressions, Functions, Output

Programming. C++ Basics

CSCE 110 PROGRAMMING FUNDAMENTALS

WARM UP LESSONS BARE BASICS

Introduction to Programming EC-105. Lecture 2

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

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Chapter 2 C++ Fundamentals

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

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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.

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Visual C# Instructor s Manual Table of Contents

Chapter 2 Basic Elements of C++

VARIABLES & ASSIGNMENTS

Expressions, Input, Output and Data Type Conversions

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

Chapter 2: Using Data

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 3. Numeric Types, Expressions, and Output

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

This watermark does not appear in the registered version - Slide 1

Chapter 3 - Notes Input/Output

Programming with C++ as a Second Language

Introduction to Programming

Engineering Problem Solving with C++, Etter/Ingber

Lecture 3 Tao Wang 1

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Reserved Words and Identifiers

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

Getting started with C++ (Part 2)

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

COMP 202 Java in one week

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

Your First C++ Program. September 1, 2010

Chapter 2. Outline. Simple C++ Programs

LECTURE 3 C++ Basics Part 2

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

CSCI 1061U Programming Workshop 2. C++ Basics

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Section we will not cover section 2.11 feel free to read it on your own

Add Subtract Multiply Divide

Ch 6. Functions. Example: function calls function

Outline. Data and Operations. Data Types. Integral Types

Computer Science II Lecture 1 Introduction and Background

C++ Programming Basics

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Full file at

Introduction to Programming using C++

Chapter 4 - Notes Control Structures I (Selection)

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Transcription:

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 from a programming language (C++) to machine code in order to run on the machine. 1 2 Structure of a C++ Program Hello world: In general: //This program outputs a message to the screen #include <iostream> using namespace std; int main() { cout << Hello world << endl; } //This is a comment #include <includefile>... using namespace std; int main() { statements... } Variables, Data Types Variable: portion of memory that stores a value Identifier: name of a program element Fundamental data types short int long float double long double Variable Declaration statement datatype identifier; Variable Initialization statement: datatype identifier = constant; bool char float hours; int count = 0; 3 4

Integer types Integers are whole numbers such as 12, 7, and -99 Data Type Range Floating-point types (and bool) Floating point types store real numbers such as 12.45 and -3.8 They are stored using scientific notation. short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 long -2,147,483,648 to 2,147,483,647 char type stores characters such as A, @, and 9 The ascii code value (an integer) of the character is stored in memory. 5 Data Type Range float ±3.4E-38 to ±3.4E38 double ±1.7E-308 to ±1.7E308 long double ±1.7E-308 to ±1.7E308 bool type stores values that are true or false false is 0, true is 1. 6 Constants Literals (specific value of a given type) 1 75-2 12.45-3.8 6.25e-5 true false A 2 Named Constants: variable whose value cannot be changed const datatype identifier = constant; const double TAX_RATE = 0.0675; 7 Assignment statement, expressions To change the value of a variable: The lefthand side must be a variable The righthand side is an expression of the right type What is an expression? variable = expression; count = 10; an expression has a type and evaluates to a value literal named constant variable arithmetic expression etc. 8

Arithmetic Operations arithmetic operators: + addition - subtraction * multiplication / division % modulo (remainder) Integer division: x + 10 7 * 2 8-5 * 10 (3 * 10) / 2 14 3 = 4 r. 2 (because 4*3+2 = 14) 14/3 => 4 in C++ 14%3 => 2 in C++ 14.0/3.0 => 4.6666667 in C++ 9 Operator precedence In an expression with multiple operators, which one happens first? Use this order for different operators: + - (unary) * / % + - (binary) < > <= >= == = && We will study relational and logical operators next week. Use this order for multiple occurrences of the same operator - (unary negation) associates right to left *, /, %, +, - associate left to right 10 Output (cout and <<) Basic Input/Output Formatting output Goal: control how output displays for numeric data sends data to the screen (console) cout << expression; cout << expr1 << expr2; cout << hello ; cout << Count is: << count << endl; Input (cin and >>) receives data typed in from the keyboard (stops at space) cin >> variable; cin >> var1 >> var2; right hand side must be a variable cout << Enter the height and width: ; cin >> height >> width; cout << The height is << height << endl; 11 these require #include<iomanip> setw(x): print next value in a field at least x spaces wide (right justified, padded with spaces). cout << setw(6) << 1234 << setw(6) << 5 << endl; cout << setw(6) << 5 << setw(6) << 1234 << endl; fixed: always use decimal notation (not scientific) setprecision(x): when used with fixed, print floating point values using x digits after the decimal cout << fixed << setprecision(2); cout << 3.14159 << endl; float x = 20; cout << x << endl; 3.14 20.00 1234 5 5 1234 12

The string class string literals: represent sequences of chars, inside of double quotes: To define string variables: string firstname, lastname; Operations include: - = for assignment -.size() function for length cout << Hello ; string name; name = George ; cout << name.size() << ; cout << name[2] << endl; - [n] to access one character in the nth position. 13 Implicit - assignment: - binary operations: Explicit Type conversions int x; double d = 3.1415; x = d; cout << x << endl; int x = 10; double d = 2.3; cout << x + d << endl; int x, y;... float avg = static_cast<float>(x)/y; or float avg = x/(float)y; //c-style notation the type of expression on the right will be converted to type of variable on left, possibly losing information. the operand with the lower ranking type is converted to the type of the other. Order of types: long double double float long int char 14 Comments Single-Line Comments // this text is ignored, to end of line Multi-Line Comments /* Anything occurring between a slash star and a star slash is ignored. Even when spanning multiple lines. */ Use comments to explain your code to a human reader who knows C++. Programming Style The visual organization of the source code Purpose: improve the readability of the source code Includes the use of spaces, tabs, and blank lines Includes naming of variables, constants. Includes where to use comments. Common elements to improve readability: Braces { } aligned vertically Indentation of statements within a set of braces Lines shorter than 80 characters. 16

Sample Problem Write a program that converts Celsius temperatures to Fahrenheit temperatures. The formula is F = 9 C + 32 5 where F is the Fahrenheit temperature, and C is the Celsius temperature. Input the Celsius temperature from the user and output the temperature in Fahrenheit. 17