UEE1302 (1102) F10: Introduction to Computers and Programming

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

Lecture 2 Tao Wang 1

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

CSc Introduction to Computing

Understanding main() function Input/Output Streams

Chapter 1 Getting Started Structured Programming 1

Programming in C++ 4. The lexical basis of C++

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

6.096 Introduction to C++ January (IAP) 2009

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

Programming. C++ Basics

Computer Programming : C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

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

Chapter 2: Introduction to C++

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

C: How to Program. Week /Mar/05

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

Chapter 2 - Introduction to C Programming

LECTURE 02 INTRODUCTION TO C++

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

The C++ Language. Arizona State University 1

Variables. Data Types.

UNIT-2 Introduction to C++

INTRODUCTION 1 AND REVIEW

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Basic Types, Variables, Literals, Constants

Creating a C++ Program

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

IS 0020 Program Design and Software Tools

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

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

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

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

Introduction to C# Applications

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

4. Structure of a C++ program

Fundamental of Programming (C)

Introduction to C++ Systems Programming

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Chapter 1 INTRODUCTION

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

Chapter 2 Basic Elements of C++

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

Objectives. In this chapter, you will:

BITG 1233: Introduction to C++

Data Types and Variables in C language

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

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

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

UNIT- 3 Introduction to C++

A A B U n i v e r s i t y

Introduction to Programming

DEPARTMENT OF MATHS, MJ COLLEGE

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

Chapter 2. C++ Basics

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Tokens, Expressions and Control Structures

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

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

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

2 nd Week Lecture Notes

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

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Getting started with C++ (Part 2)

Syntax and Variables

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Programming with C++ as a Second Language

Chapter 1 & 2 Introduction to C Language

Introduction to C++ CS 1: Problem Solving & Program Design Using C++

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Basic Elements of C. Staff Incharge: S.Sasirekha

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Short Notes of CS201

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

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

CS201 - Introduction to Programming Glossary By

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

H.O.#2 Fall 2015 Gary Chan. Overview of C++ Programming

Problem Solving With C++ Ninth Edition

CSI33 Data Structures

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

Maciej Sobieraj. Lecture 1

CSCI 123 Introduction to Programming Concepts in C++

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

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

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Interview Questions of C++

Chapter 2 C++ Fundamentals

Programming with C++ Language

Procedures, Parameters, Values and Variables. Steven R. Bagley

Your First C++ Program. September 1, 2010

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

Lecture 3 Tao Wang 1

EEE145 Computer Programming

Transcription:

Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions and Assignment Statements Console Input & Output Style of C++ Programs Libraries and Namespaces PRO_00 PROF. HUNG-PIN (CHARLES) WEN 2 Introduction to C++ C++ origins Low-level languages Ex: Machine, assembly High-level languages Ex: C, C++, ADA, COBOL, FORTRAN Object-Oriented Programming in C++ C++ Terminology Programs and functions Basic Input & Output (I/O) with cin and cout Algorithms vs. Procedures Before writing a program, a programmer must clearly understand What data is to be used Desired result Procedure needed to produce this result The procedure is referred to as an algorithm Algorithm: Step-by-step sequence of instructions describing how to perform a computation PRO_00 PROF. HUNG-PIN (CHARLES) WEN 3 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 4

Example of Algorithms (1/2) Assume that a program must calculate sum of all whole numbers from 1 through 100 A computer can not respond to heuristic command: Add the numbers from 1-100 A computer is algorithm-responding machine and not intuition-responding machine Several methods or algorithms can be used to find the required sum Example of Algorithms (2/2) sum = n(a + b)/2 where n = number of terms to be added (100) a = first number added (1) b = last number to be added (100) sum = 100(1 + 100)/2 = 5050 Question: any other algorithm? PRO_00 PROF. HUNG-PIN (CHARLES) WEN 5 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 6 Flowchart Symbols (1/2) Flowchart Symbols (2/2) SYMBOL NAME DESCRIPTION SYMBOL NAME DESCRIPTION Terminal Indicates the beginning or end of an algorithm Decision Indicates a decision point in the algorithm Input/Output Indicates an input or output operation Loop Indicates the initial, final and increment values of a loop Process Indicates computation or data manipulation Predefined process Indicates a predefined process, as in calling a sorting process Flow lines Used to connect the flowchart symbols and indicate the logic flow PRO_00 PROF. HUNG-PIN (CHARLES) WEN 7 Connector Indicates an entry to, or exit from another part of the flowchart PRO_00 PROF. HUNG-PIN (CHARLES) WEN 8

Example of Flowchart Classes and Objects Question: How do you design a flowchart to calculate the average of three numbers? Exercise: Design a flowchart to sort five numbers and output the biggest one? Start Input three values Calculate the average Display the average End Data Object: Set of values packaged as single unit Class: Set of objects with similar attributes General concept of object-oriented programming is difference between an object and the larger set of which it is a member (class) A red, Ford Taurus sedan is an instance, or object, of general class of automobiles PRO_00 PROF. HUNG-PIN (CHARLES) WEN 9 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 10 Program Translation (1/2) Program Translation (2/2) C++ source program: Set of instructions written in C++ language Machine language: Internal computer language Consists of a series of 1s and 0s Source program cannot be executed until it is translated into machine language C++ language source program Translation program Machine language program Interpreted language translates one statement at a time Compiled language translates all statements together PRO_00 PROF. HUNG-PIN (CHARLES) WEN 11 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 12

Function vs. Class Names (1/3) Modular programs: Segments arranged in logical order to form an integrated unit Module: Segments of modular program Function: Name of a C++ procedure Composed of sequence of C++ instructions Function interface is its inputs and outputs Method of converting input to results is encapsulated and hidden within function Function vs. Class Names (2/3) Module 1 Module 2 Module 3 Module 4 Module 5 Module 6 FIGURE 1.7 A Well-Designed Program is Built Using Modules PRO_00 PROF. HUNG-PIN (CHARLES) WEN 13 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 14 Function vs. Class Names (3/3) Function/Class Naming Conventions First number Second number A X B Result FIGURE 1.8 A Multiplying Function Identifiers: Names that convey an idea of the purpose of function or class Identifier composition rules: First character must be a letter or underscore Only letter, digit or underscore may follow Blank spaces allowed Identify component words with initial capitalization Cannot be C++ keyword Should be a mnemonic PRO_00 PROF. HUNG-PIN(CHARLES) WEN 15 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 16

C++ Keywords C++ Identifiers (1/2) auto default goto public this break do if register template case double inline return typedef catch else int short union char enum long signed unsigned class extern new sizeof virtual const float overload static void continue for private struct volatile delete friend protected switch while Examples of valid identifiers: grosspay taxcalc addnums degtorad multbytwo salestax netpay bessel PRO_00 PROF. HUNG-PIN (CHARLES) WEN 17 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 18 C++ Identifiers (2/2) Examples of invalid identifiers: 4ab3 (begins with a number) e*6 (contains a special character) while (is a keyword) The main Function (1/3) Each C+ program must have one and only one function named main Called a driver function because it drives the other modules PRO_00 PROF. HUNG-PIN (CHARLES) WEN 19 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 20

The main Function (2/3) The main Function (3/3) main You go first I m done You go first I m done You go first I m done You go first I m done 1st module 2nd module 3rd module last module First line of function is called header line What type of data, if any, is returned from function The name of function What type of data, if any, is sent into function Data transmitted into function at run time are referred to as arguments of function PRO_00 PROF. HUNG-PIN (CHARLES) WEN 21 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 22 main Function Composition Program 1.0 #include <iostream> using namespace std; int main( ) Function Body Type of returned value Function name An empty argument list The cout Object The cout object sends data to the standard output display device The display device is usually a video screen Name derived from Console OUTput and pronounced see out Data is passed to cout by the insertion symbol cout << Hello there, world! ; PRO_00 PROF. HUNG-PIN (CHARLES) WEN 23 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 24

C++ Sample Code using cout Newline Escape Sequence Program 1.1 #include <iostream> using namespace std; int main( ) cout << Hello, world! ; Task: passing a message to cout return 0; What will be displayed on screen? Hello, world! PRO_00 PROF. HUNG-PIN (CHARLES) WEN 25 Instructs the display device to move to a new line A newline caused when the characters backslash \ and n are used together Backslash provides an escape from the normal interpretation of the character that follows Newline escape sequences can be placed anywhere within a message to cout PRO_00 PROF. HUNG-PIN (CHARLES) WEN 26 Preprocessor Command Performs an action before the compiler translates source code to machine code An example is: #include <iostream> Causes the iostream file to be inserted wherever the #include command appears iostream is part of the C++ standard library include two important classes: istream: declarations and methods for data input ostream: declarations and methods for data output Namespaces Files accessed by compiler when looking for prewritten classes or functions Sample namespace statement: using namespace std; iostream contained in a namespace called std Compiler uses iostream s cout object from std whenever cout is referenced PRO_00 PROF. HUNG-PIN (CHARLES) WEN 27 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 28

Program 1.2 #include <iostream> using namespace std; int main( ) cout << Computer, computers everywhere ; cout << \n as far as I can see ; return 0; More C++ Sample Code (1/2) More C++ Sample Code (2/2) Program 1.3 #include <iostream> using namespace std; int main( ) cout << Computers everywhere\n as far as\n\ni can see ; return 0; What will be displayed on screen? Computer, computers everywhere as far as I can see PRO_00 PROF. HUNG-PIN (CHARLES) WEN 29 What will be displayed on screen? Computer everywhere as far as I can see PRO_00 PROF. HUNG-PIN (CHARLES) WEN 30 Syntax The set of rules for formulating grammatically correct C++ language statements Compiler accepts statements with correct syntax without generating error message A program statement can syntactically correct and logically incorrect Compiler will accept statement Program will produce incorrect results Style of C++ Programs Every C++ program must contain one and only one main() function Statements included within braces C++ allows flexibility in format for the word main, the parentheses (), and braces More than one statement can be put on line One statement can be written across lines Use formatting for clarity and ease of program reading PRO_00 PROF. HUNG-PIN (CHARLES) WEN 31 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 32

Standard C++ Program Form (1/2) Function name starts in column 1 Name and parentheses on their own line Opening brace of function body on next line Aligned with first letter of function name Closing brace is last line of function Aligned with opening brace Standard form highlights the function as a unit Standard C++ Program Form (2/2) Within function, indent statements 4 spaces (=1 tab) Creates uniform look for similar statement groups Good programming practice Final program form should be consistent Proper format improves program readability and understandability PRO_00 PROF. HUNG-PIN (CHARLES) WEN 33 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 34 Bad Program Format Good Program Format int main ( ) first statement; second statement; third statement; fourth statement; return 0; int main () program statements in here; return 0; PRO_00 PROF. HUNG-PIN (CHARLES) WEN 35 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 36

Comments Explanatory remarks written within program Clarify purpose of the program Describe objective of a group of statements Explain function of a single line of code Computer ignores all comments Comments exist only for convenience of reader A well-constructed program should be readable and understandable Comments help explain unclear components Comment Structure Line comment: Begins with 2 slashes(//) and continues to the end of the line Can be written on line by itself or at the end of line that contains program code // this is a line comment Block comment: Multiple line comment begins with the symbols /* and ends with the symbols */ /* This is a block comment that span across two lines */ PRO_00 PROF. HUNG-PIN (CHARLES) WEN 37 PRO_00 PROF. HUNG-PIN (CHARLES) WEN 38 Common Programming Errors (1/2) Omitting parentheses after main Omitting or incorrectly typing the opening brace Opening brace signifies start of function body Omitting or incorrectly typing the closing brace Closing brace signifies end of function Misspelling the name of an object or function Example: Typing cot instead of cout Common Programming Errors (2/2) Forgetting to close a string sent to cout with a double-quote symbol Omitting the semicolon at the end of each statement Forgetting \n to indicate a new line PRO_00 PROF. HUNG-PIN (CHARLES) WEN 39 PRO_00 PROF. HUNG-PIN(CHARLES) WEN 40

Summary (1/2) Summary (2/2) A C++ program consists of one or more modules One module must be the function main() main() is starting point of C++ program The simplest C++ program has the form: #include <iostream> using namespaces std; int main( ) program statements; return 0; PRO_00 PROF. HUNG-PIN (CHARLES) WEN 41 C++ statements are terminated by a semicolon C/C++ standard library contains many functions and classes Standard Library provided with C++ compiler Include <iostream> for input and output cout object displays text or numeric results Stream of characters is sent to cout by: Enclosing characters in double quotes Using the insertion ( put to ) operator, << PRO_00 PROF. HUNG-PIN (CHARLES) WEN 42