dr. yang, qingxiong (with slides borrowed from dr. yuen, joe) cs2311 computer programming lt1:...

37
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Upload: verity-green

Post on 30-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Dr. Yang, Qingxiong(with slides borrowed from Dr. Yuen, Joe)

CS2311 Computer Programming

LT1: Introduction to Programming

Page 2: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

What is a Computer

3

Secondary Storage

Memory

CPU (Central Processing Unit)

Motherboard

Page 3: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Stored Program Computer (Von Neumann Machines)

4

CPU

Main Memory

Input Unit Output Unit

Secondary Storage

Page 4: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Personal Computer

5

Input Unit

Output Unit

Input Unit: Get input from user or external environment

Output Unit: Show result to user or other programs

Page 5: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Personal Computer

6

Secondary Storage: (Slow) storage of program and data files (e.g., maintain them after shutting down the computer).

Main Memory: fast storage of program and data in action

CPU (Central Processing Unit): Read instruction from main memory and execute the instruction. Update main memory value or send instruction to motherboard

Secondary Storage

Page 6: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

What is a Computer Program?

A list of instructions that instructs a computer to do some tasks

1. Turn on

2. Set Channel to ch01

3. Set Date to 1/5/2009

4. Set Time to 3:00am

5. Confirm setting

Timer Recording

int x=10;

int y=11;

y+=x;

System.out.println(y);

System.out.println(x);

Program X

7

Page 7: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

A Computer Program

8

A way to communicates with computersWritten in Programming Language

Page 8: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Programming LanguagesTo write a program for a computer, we must use a

computer language.

9

Machine LanguageLanguage directly understood by the computer

binary code

Page 9: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

10

The Multiplication Program in Machine LanguagePROGRAM 1-1

The only language understood by computer hardware is machine language.

Page 10: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Programming LanguagesTo write a program for a computer, we must use a

computer language.

11

Machine LanguageLanguage directly understood by the computer

Symbolic LanguageEnglish-like abbreviations representing elementary computer operations

binary code assembly language

Page 11: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

12

PROGRAM 1-2 The Multiplication Program in Symbolic Language

Symbolic language uses symbols, or mnemonics, to represent the various machine language instructions.

Page 12: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Programming LanguagesTo write a program for a computer, we must use a

computer language.

13

Machine LanguageLanguage directly understood by the computer

Symbolic LanguageEnglish-like abbreviations representing elementary computer operations

High-level LanguageClose to human language.Example: a = a + b[add values of a and b, and store the result in a, replacing the previous value]

binary code assembly language C, C++, Java, Basic

Page 13: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

14

PROGRAM 1-3 The Multiplication Program in C

high-level languages are easier for us to

understand.

Page 14: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Building a C++ program Writing source code as an C++ file.

e.g. “hello.cpp” file Preprocessing

Processes the source code for compilation.

Compilation Checks the grammatical rules

(syntax). Source code is converted to object

code in machine language (e.g. “hello.obj” file)

Linking Combines object code and libraries

to create an executable (e.g. “hello.exe” file).

Library: common functions (input, output, math, etc).

15

Page 15: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

There are many Programming Languages in the world!!

16

ActionScript Ada ASP.NET Assembler Basic

C C++ C# Cobol Cobra CODE ColdFusion

Delphi Eiffel Fortran FoxPro GPSS HTML J#

J++ Java JavaScript JSP LISP Logo LUA MEL Modula-2 Miranda Objective-C Perl PHP Prolog Python SQL Visual Basic Visual Basic.NET VBA Visual-FoxPro

Page 16: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Programming Languages

17

Programming languages usually differ in two aspectsLanguage SyntaxStandard libraries/SDKs/functions

Javaif (a>b){

System.out.println(“a is larger than b”);}else{

System.out.println(“a is smaller than or equal to b”);}

Pascalif a>b then

writeln(‘a is larger than b’);else

writeln(‘a is smaller than or equal to b’);

Page 17: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Programming Languages

18

Syntax is well-defined, no exceptionif (…){…}else{…}; for (;;;){…}while (){…}

Basic Components:Variable / structure /function declaration Variable / structure /function accessConditional statementIteration statementSDK/built-in functions

Page 18: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Programmer

19

Page 19: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Some Difficulties

20

Computer only follows instructions. It won’t solve problems by itself

Programmer needs to:

1. develop an appropriate solution (logic)

2. express the solution in programming language (implementation)

3. validate the logic and implementation (testing)

Page 20: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Requirements

21

Correct syntax Correct logicEfficientRunning properly under various constraintsScalability, Maintainability Platform independent

Page 21: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Basic Concept of Programming

22

Page 22: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Computer Program (External View)

23

Basic elements of a Computer ProgramInputProcessOutput

Input Process Output

Page 23: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Computer Program (Internal View)

24

Logic Flow

Computer Program

Instructions Data

• A list of instructions ordered logically

• Usually involve data access

Page 24: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Computer Program

25

Instructions A set of predefined action that a computer can perform E.g. addition, subtraction, read , write

Logic Flow Arrangement of Instructions E.g. Calculate BMI (Body Mass Index)

1. Read weight from keyboard2. Read height from keyboard3. Weight x weight/height4. Write BMI to screen

Variable (data) A space for temporarily store value for future process

Constant (data) A value that will not be changed for the whole processing

Page 25: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Logic Flow

26

Turn on the mobile

Key in xxxx - xxxx

Wait for connection

Talking

Hand offTurn on the mobile

Key in xxxx - xxxx

Wait for connection

Talking

Hang up

Page 26: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Logic Flow

27

Turn on the mobile

Key in xxxx - xxxx

Wait for connection

Talking

Hang up

Connection made?

Yes No

Turn on the mobile

Key in xxxx - xxxx

Wait for connection

Talking

Hang up

Connection made?

Yes

No

Page 27: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Sample Program (Framework)

#include <iostream>using namespace std;void main(){ count << “Hello World!\n”;}

Compiler/linker

Plain Text

0101011101111101011010101010100110101010101011010101010101010101010100001101010110101001011010101010101011001010101010101010110110101110101010110010110101000000

Binary Code

28

Page 28: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Simple Program

29

/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */

#include <iostream>using namespace std;

void main(){ cout << "Hello, world!\n";}

Page 29: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Simple Program

30

/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */

Enclosed by “/*” and “*/” or begin with “//”// single line comments

// this is a single line comment// each line must begin with “//” sign

Page 30: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Preprocessor directive

31

Give information / instruction to compiler for program creation

#include <iostream>

Preprocessor directive Tells computer to load contents of a certain file / library In this program, we include the library iostream into the program

as it contains the definition of coutwhich is used to print something to the screen.

No semi-colon at the end of the include directive

using namespace std

Specifying that the standard (std) namespace is used such that we can use a shorthand name for the object coutstd::cout <-> cout

Page 31: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Functions

32

When writting program, programmer usually group related code (instructions) into functions for easy design and maintenance.

We will talk about function and how to write your own function in later lectures.

Page 32: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Function - main

void main(){

}The starting point of program (the first function called by the

computer)

main()

main()

main()

Example1.exe

Calculator.exe

texteditor.exe

33

Page 33: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Function - main

34

void main()main is the name of the functionvoid means there is no return valueno semi-colon after main()C++ is case sensitive:E.g., void Main(), VOID main() are incorrect

{ }Braces: left brace begins the body of a function. The

corresponding right brace must end the function

Page 34: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Simple Program

35

/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972 */

#include <iostream>using namespace std;void main(){ cout << "Hello, world!\n”;}

Page 35: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Library / SDK /Package

36

Normally, we won’t write a program all by ourselves. Instead, we will reuse the code written by ourselves / other developers. Especially for the repeating tasks or low-level operation like disk I/O.

The reusing code is well designed and pack a library / SDK / Package.

Standard C++ program comes with a set of package to make programmer task easier.

cout is one of the example.

Page 36: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Object - cout

37

cout << "Hello, world!\n“ ;

Object is a programming unit that store values (attributes) and provide functions (methods) to manipulate the values (we will elaborate this concept in future classes)

cout: object provided by iostream library (package) for screen (console) output

<<: output (also called insertion) operator that output values to an output device. In this case, the output device is cout (the screen)

The value on the right hand side of the operator ("Hello, world!\n“ ) is the string you want to output

Page 37: Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) CS2311 Computer Programming LT1: Introduction to Programming

Object - cout

38

\nescape sequence: the character following \ is not interpreted in

the normal way

represents a newline character: the effect is to advance the cursor on the screen to the beginning of the next line

newline: position the character to the beginning of next line \\

backslash: Insert the backslash character \ in a string \"

double quote: Insert the double quote character " in a string