CSc Introduction to Computing

Similar documents
Lecture 2 Tao Wang 1

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

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

UEE1302 (1102) F10: Introduction to Computers and Programming

CS242 COMPUTER PROGRAMMING

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

Lecture 3 Tao Wang 1

6.096 Introduction to C++ January (IAP) 2009

LECTURE 02 INTRODUCTION TO C++

Chapter 2: Introduction to C++

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

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

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

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

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

The C++ Language. Arizona State University 1

Computer Programming : C++

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

Chapter 2: Basic Elements of C++

Chapter 1 INTRODUCTION

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

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT- 3 Introduction to C++

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

Creating a C++ Program

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

BITG 1233: Introduction to C++

2 nd Week Lecture Notes

Objectives. In this chapter, you will:

Chapter 2 Basic Elements of C++

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

Exercise: Using Numbers

CHAPTER 3 BASIC INSTRUCTION OF C++

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

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

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

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

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Data Types and Variables in C language

Getting started with C++ (Part 2)

Java Notes. 10th ICSE. Saravanan Ganesh

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

BTE2313. Chapter 2: Introduction to C++ Programming

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

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

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

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

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

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

Fundamentals of Programming Session 4

VARIABLES & ASSIGNMENTS

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

4. Inputting data or messages to a function is called passing data to the function.

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

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

Variables. Data Types.

Number Systems, Scalar Types, and Input and Output

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

Fundamental of Programming (C)

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

ARG! Language Reference Manual

C++ For Science and Engineering Lecture 15

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

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

Week 3 Lecture 2. Types Constants and Variables

C: How to Program. Week /Mar/05

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

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Basics of Programming

Full file at

Your first C++ program

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

DEPARTMENT OF MATHS, MJ COLLEGE

Basics of Java Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter 2 - Introduction to C Programming

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

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

Cellular Automata Language (CAL) Language Reference Manual

Presented By : Gaurav Juneja

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

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Chapter 2 Working with Data Types and Operators

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

Visual C# Instructor s Manual Table of Contents

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter-8 DATA TYPES. Introduction. Variable:

Fundamentals of Programming CS-110. Lecture 2

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

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

VARIABLES AND CONSTANTS

Transcription:

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 arranged in a logical and understandable form Easier to develop, correct, and modify than other kinds of programs Module: A small segment which is designed to perform a specific task A group of modules is used to construct a modular program Thursday, September 1, 2011

Introduction to C++ Modules in C++ can be classes or functions Function: Accepts an input and produces an output by processing the input in some fashion A function s processing is encapsulated and hidden within the function Thursday, September 1, 2011

Functions Thursday, September 1, 2011

Hello World #include <iostream> using namespace std; int main() { cout << "Hello World!\n"; } return 0; Thursday, September 1, 2011

main() is a special function Overall structure of a C++ program contains one function named main(), called the driver function All other functions are invoked from main() Thursday, September 1, 2011

main() Thursday, September 1, 2011

What does it mean? Thursday, September 1, 2011

the main() function Function header line: First line of a function, which contains: ( any The type of data returned by the function (if The name of the function The type of data that must be passed into the ( any function when it is invoked (if Arguments: The data passed into a function Function body: The statements inside a function ( braces (enclosed in Thursday, September 1, 2011

the main() function Each statement inside the function must be terminated with a semicolon return: A keyword causing the appropriate value to be returned from the function The statement return 0 in the main() function causes the program to end Thursday, September 1, 2011

Data Types, Variables and Assignment Thursday, September 1, 2011

Comments Comments: Explanatory remarks in the source code added by the programmer Line comment: Begins with // and continues to the end of the line Line comment can be on a line by itself, or at the end of a line of code Line comment cannot be longer than one line C++ for Engineers and Scientists, Third Edition 1

Comments (continued)! C++ for Engineers and Scientists, Third Edition 2

Comments (continued)! Block comments: Span across two or more lines Begin with /* and ends with */ Example: /* This is a block comment that spans across three lines */ C++ for Engineers and Scientists, Third Edition 3

Data Programs operate on data We use names (or identifiers) to access the data What is data? Numbers: 1, 212, 3.14, " Text: city college of new york, cs102 Images, Videos, Music, etc Fundamentals of Information Systems, Fifth Edition 4

Reserved Keywords Keyword: A reserved name that represents a built-in object or function of the language Table 2.1: Keywords in C++ C++ for Engineers and Scientists, Third Edition 5

Valid Names Rules for forming identifier names: First character must be a letter or underscore Only letters, digits, or underscores may follow the initial letter (no blanks allowed)! Keywords cannot be used as identifiers Maximum length of an identifier = 1024 characters C++ for Engineers and Scientists, Third Edition 6

Valid Names (continued)! Examples of valid C++ identifiers: degtorad intersect addnums slope bessell multtwo findmax density Examples of invalid C++ identifiers: 1AB3 (begins with a number)! E*6 (contains a special character)! while (this is a keyword)! C++ for Engineers and Scientists, Third Edition 7

Naming Functions Function names Require a set of parentheses at the end Can use mixed upper and lower case Should be meaningful, or be a mnemonic Mnemonic: A word designed as a memory aid Examples of function names: easy() c3po() r2d2() theforce()! Note that C++ is a case-sensitive language! C++ for Engineers and Scientists, Third Edition 8

Data Types Data type: A set of values and the operations that can be applied to these values Two fundamental C++ data groupings: Class data type (a class): Created by the programmer Built-in data type (primitive type): Part of the C++ compiler C++ for Engineers and Scientists, Third Edition 9

Data Types (continued)! Figure 2.5 Built-in data types C++ for Engineers and Scientists, Third Edition 10

Data Types (continued)! Table 2.2 Built-in Data Types Operations C++ for Engineers and Scientists, Third Edition 11

Data Types (continued)! Literal (constant): An actual value Examples: 3.6 //numeric literal Hello //string literal Integer: A whole number C++ has nine built-in integer data types Each provides differing amounts of storage (compiler dependent) C++ for Engineers and Scientists, Third Edition 12

Integer Data Types Figure 2.6 C++ integer data types C++ for Engineers and Scientists, Third Edition 13

Integer Data Types (continued)! int data type: Whole numbers, optionally with plus (+) or minus ( ) sign Example: 2 char data type: Individual character; any letter, digit, or special character enclosed in single quotes Example: A Character values are usually stored in ASCII code C++ for Engineers and Scientists, Third Edition 14

Integer Data Types (continued)! Table 2.3 The ASCII Uppercase Letter Codes C++ for Engineers and Scientists, Third Edition 15

Integer Data Types (continued)! When storing the ASCII codes shown in Table 2.3 to represent text, each letter takes one byte of memory and is represented by the associated number from the chart Figure 2.7 The letters BARTER stored inside a computer C++ for Engineers and Scientists, Third Edition 16

Integer Data Types (continued)! Escape character: The backslash, \ Indicates an escape sequence Escape sequence: Tells compiler to treat the following characters as special instruction codes C++ for Engineers and Scientists, Third Edition 17

Integer Data Types (continued)! Table 2.4 Escape sequences C++ for Engineers and Scientists, Third Edition 18

Integer Data Types (continued)! Table 2.4 Escape sequences (continued)! C++ for Engineers and Scientists, Third Edition 19

Integer Data Types (continued)! bool data type: Represents Boolean (logical) data Restricted to two values: true or false Useful to indicate a condition and take a prescribed course of action C++ for Engineers and Scientists, Third Edition 20

Determining Storage Size A unique feature of C++ is that you can see where and how values are stored sizeof() operator provides the number of bytes used to store values of the data type names in the parenthesis Values returned by sizeof() are compiler dependent C++ for Engineers and Scientists, Third Edition 21

Determining Storage Size (continued)! C++ for Engineers and Scientists, Third Edition 22

Signed and Unsigned Data Types Signed data type: One that permits negative, positive, and zero values Unsigned data type: Permits only positive and zero values An unsigned data type provides essentially double the range of its signed counterpart C++ for Engineers and Scientists, Third Edition 23

Signed and Unsigned Data Types (continued)! Table 2.5 Integer Data Type Storage C++ for Engineers and Scientists, Third Edition 24

Floating-Point Types Floating-point number (real number): Zero or any positive or negative number containing a decimal point Examples: +10.625 5. -6.2 No special characters are allowed Three floating-point data types in C++: float (single precision)! double (double precision)! long double C++ for Engineers and Scientists, Third Edition 25

Floating-Point Types (continued)! Table 2.6 Floating-Point Data Types C++ for Engineers and Scientists, Third Edition 26

Floating-Point Types (continued)! float literal: Append an f or F to the number long double literal: Append an l or L to the number Examples: 9.234 // a double literal 9.234F // a float literal 9.234L // a long double literal C++ for Engineers and Scientists, Third Edition 27

Variables and Declaration Statements Variable: All integer, float-point, and other values used in a program are stored and retrieved from the computer's memory Each memory location has a unique address Figure 2.8 Enough storage for two integers C++ for Engineers and Scientists, Third Edition 28

Variables and Declaration Statements (continued)! Variable: Symbolic identifier for a memory address where data can be held Use identifier naming rules for variable names Figure 2.9 Naming storage locations C++ for Engineers and Scientists, Third Edition 29

Variables and Declaration Statements (continued)! Assignment statement: Used to store a value into a variable Value of the expression on the right side of the = is assigned to the memory location of the variable on the left side of the = Examples: num1 = 45; num2 = 12; total = num1 + num2; C++ for Engineers and Scientists, Third Edition 30

Variables and Declaration Statements (continued)! Declaration statement: Specifies the data type and identifier of a variable; sets up the memory location Syntax: datatype variablename; Data type is any valid C++ data type Example: int sum; Declarations may be used anywhere in a function Usually grouped at the opening brace C++ for Engineers and Scientists, Third Edition 31

Variables and Declaration Statements (continued)! Character variables: Declared using the char keyword Multiple variables of the same data type can be declared in a single declaration statement Example: double grade1, grade2, total, average; Variables can be initialized in a declaration Example: double grade1 = 87.0 A variable must be declared before it is used C++ for Engineers and Scientists, Third Edition 32

Variables and Declaration Statements (continued)! C++ for Engineers and Scientists, Third Edition 33

Memory Allocation Definition statement: A declaration that defines how much memory is needed for data storage Three items associated with each variable: Data type Actual value stored in the variable (its contents)! Memory address of the variable Address operator (&) provides the variable s address C++ for Engineers and Scientists, Third Edition 34

Memory Allocation (continued)! Declaring a variable causes memory to be allocated based on the data type Figure 2.10b Defining the floating-point variable named slope C++ for Engineers and Scientists, Third Edition 35

Memory Allocation (continued)! C++ for Engineers and Scientists, Third Edition 36

READ CH. 2 Fundamentals of Information Systems, Fifth Edition 37