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

Size: px
Start display at page:

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

Transcription

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

2 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel identical. 9 of the 10 stacks contain coins that weigh 1 gram each while the 10th stack has coins that weigh 1.1 grams each Difference is undistinguishable by humans You have a digital scale which measures the number of grams of the total items on the scale, accurate to 0.1 grams. You can only use the scale ONCE! Select a set of coins to put on the scale all at once in such a way that you can identify which stack has the heavy coins 2

3 10 Stacks of Coins Solution Choose 1 coin from the 1 st stack, 2 coins from the 2 nd stack, etc. and weigh that collection The non-integer portion of the weight returned should indicate which stack 3

4 4 C++ Programming Computer program Sequence of statements whose objec9ve is to accomplish a task Programming Process of planning and crea9ng a program Real- world analogy: a recipe for cooking 4

5 5 A C++ Program 5

6 6 A C++ Program Output Sample run: 6

7 7 Dissec9ng: A C++ Program 7

8 8 Dissec9ng: A C++ Program 8

9 9 Memory for A C++ Program Variable: a memory loca9on whose contents can be changed 9

10 10 The Basics of a C++ Program Func9on (or subprogram): collec9on of statements; when executed, accomplishes something May be predefined or standard Syntax rules: rules that specify which statements (instruc9ons) are legal or valid Seman9c rules: determine the meaning of the instruc9ons Programming language: a set of rules, symbols, and special words 10

11 11 Comments Comments are for the reader, not the compiler Two types: Single line: begin with // // This is a C++ program. // Welcome to C++ Programming. Mul9ple line: enclosed between /* and */ /* */ You can include comments that can occupy several lines. 11

12 12 Special Symbols Token: the smallest individual unit of a program wriren in any language C++ tokens include special symbols, word symbols, and iden9fiers Special symbols in C++ include: 12

13 13 Reserved Words (Keywords) Reserved word symbols (or keywords): Cannot be redefined within program Cannot be used for anything other than their intended use Examples: int float double char const void return 13

14 14 Iden9fiers Iden9fier: the name of something that appears in a program Consists of lerers, digits, and the underscore character (_) Must begin with a lerer or underscore C++ is case sensi9ve NUMBER is not the same as number Two predefined iden9fiers are cout and cin Unlike reserved words, predefined iden9fiers may be redefined, but it is not a good idea 14

15 15 Iden9fiers Legal iden9fiers in C++: first conversion payrate 15

16 16 Whitespaces Every C++ program contains whitespaces Include blanks, tabs, and newline characters Used to separate special symbols, reserved words, and iden9fiers Proper u9liza9on of whitespaces is important Can be used to make the program more readable 16

17 17 Data Types Data type: set of values together with a set of opera9ons C++ data types fall into three categories: Simple data type Structured data type Pointers 17

18 18 Simple Data Types Three categories of simple data Integral: integers (numbers without a decimal) Can be further categorized: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, unsigned long Floa9ng- point: decimal numbers Enumera9on type: user- defined data type 18

19 19 Simple Data Types Different compilers may allow different ranges of values 19

20 20 int Data Type Examples: Cannot use a comma within an integer Commas are only used for separa9ng items in a list 20

21 21 bool Data Type bool type Two values: true and false Manipulate logical (Boolean) expressions true and false Logical values bool, true, and false Reserved words 21

22 22 char Data Type The smallest integral data type Used for single characters: lerers, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character WriRen ' ', with a space le\ between the single quotes 22

23 23 char Data Type (cont d.) Different character data sets exist ASCII: American Standard Code for Informa9on Interchange Each of 128 values in ASCII code set represents a different character Characters have a predefined ordering based on the ASCII numeric value Colla9ng sequence: ordering of characters based on the character set code 23

24 24 Floa9ng- Point Data Types C++ uses scien9fic nota9on to represent real numbers (floa9ng- point nota9on) 24

25 25 Floa9ng- Point Data Types float: represents any real number Range: - 3.4E+38 to 3.4E+38 (four bytes) double: represents any real number Range: - 1.7E+308 to 1.7E+308 (eight bytes) Minimum and maximum values of data types are system dependent 25

26 26 Floa9ng- Point Data Types Maximum number of significant digits (decimal places) for float values: 6 or 7 Maximum number of significant digits for double: 15 Precision: maximum number of significant digits Float values are called single precision Double values are called double precision 26

27 27 Data Types and Variables To declare a variable, must specify the data type it will store Syntax: datatype identifier; Examples: int counter; double interestrate; char grade; 27

28 28 Arithme9c Operators, Operator Precedence, and Expressions C++ arithme9c operators: + addi9on - subtrac9on * mul9plica9on / division % modulus (or remainder) operator +, -, *, and / can be used with integral and floa9ng- point data types Use % only with integral data types 28

29 29 Arithme9c Operators, Operator Precedence, and Expressions When you use / with integral data types, the integral result is truncated (no rounding) Arithme9c expressions: contain values and arithme9c operators Operands: the number of values on which the operators will work Operators can be unary (one operand) or binary (two operands) 29

30 30 Order of Precedence All opera9ons inside of () are evaluated first *, /, and % are at the same level of precedence and are evaluated next + and have the same level of precedence and are evaluated last When operators are on the same level Performed from le\ to right (associa9vity) 3 * * 5 / means (((3 * 7) 6) + ((2 * 5) / 4 ))

31 31 Expressions Integral expression: all operands are integers Yields an integral result Example: * 5 Floa9ng- point expression: all operands are floa9ng- point Yields a floa9ng- point result Example: 12.8 *

32 32 Mixed Expressions Mixed expression: Has operands of different data types Contains integers and floa9ng- point Examples of mixed expressions: / * / 2 32

33 33 Mixed Expressions Evalua9on rules: If operator has same types of operands Evaluated according to the type of the operands If operator has both types of operands Integer is changed to floa9ng- point Operator is evaluated Result is floa9ng- point En9re expression is evaluated according to precedence rules 33

34 34 Type Conversion (Cas9ng) Implicit type coercion: when value of one type is automa9cally changed to another type Cast operator: provides explicit type conversion static_cast<datatypename>(expression) 34

35 35 string Type Programmer- defined type supplied in ANSI/ISO Standard C++ library Sequence of zero or more characters enclosed in double quota9on marks Null (or empty): a string with no characters Each character has a rela9ve posi9on in the string Posi9on of first character is 0 Length of a string is number of characters in it Example: length of "William Jacob" is 13 35

36 36 Allocating Memory with Constants and Variables Named constant: memory loca9on whose content can t change during execu9on Syntax to declare a named constant: In C++, const is a reserved word 36

37 37 Alloca9ng Memory with Variables Variable: memory loca9on whose content may change during execu9on 37

38 38 Assignment Statement The assignment statement takes the form: Expression is evaluated and its value is assigned to the variable on the le\ side A variable is said to be ini9alized the first 9me a value is placed into it In C++, = is called the assignment operator 38

39 39 Assignment Statement 39

40 40 Saving and Using the Value of an Expression To save the value of an expression: Declare a variable of the appropriate data type Assign the value of the expression to the variable that was declared Use the assignment statement Wherever the value of the expression is needed, use the variable holding the value 40

41 41 Declaring & Ini9alizing Variables Not all types of variables are ini9alized automa9cally Variables can be ini9alized when declared: int first=13, second=10; char ch=' '; double x=12.6; All variables must be ini9alized before they are used But not necessarily during declara9on 41

42 42 Input (Read) Statement cin is used with >> to gather input This is called an input (read) statement The stream extrac9on operator is >> For example, if miles is a double variable cin >> miles; Causes computer to get a value of type double and places it in the variable miles 42

43 43 Input (Read) Statement Using more than one variable in cin allows more than one value to be read at a 9me Example: if feet and inches are variables of type int, this statement: cin >> feet >> inches; Inputs two integers from the keyboard Places them in variables feet and inches respec9vely 43

44 44 Input (Read) Statement.) 44

45 45 Increment and Decrement Operators Increment operator: increase variable by 1 Pre- increment: ++variable Post- increment: variable++ Decrement operator: decrease variable by 1 Pre- decrement: --variable Post- decrement: variable What is the difference between the following? x = 5; y = ++x; x = 5; y = x++; 45

46 46 Output The syntax of cout and << is: Called an output statement The stream inser9on operator is << Expression evaluated and its value is printed at the current cursor posi9on on the screen 46

47 47 Output A manipulator, like endl, is used to format the output 47

48 48 Preprocessor Direc9ves Many func9ons and symbols needed to run a C++ program are provided as collec9on of libraries Every library has a name and is referred to by a header file Preprocessor direc9ves are commands supplied to the preprocessor program All preprocessor commands begin with # No semicolon at the end of these commands 48

49 49 Preprocessor Direc9ves Syntax to include a header file: For example: #include <iostream> Causes the preprocessor to include the header file iostream in the program Preprocessor commands are processed before the program goes through the compiler 49

50 50 namespace and Using cin and cout in a Program cin and cout are declared in the header file iostream, but within std namespace To use cin and cout in a program, use the following two statements: #include <iostream> using namespace std; 50

51 51 Crea9ng a C++ Program A C++ program is a collec9on of func9ons, one of which is the func9on main The first line of the func9on main is called the heading of the func9on: int main() The statements enclosed between the curly braces ({ and }) form the body of the func9on 51

52 52 Crea9ng a C++ Program A C++ program contains two types of statements: Declara9on statements: declare things, such as variables Executable statements: perform calcula9ons, manipulate data, create output, accept input, etc. 52

53 53 Crea9ng a C++ Program C++ program has two parts: Preprocessor direc9ves The program Preprocessor direc9ves and program statements cons9tute C++ source code (.cpp) Compiler generates object code (.o) Executable code is produced and saved in a file 53

54 54 Syntax Syntax rules: indicate what is legal and what is not legal Errors in syntax are found in compila9on int x; //Line 1 int y //Line 2: error double z; //Line 3 y = w + x; //Line 4: error 54

55 55 Use of Blanks In C++, you use one or more blanks to separate numbers when data is input Blanks are also used to separate reserved words and iden9fiers from each other and from other symbols Blanks must never appear within a reserved word or iden9fier 55

56 56 Use of Semicolons, Brackets, and Commas All C++ statements end with a semicolon Also called a statement terminator { and } are not C++ statements Can be regarded as delimiters Commas separate items in a list 56

57 57 Seman9cs Seman9cs: set of rules that gives meaning to a language Possible to remove all syntax errors in a program and s9ll not have it run Even if it runs, it may s9ll not do what you meant it to do Ex: * 5 and (2 + 3) * 5 are both syntac9cally correct expressions, but have different meanings 57

58 58 Naming Iden9fiers Iden9fiers can be self- documen9ng: CENTIMETERS_PER_INCH Avoid run- together words : annualsale Solu9on: Capitalizing the beginning of each new word: annualsale Inser9ng an underscore just before a new word: annual_sale 58

59 59 Prompt Lines Prompt lines: executable statements that inform the user what to do cout << "Please enter a number between 1 and 10 and " << "press the return key" << endl; cin >> num; Always include prompt lines when input is needed from users 59

60 60 Documenta9on A well- documented program is easier to understand and modify You use comments to document programs Comments should appear in a program to: Explain the purpose of the program Iden9fy who wrote it Explain the purpose of par9cular statements 60

61 61 Form and Style Consider two ways of declaring variables: Method 1 int feet, inch; double x, y; Method 2 int feet,inch;double x,y; Both are correct; however, the second is hard to read 61

62 62 Processing a C++ Program #include <iostream> using namespace std; int main() { cout << Hello, World" << endl; return 0; } Sample Run: Hello, World 62

63 63 Processing a C++ Program #include <iostream> using namespace std; int main(){ cout << Hello, World" << endl; return 0;} Sample Run: Hello, World 63

64 64 #include <iostream> using namespace std; int main() { cout << Hello, World" << endl; return 0; } Sample Run: Hello, World 64

65 65 Algorithm Development Algorithm: Get length of the rectangle Get width of the rectangle Find the perimeter using the following equa9on: perimeter = 2 * (length + width) Find the area using the following equa9on: area = length * width 65

66 66 Algorithm Development Problem: Calculate students grades and class average 10 students in a class; each student has taken five tests; each test is worth 100 points Design algorithms to: Calculate the grade for each student and class average Find the average test score Determine the grade Data: students names; test scores 66

67 67 Algorithm Algorithm to determine the average test score: Get the five test scores Add the five test scores Suppose sum stands for the sum of the test scores Suppose average stands for the average test score: average = sum / 5; 67

68 68 Algorithm Algorithm to determine the grade: if average is greater than or equal to 90 grade = A otherwise if average is greater than or equal to 80 and less than 90 grade = B otherwise if average is greater than or equal to 70 and less than 80 grade = C otherwise if average is greater than or equal to 60 and less than 70 grade = D otherwise grade = F 68

69 69 Algorithm Main algorithm is as follows: totalaverage = 0; Repeat the following for each student: Get student s data (name and test scores) Use the algorithm to find the average test score Use the algorithm to find the grade Update totalaverage by adding current student s average test score Determine the class average as follows: classaverage = totalaverage / 10 69

70 Algorithm Development: Fibonacci Number Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34,... Called the Fibonacci sequence Given the input n The n th number a n, n >= 3, of this sequence is given by: a n = a n- 1 + a n

Chapter 2: Basic Elements of C++

Chapter 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 information

Objectives. 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++ 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 information

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

Chapter 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 information

Objectives. In this chapter, you will:

Objectives. 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 information

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

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

Main Memory Organization

Main Memory Organization Main Memory Organization Bit Smallest piece of memory Stands for binary digit Has values 0 (off) or 1 (on) Byte Is 8 consecu>ve bits Word Usually 4 consecu>ve bytes Has an address 8 bits 0 1 1 0 0 1 1

More information

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

Another Simple Program: Adding Two Integers

Another Simple Program: Adding Two Integers 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>, to obtain two integers

More information

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

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

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

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++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

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

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3 Aykut Erdem, Erkut Erdem, Fuat Akal 1 Today Introduc/on to Programming Basic Concepts Developing Algorithms Crea

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

The C++ Language. Arizona State University 1

The 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 information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

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

Review: 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 information

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

PART 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 information

BASIC ELEMENTS OF A COMPUTER PROGRAM

BASIC ELEMENTS OF A COMPUTER PROGRAM BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING LOGO Contents 1 Identifier 2 3 Rules for naming and declaring data variables Basic data types 4 Arithmetic operators

More information

BITG 1233: Introduction to C++

BITG 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 information

Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

More information

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

Chapter 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, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

Chapter 2: Overview of C++

Chapter 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

LECTURE 02 INTRODUCTION TO C++

LECTURE 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 information

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

! 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 information

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

1. In C++, reserved words are the same as predefined identifiers. a. True C++ Programming From Problem Analysis to Program Design 8th Edition Malik TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/c-programming-problem-analysis-program-design-8thedition-malik-test-bank/

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Creating a C++ Program

Creating a C++ Program Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

More information

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

A First Program - Greeting.cpp

A 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 information

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set

More information

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

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

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

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

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2 Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2 Ziad Matni Dept. of Computer Science, UCSB A Word About Registra>on for CS16 FOR THOSE OF YOU NOT YET REGISTERED: There s a LONG waitlist

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

CHAPTER 3 BASIC INSTRUCTION OF C++

CHAPTER 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 information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Programming. C++ Basics

Programming. 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 information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data Declaring Variables Constant Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering 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 information

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

! 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 information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

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

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

Topic 2: C++ Programming fundamentals

Topic 2: C++ Programming fundamentals Topic 2: C++ Programming fundamentals Learning Outcomes Upon successful completion of this topic you will be able to: describe basic elements of C++ programming language compile a program identify and

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

CSc Introduction to Computing

CSc Introduction to Computing CSc 10200 Introduction to Computing Lecture 2 Edgardo Molina Fall 2011 - City College of New York Thursday, September 1, 2011 Introduction to C++ Modular program: A program consisting of interrelated segments

More information

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

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Malak Abdullah The Evolution of Programming Languages (cont'd.)

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

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

Introduction to C++ Programming Pearson Education, Inc. All rights reserved. 1 2 Introduction to C++ Programming 2 What s in a name? that which we call a rose By any other name would smell as sweet. William Shakespeare When faced with a decision, I always ask, What would be the

More information

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

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1 NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.

More information

BTE2313. Chapter 2: Introduction to C++ Programming

BTE2313. Chapter 2: Introduction to C++ Programming For updated version, please click on http://ocw.ump.edu.my BTE2313 Chapter 2: Introduction to C++ Programming by Sulastri Abdul Manap Faculty of Engineering Technology sulastri@ump.edu.my Objectives In

More information

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

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

Programming with C++ as a Second Language

Programming with C++ as a Second Language Programming with C++ as a Second Language Week 2 Overview of C++ CSE/ICS 45C Patricia Lee, PhD Chapter 1 C++ Basics Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Introduction to

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

More information

Introduction to C# Applications

Introduction to C# Applications 1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making

More information

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

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights

More information

CHAPTER 3 Expressions, Functions, Output

CHAPTER 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 information

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.

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. Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,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 information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

UNIT-2 Introduction to C++

UNIT-2 Introduction to C++ UNIT-2 Introduction to C++ C++ CHARACTER SET Character set is asset of valid characters that a language can recognize. A character can represents any letter, digit, or any other sign. Following are some

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 5 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

More information

Chapter 2 C++ Fundamentals

Chapter 2 C++ Fundamentals Chapter 2 C++ Fundamentals 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates Goals Reuse existing code in your programs with #include Obtain input data from the user

More information

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) 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

More information

CSCI 1061U Programming Workshop 2. C++ Basics

CSCI 1061U Programming Workshop 2. C++ Basics CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 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 information

C Programming Primer 8/31/15 1

C Programming Primer 8/31/15 1 C Programming Primer 8/31/15 1 Objec5ves Develop a func5onal knowledge of C programming concepts Understand basic variables, constructs, and control flow statements 8/31/15 2 Special Notes Copying and

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

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

CS 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 information

Java Notes. 10th ICSE. Saravanan Ganesh

Java Notes. 10th ICSE. Saravanan Ganesh Java Notes 10th ICSE Saravanan Ganesh 13 Java Character Set Character set is a set of valid characters that a language can recognise A character represents any letter, digit or any other sign Java uses

More information

CS2255 HOMEWORK #1 Fall 2012

CS2255 HOMEWORK #1 Fall 2012 CS55 HOMEWORK #1 Fall 01 1.What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; a. 10 b. 7 c. The

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

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

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C Overview The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

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?

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? Objec+ves Basics of Java Syntax Java fundamentals Ø Primi+ve data types Ø Sta+c typing Ø Arithme+c operators Ø Rela+onal operators 1 Review What are quali+es of good sooware? What is Java? Ø Benefits to

More information

Introduction to the C++ Programming Language

Introduction to the C++ Programming Language LESSON SET 2 Introduction to the C++ Programming Language OBJECTIVES FOR STUDENT Lesson 2A: 1. To learn the basic components of a C++ program 2. To gain a basic knowledge of how memory is used in programming

More information

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Motivation Hello, world! Basic Data Types Variables Arithmetic Operators Relational Operators Assignments

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

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

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

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

Chapter 7. Additional Control Structures

Chapter 7. Additional Control Structures Chapter 7 Additional Control Structures 1 Chapter 7 Topics Switch Statement for Multi-Way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements 2 Chapter

More information