Getting started with C++ (Part 2)

Similar documents
BITG 1233: 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

Fundamentals of Programming CS-110. Lecture 2

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

Chapter 2: Introduction to C++

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

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

Introduction to Programming EC-105. Lecture 2

Maciej Sobieraj. Lecture 1

CHAPTER 3 BASIC INSTRUCTION OF C++

LECTURE 02 INTRODUCTION TO C++

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

Introductory Lecture. CS319: Scientific Computing (with C++) Week 1: 11 Jan, Lecturer: Niall Madden.

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

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

CS242 COMPUTER PROGRAMMING

WARM UP LESSONS BARE BASICS

The C++ Language. Arizona State University 1

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

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

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

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

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

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

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

CSc Introduction to Computing

I/O Streams and Standard I/O Devices (cont d.)

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

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

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

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

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Lecture 2 Tao Wang 1

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

Introduction to the C++ Programming Language

Computer Programming : C++

Objects and streams and files CS427: Elements of Software Engineering

BTE2313. Chapter 2: Introduction to C++ Programming

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.

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

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Programming with C++ as a Second Language

2 nd Week Lecture Notes

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS3157: Advanced Programming. Outline

A First Program - Greeting.cpp

Program Organization and Comments

Ch 6. Functions. Example: function calls function

Chapter 1 INTRODUCTION

UNIT- 3 Introduction to C++

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

Introduction to C++ (Extensions to C)

Objectives. In this chapter, you will:

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

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

Understanding main() function Input/Output Streams

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

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-

Programming with C++ Language

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

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

Chapter 2 Basic Elements of C++

Programming. C++ Basics

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Review of Important Topics in CS1600. Functions Arrays C-strings

Creating a C++ Program

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

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

UEE1302 (1102) F10: Introduction to Computers and Programming

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

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

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

Engineering Problem Solving with C++, Etter/Ingber

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.

Lab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Chapter 2: Overview of C++

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

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)

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

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

C++ Input/Output: Streams

Chapter 3 - Notes Input/Output

Fundamental of Programming (C)

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Expressions, Input, Output and Data Type Conversions

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

EEE145 Computer Programming

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

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

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Transcription:

Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22

Outline 1 Recall from last week... 2 Recall: Output 3 Recall: Variables 4 Output Manipulators endl 5 Input 6 Flow of control: if-blocks 7 Loops for loops 8 What is a compiler? CS427 Getting started with C++ (Part 2) 2/22

Recall from last week... Please review the notes from last week. Also, please read Chapter 1 of Kelley and Pohl: A Book on C. Each program begins by including header files. In particular, we need I/O Steam: #include <iostream> Declare variables before using them. They may be local to a program block. the heart of the program is the main() function every program needs one. void is the default argument list and can be omitted. Like C, the C++ language is case-sensitive. E.g., the functions main() and Main() are not the same. Curly brackets are used the delimit a program block. Every (logical) line is terminated by a semicolon; In strings, the backslash escape character is used for output. For example \n is used to output a new line. Two forward-slashes // indicate a comment everything after them is ignored until an end-of-line is reached. CS427 Getting started with C++ (Part 2) 3/22

Recall: Output Basic components: #include <iostream> using namespace std; cout and cin The operators << and >> the identifier cout is an object representing the Standard Output Stream. The operator << is the put to operator and sends the text to Standard Output usually the terminal/console window. E.g. cout << "Hello World." << "\n"; CS427 Getting started with C++ (Part 2) 4/22

Recall: Variables Variables are used to temporarily store values (numerical, text, etc,...) and refer to them by name, rather than value. More formally, the variable s name is called the identifier. It must start with a letter or an underscore, and may contain only letters, digits and underscores. Examples: All variables must be defined before they can be used. That means, we need to tell the compiler before we use them. This can be done at any stage in the code, up to when the variable is first used. Every variable should have a type; this tells use what sort of value will be stored in it. The variables/data types we can define include Integers (positive or negative whole numbers), e.g., i n t i ; i = 1 i n t j =122; i n t k = j+i ; CS427 Getting started with C++ (Part 2) 5/22

Recall: Variables Floats these are not whole numbers. They usually have a decimal places. E.g, float pi=3.1415; Note that one can initialize (i.e., assign a value to the variable for the first time) at the time of definition. Characters single alphabetic or numeric symbols, are defined using the char keyword: char c; or char s= 7 ; Note that again we can choose to initialize the character at time of definition. Also, the character should be enclosed by single quotes. We can declare arrays or vectors as follows: int Fib[10]; This declares a integer array called Fib. To access the first element, we refer to Fib[0], to access the second: Fib[1], and to refer to the last entry: Fib[9]. In in C, all vectors in C++ are indexed from 0. CS427 Getting started with C++ (Part 2) 6/22

Recall: Variables Here is a list of common data types. Size is measured in bytes. Type Description (min) Size char character 1 int integer 4 float floating point number 4 double 16 digit (approx) float 8 bool true or false 1 See also: 01Variables.cpp CS427 Getting started with C++ (Part 2) 7/22

Recall: Variables As with C, in C++, there is a distinction between declaration and assignment, but they can be combined. Using the const modifier means the value can t be changed later. Example: Displaying the value stored in a variable is easier than in C, because we don t have to explicitly state the data type. Example: CS427 Getting started with C++ (Part 2) 8/22

Output Manipulators endl As well as passing variable names and strings to the output stream, we can also pass manipulators to change how variable values are displayed. Some require that <iomanip> be included. endl print a new line (and flush) 1 i n t main ( ) 2 { 3 i n t i, f i b [ 1 6 ] ; 4 f i b [ 0 ] = 1 ; f i b [ 1 ] = 1 ; 02Manipulators.cpp 6 cout << \n\ nwithout t h e setw m a n i p u l a t o r << e n d l ; 7 f o r ( i =0; i <=12; i ++) 8 { 9 i f ( i >= 2) f i b [ i ] = f i b [ i 1] + f i b [ i 2]; 10 cout << The << i << th << 11 F i b o n a c c i Number i s << f i b [ i ] << e n d l ; 12 } CS427 Getting started with C++ (Part 2) 9/22

Output Manipulators setw(n) will the width of a field to n. Useful for tabulating data. 02Manipulators.cpp 1 cout << \n\ nwith t h e setw m a n i p u l a t o r << e n d l ; 2 f o r ( i =0; i <=12; i ++) 3 { 4 i f ( i >= 2) f i b [ i ] = f i b [ i 1] + f i b [ i 2]; 5 cout << The << setw ( 2 ) << i << th << 6 F i b o n a c c i Number i s << setw ( 3 ) << f i b [ i ] << e n d l ; 7 } 8 r e t u r n ( 0 ) ; 9 } endl..................................................................... Other useful manipulators: setfill setprecision fixed and scientific dec, hex, oct CS427 Getting started with C++ (Part 2) 10/22

Input In C we typically used scanf to take input from the user (via the keyboard). It s syntax is a little difficult: scanf("%d", &i); We need to use an appropriate conversion character so that the inputted data is correctly interpreted (e.g., distinguish between the number 5 and the character 5 ). Also, we have to remember to pass the address of the desination variable. In C++ things can be a little simpler. The object cin is used. It is a name for the C onsole IN put. In conjunction with the operator >> (called the get from or extraction operator), it assigns data from input stream to the named variable. CS427 Getting started with C++ (Part 2) 11/22

Input 1 #i n c l u d e <i o s t r e a m > 2 #i n c l u d e <s t r i n g > 3 #i n c l u d e <iomanip> 03Input.cpp 5 u s i n g namespace s t d ; 7 i n t main ( ) 8 { 9 c o n s t double S t i r l i n g T o E u r o =1.20810; // Correct 15/01/12 10 double S t i r l i n g ; 11 cout << I n p u t amount i n S t i r l i n g : ; 12 c i n >> S t i r l i n g ; 13 cout << That i s worth << S t i r l i n g S t i r l i n g T o E u r o << 14 Euros \n ; 15 cout << That i s worth << f i x e d << s e t p r e c i s i o n ( 2 ) << 16 \u20ac << S t i r l i n g S t i r l i n g T o E u r o << e n d l ; 17 r e t u r n ( 0 ) ; 18 } CS427 Getting started with C++ (Part 2) 12/22

Input As we will see, cin is an object, with more sophisticated uses/methods than shown here. However, we will defer this discussion until we have studied something of objects and classes. CS427 Getting started with C++ (Part 2) 13/22

Flow of control: if-blocks if statements are used to conditionally execute part of your code. Structure (i): if( exprn ) { statements to execute if exprn evaluates as non-zero } else { statements if exprn evaluates as 0 } CS427 Getting started with C++ (Part 2) 14/22

Flow of control: if-blocks The argument to if() is a logical expression. Example x == 8 m == 5 y <= 1 y!= x y > 0 More complicated examples can be constructed using AND && and OR. CS427 Getting started with C++ (Part 2) 15/22

Flow of control: if-blocks 1 #i n c l u d e <i o s t r e a m > 04EvenOdd.cpp 3 u s i n g namespace s t d ; 5 i n t main ( v o i d ) 6 { 7 i n t Number ; 9 cout << P l e a s e e n t e r an i n t e g r e r : ; 10 c i n >> Number ; 12 i f ( ( Number%2) == 0) 13 cout << That i s an even number. << e n d l ; 14 e l s e 15 cout << That number i s odd. << e n d l ; 17 r e t u r n ( 0 ) ; 18 } CS427 Getting started with C++ (Part 2) 16/22

Flow of control: if-blocks More complicated examples are possible: Structure (ii): if( exp1 ) { statements to execute if exp1 is true } else if (exp2 ) { statements run if exp1 is false but exp2 is true } else { catch all statements if neither exp1 or exp2 true. } CS427 Getting started with C++ (Part 2) 17/22

Flow of control: if-blocks 05Grades.cpp 1 i n t main ( v o i d ) 2 { 3 i n t NumberGrade ; 4 char L e t t e r G r a d e ; 6 cout << P l e a s e e n t e r t h e g r a d e ( p e r c e n t a g e ) : ; 7 c i n >> NumberGrade ; 9 i f ( NumberGrade >= 70 ) 10 L e t t e r G r a d e = A ; 11 e l s e i f ( NumberGrade >= 60 ) 12 L e t t e r G r a d e = B ; 13 e l s e i f ( NumberGrade >= 50 ) 14 L e t t e r G r a d e = C ; 15 e l s e i f ( NumberGrade >= 40 ) 16 L e t t e r G r a d e = D ; 17 e l s e 18 L e t t e r G r a d e = E ; 20 cout << A s c o r e o f << NumberGrade << 21 % c o o r e s p o n d s to a << L e t t e r G r a d e <<. << e n d l ; 22 r e t u r n ( 0 ) ; 23 } CS427 Getting started with C++ (Part 2) 18/22

Flow of control: if-blocks The other main flow-of-control structures are the switch... case structures the use of the? and : operators. Exercise Find out how switch.. case works. Rewrite the Even/Odd example above using switch... case. What errors/bugs/problems are there with the Grades example? Use switch to write an improved version. CS427 Getting started with C++ (Part 2) 19/22

Loops for loops Loops in C++ are the same as in C. The most commonly used loop structure is for for(initial value; test condition; step) { // code to execute inside loop }..................................................................... Example: 06CountDown.cpp 1 i n t main ( v o i d ) 2 { 3 i n t i ; 5 f o r ( i =10; i >=1; i ) 6 cout << i <<... ; 8 cout << Zero! \ n ; 10 r e t u r n ( 0 ) ; 11 } CS427 Getting started with C++ (Part 2) 20/22

Loops for loops The other two common forms of loop in C++ are while loops do... while loops Exercise Rewrite the count down example above using a 1 while loop. 2 do... while loop. CS427 Getting started with C++ (Part 2) 21/22

What is a compiler? CS427 Getting started with C++ (Part 2) 22/22