Fundamentals of Programming CS-110. Lecture 2

Similar documents
Introduction to Programming EC-105. Lecture 2

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

Chapter 2. C++ Basics

Values, Variables, Types & Arithmetic Expressions. Agenda

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

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

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

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

CS242 COMPUTER PROGRAMMING

The C++ Language. Arizona State University 1

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

Getting started with C++ (Part 2)

Chapter 2: Overview of C++

Computer Programming : C++

LECTURE 02 INTRODUCTION TO C++

BITG 1233: Introduction to C++

A First Program - Greeting.cpp

2 nd Week Lecture Notes

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

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Chapter 2: Introduction to C++

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

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

VARIABLES & ASSIGNMENTS

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

WARM UP LESSONS BARE BASICS

CHAPTER 3 BASIC INSTRUCTION OF C++

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

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-

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

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

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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

UNIT- 3 Introduction to C++

Creating a C++ Program

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

Chapter 2 Basic Elements of C++

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

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

Introduction to the C++ Programming Language

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Full file at

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

Objectives. In this chapter, you will:

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

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

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

CSCE 110 PROGRAMMING FUNDAMENTALS

CS313D: ADVANCED PROGRAMMING LANGUAGE

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.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Programming. C++ Basics

Chapter 1 INTRODUCTION

IT 1033: Fundamentals of Programming Data types & variables

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

BTE2313. Chapter 2: Introduction to C++ Programming

Lecture 2 Tao Wang 1

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

Expressions, Input, Output and Data Type Conversions

6.096 Introduction to C++ January (IAP) 2009

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

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CSCI 1061U Programming Workshop 2. C++ Basics

Chapter 2 C++ Fundamentals

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

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

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

Introduction to Programming using C++

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

Introduction to C++ (Extensions to C)

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

Full file at

Basic data types. Building blocks of computation

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

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

Programming with C++ Language

CHAPTER 3 Expressions, Functions, Output

CSCI 123 Introduction to Programming Concepts in C++

Engineering Problem Solving with C++, Etter/Ingber

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

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

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

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Input And Output of C++

CSc Introduction to Computing

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Programming with C++ as a Second Language

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Transcription:

Fundamentals of Programming CS-110 Lecture 2

Last Lab // Example program #include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; cout << "I am a master programmer."; } Run it on cpp.sh

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 originates at the keyboard at a file An output stream is the program s output - Destination is typically the monitor a file

Output using cout cout is an output stream sending data to the monitor The insertion operator "<<" inserts data into cout Example: cout << number_of_bars << " candy bars\n"; - This line sends two items to the monitor The value of number_of_bars The quoted string of characters " candy bars\n" - Notice the space before the c in candy - The \n causes a new line to be started following the s in bars Slide 2-35 A new insertion operator is used for each item of output

Examples Using cout This produces the same result as the previous sample cout << number_of_bars ; cout << " candy bars\n"; Here arithmetic is performed in the cout statement cout << "Total cost is $" << (price + tax); Quoted strings are enclosed in double quotes ("Walter") - Don t use two single quotes (') A blank space can also be inserted with cout << " " ; if there are no strings in which a space is desired as in " candy bars\n"

Include Directives Include Directives add library files to our programs - To make the definitions of the cin and cout available to the program: #include <iostream> Using Directives include a collection of defined names - To make the names cin and cout available to our program: using namespace std;

Receiving Information from the user We can receive input from the user by using the cin (Standard input stream Example int cin>>ftemp; ftemp; causes the program to wait for the user to type in a number. The resulting number is placed in the variable ftemp. The keyword cin (pronounced C in ) is an object, predefined in C++ to correspond to the standard input stream. This stream represents data coming from the keyboard The >> is the extraction or get from operator. It takes the value from the stream object on its left and places it in the variable on its right.

Reading Data From cin Multiple data items are separated by spaces Data is not read until the enter key is pressed - Allows user to make corrections Example: cin >> v1 >> v2 >> v3; - Requires three space separated values - User might type 34 45 12 <enter key>

Designing Input and Output Prompt the user for input that is desired - cout statements provide instructions cout << "Enter your age: "; cin >> age; Notice the absence of a new line before using cin Echo the input by displaying what was read - Gives the user a chance to verify data cout << age << " was entered." << endl;

Simplistic View of a Computer

Very Simplistic View of a Computer CPU Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Each location is 1 byte of memory 1 byte = 8 bits Each bit is an electric impulse carrying 1 or 0. This simplistic view is enough to explain the basic concepts of programming to students 5

Value The only task a computer can do is arithmetic e.g. multiplying, dividing, subtracting, etc. Therefore, everything in the computer is represented as a value - Numbers, letters, characters, etc are all represented as values Values could change depending on their nature. For example - the temperature today is different from the temperature yesterday - The number of cars inside Lahore is different then the number of in cars Islamabad.

Identifiers C++ programs can be written using many English words It is useful to think of words found in a program as being one of three types: Reserved Words: words such as if, int and else, predefined, their meanings can not be changed Library Identifiers: These words are supplied default meanings by the programming environment cin, cout and sqrt Programmer-supplied Identifiers: words "created" by the programmer, called variables

Variable To store a value inside a computer a variable is used. A variable is a space in the memory to store a value. This space is reserved until the variable is required. Session 2 7

What Makes a Variable Variable has three important characteristics: - Type How much memory do a variable need. - This information is determined by a type. - Name How to differentiate a variable with another variable of the same type. - Name refers to the memory location assigned to this variable. - Value What is the value? - The actual value contained by a variable. Session 2 8

An Example of a Variable Type of the variable is integer (written as int in C++) int temperature = 35 A name of the variable An initial value of the variable Session 2 9

int temperature = 35 Example of a Variable (Memory View) 00000000 Location 0 Locations 0-3 are collectively 00000000 Location 1 called as temperature 00000000 Location 2 00100011 Location 3 Location 4 100011 is the binary equivalent of 35 Location 5 Session 2 10

Changing the Value of Variable Lets change the value of temperature. temperature = 45902 00000000 Locations 0-3 are collectively 00000000 called as temperature 10110011 01001110 1011001101001110 is the binary equivalent of 45902 Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Session 2 11

Type of a Variable Among other advantages a type binds the memory to a variable name. The type int is of 4 bytes in C++. Therefore, it can hold maximum of 2,147,483,647 value. It can also hold values in negative down to -2,147,483,648. Session 2 12

Initializing Variables Declaring a variable does not give it a value - Giving a variable its first value is initializing the variable Variables are initialized in assignment statements double mpg; // declare the variable mpg = 26.3; // initialize the variable Declaration and initialization can be combined using two methods - Method 1 double mpg = 26.3, area = 0.0, volume; - Method 2 double mpg(26.3), area(0.0), volume;

Variable for Real Numbers int cannot hold a real value. Therefore, a type double is used to hold real values. Double takes 8 bytes of memory instead of 4 bytes of a double. Out of the 8 bytes in a double 4 bytes are used to hold the value before the decimal point and 4 bytes for the value after the decimal point. ession 2 14

Relative Comparison of int and double int numpeople = 2; Reserves 32 bits (4 bytes) and sets the value stored in that space to 2. The name numpeople is associated with this space. double bill = 32.45; Reserves 64 bits (8 bytes) and sets the value stored in that space to 32.45. The name bill is associated with this space. Session 2 15

Session 2 16

Characters Char Character constants of type "char" must be enclosed in single quotation marks Char mychar = A ; Sample Code

Manipulating Variables Assignment Statement - In Mathematics the value x = x + 1 is not possible why? - In C++ x = x +1 is possible because = is an assignment operator and not an equality operator. - Assignment operator means that the contents of the right hand side is transferred to the memory location of the left hand side. Sessio 2 17

Assignment Statement x = 5671 5671 is written at the memory location reserved for x Session 2 18

Constants Constants are values which cannot be modified e.g. the value of Pi To declare a constant in C++, we write a keyword const before the variable type. const double pi = 3.14; Session 2 19

Reserved Words Some names cannot be declared as variable names because they are reserved words in C++ 20

Variables You can store your program (algorithm) data into variables (also called memory locations). In C++ language there are various types of variable available.

Variable types Variable type Keyword used in declaration Size in bits Range integer int 32 bits -2147483648 to 2147483647 Short integer short int 16 bits -32768 to 32767 Long integer long int 32 bits -2147483648 to 2147483647 Floating point data Floating point data (with large fraction) float 32 bits -1.0x10 38 to 1.0x10 38 double 64 bits -1.0x10 308 to 1.0x10 308 Text type data char 8 bits -128 to 127 Boolean data (True or False) bool 1 bit 1 or 0

Variable Types To use a variable in our code First, we must have to declare it, variable are known with its keywords. Use the name of keyword for declaration of a variable. Example: int number1 = 10; Example: float floatdata = 20.93; Example: long int mydata = -10; Example: char textdata = A ; Example: bool booldata = true; #include <iostream> using namespace std; void main () { int number1 = 10; float floatdata = 20.93; } #include <iostream> using namespace std; void main () { int number1 = 20, number2 = 10; }

Points to remeber in naming the variables The names given to variables (and other program features) are called identifiers. What are the rules for writing identifiers? You can use upper- and lowercase letters, and the digits from 1 to 9. You can also use the underscore (_). The first character must be a letter or underscore. (it cannot be a digit No spaces allowed in a variable name

Primitive Data Types So far the variable types that we have studied are primitive data types. Primitive data types only have a memory space for storing values. 27

Type char Computers process character data too char - Short for character - Can be any single character from the keyboard To declare a variable of type char: char letter;

char constants Character constants are enclosed in single quotes char letter = 'a'; Strings of characters, even if only one character is enclosed in double quotes - "a" is a string of characters containing one character - 'a' is a value of type character

Reading Character Data cin skips blanks and line breaks looking for data The following reads two characters but skips any space that might be between char symbol1, symbol2; cin >> symbol1 >> symbol2; User normally separate data items by spaces J D Results are the same if the data is not separated by spaces JD

Type bool bool is a new addition to C++ - Short for boolean - Boolean values are either true or false To declare a variable of type bool: bool old_enough;

Manipulator Manipulators are instructions to the output stream that modify the output in various ways -endl is a manipulator that causes a linefeed to be inserted into the stream -cout<< Sample text <<endl; -setw is another manipulator that sets the field width of the output

Casting Converting a value of one type into another type Manual Casting static_cast<double> (intvar) Automatic Casting Automatic conversion of lower order type to higher order type when two operands of different types are encountered in the same expression