Another Simple Program: Adding Two Integers

Similar documents
by Pearson Education, Inc. All Rights Reserved. 2

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

Introduction to Programming

Chapter 1 Introduction to Computers and C++ Programming

CS242 COMPUTER PROGRAMMING

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

download instant at Introduction to C++

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Lecture 3 Tao Wang 1

Introductionto C++ Programming

download instant at Introduction to C++

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

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++

1. In C++, reserved words are the same as predefined identifiers. a. True

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

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 2: Overview of C++

Engineering Problem Solving with C++, Etter/Ingber

Computer Programming : C++

Programming for Engineers Introduction to C

VARIABLES & ASSIGNMENTS

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

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

Full file at C How to Program, 6/e Multiple Choice Test Bank

BITG 1233: Introduction to C++

IT 374 C# and Applications/ IT695 C# Data Structures

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

Object Oriented Design

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

Introduction to Programming

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

C: How to Program. Week /Mar/05

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

Objectives. In this chapter, you will:

Cours de C++ Introduction

2.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++

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

Chapter 2 - Introduction to C Programming

Main Memory Organization

Chapter 2. Outline. Simple C++ Programs

CHAPTER 3 BASIC INSTRUCTION OF C++

2 nd Week Lecture Notes

Chapter 2 Basic Elements of C++

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

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

LECTURE 02 INTRODUCTION TO C++

Fundamentals of Programming CS-110. Lecture 2

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

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

The C++ Language. Arizona State University 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2, Part III Arithmetic Operators and Decision Making

CSCE 110 PROGRAMMING FUNDAMENTALS

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

In this chapter you will learn:

Basics of Programming

A First Program - Greeting.cpp

Introduction to Programming EC-105. Lecture 2

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

Chapter 4 Selection Structures: Making Decisions PRELUDE TO PROGRAMMING, 6TH EDITION BY ELIZABETH DRAKE

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Programming with C++ as a Second Language

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

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.

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

Chapter 15 - C++ As A "Better C"

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

BASIC ELEMENTS OF A COMPUTER PROGRAM

Why Is Repetition Needed?

CSCI 1061U Programming Workshop 2. C++ Basics

Introduction to C++ Systems Programming

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

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

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

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

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

Introduction to Programming using C++

Creating a C++ Program

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Fundamentals of Programming Session 4

Programming. C++ Basics

IS 0020 Program Design and Software Tools

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

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

Chapter 2. C++ Basics

Differentiate Between Keywords and Identifiers

Introduction to C# Applications

Introduction to C++ (Extensions to C)

Transcription:

Another Simple Program: Adding Two Integers

Another Simple Program: Adding Two Integers This program uses the input stream object std::cin and the stream extrac<on operator, >>, to obtain two integers typed by a user at the keyboard, computes the sum of these values and outputs the result using std::cout. Note: << is called inser<on operator

Another Simple Program: Adding Two Variable declara0on: Integers number1, number2, sum are the names of variables A variable is a loca<on in the computer s memory where a value can be stored for use by a program. These declara<ons specify that the variables number1, number2, and sum are data of type int, meaning that these variables will hold integer values, i.e., whole numbers such as 7, 11, 0 and 31,914. All variables must be declared with a name and a data type before they can be used in a program. Several variables of the same type may be declared in one declara<on or in mul<ple declara<ons. We could have declared all three variables in one declara<on by using a comma-separated list as follows: int number1, number2, sum;!

Another Simple Program: Adding Two Integers Variable s name A series of characters consis<ng of leqers, digits and underscores ( _ ). Does NOT begin with a number. C++ is case sensi<ve i.e., uppercase and lowercase leqers are different, so a1 and A1 represent different variables. Must NOT be a special character e.g., &, %, $, Must NOT be a keyword. (What is a keyword?)

Another Simple Program: Adding Two Integers Variable s types We ll soon discuss the type float for specifying decimal numbers, and the type char for specifying character data. Decimal numbers are numbers with decimal points, such as 3.4, 0.0 and 11.19. A char variable may hold only a single lowercase leqer, a single uppercase leqer, a single digit or a single special character (e.g., $ or *). Types such as int, float, and char are called fundamental types. Fundamental-type names are keywords.

Another Simple Program: Adding Two Integers Placement of Variable Declara0ons Declara<ons of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program.

Another Simple Program: Adding Two Integers Obtaining the First Value from the User displays Enter first integer: followed by a space. This message is called a prompt because it directs the user to take a specific ac<on. uses the standard input stream object cin (of namespace std) and the stream extrac<on operator, >>, to obtain a value from the keyboard. Using the stream extrac<on operator with std::cin takes character input from the standard input stream, which is usually the keyboard.

Another Simple Program: Adding Two Integers Obtaining the Second Value from the User

Another Simple Program: Adding Two Integers Calcula0ng the Sum of the Values Input by the User This assignment statement adds the values of variables number1 and number2 and assigns the result to variable sum using the assignment operator =

Another Simple Program: Adding Two Displaying the Result Integers displays the character string Sum is followed by the numerical value of variable sum followed by std::endl a so-called stream manipulator. The name endl is an abbrevia<on for end line and belongs to namespace std. The std::endl stream manipulator outputs a newline.

Memory Concepts Variable names such as number1, number2 and sum actually correspond to loca<ons in the computer s memory. Every variable has a name, a type, a size and a value.

Arithme<c C++ provides the modulus operator, %, that yields the remainder amer integer division. The modulus operator can be used only with integer operands. The expression x % y yields the remainder amer x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.

Examples, Arithme<c

Arithme<c Rules of Operator Precedence C++ applies the operators in arithme<c expressions in a precise order determined by these rules of operator precedence, which are generally the same as those in algebra: 1. Operators in expressions contained with in pairs of parentheses are evaluated first. Parentheses are said to be at the highest level of precedence. In cases of nested, or embedded, parentheses, such as (a*(b+c)), the operators in the innermost pair of parentheses are applied first. 2. Mul0plica0on, division, and modulus opera<ons are applied next. If an expression contains several mul<plica<on, division and modulus opera<ons, operators are applied from lem to right. 3. Addi0on and subtrac0on opera<ons are applied last. If an expression contains several addi<on and subtrac<on opera<ons, operators are applied from lem to right.

Examples Arithme<c 1: 2 + (2-2) * (2 + 2) / 2 =? 2: 3 + 3-3 * (3 / 3) =? 3: (4 + 4 4 * 4) / ( (4 + 4) * -1) =? 4: (22 % 4 1) / 2 + 1 / 2 =?

Modify the Program Write a program that takes three integers from the user and outputs the following opera<on:

Decision making (Equality and Rela<onal Operators) The if statement allows a program to take alterna<ve ac<on based on whether a condi<on is true or false. If the condi<on is true, the statement in the body of the if statement is executed. If the condi<on is false, the body statement is not executed. Condi<ons in if statements can be formed by using the equality operators and rela<onal operators.

Decision making (Equality and If statement Rela<onal Operators) if (condi<on){ } //do something

Decision making (Equality and Rela<onal Operators)

Decision making (Equality and Rela<onal Operators) Let s write a program that compares two integers!

Decision making (Equality and Rela<onal Operators) using namesapce std! is a direc<ve that enables a program to use all names in any standard C++ header such as <iostream>

Lab Assignment #1