Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Chapter 1 Programming & Programs.

Similar presentations


Presentation on theme: "C++ Programming Chapter 1 Programming & Programs."— Presentation transcript:

1

2 C++ Programming Chapter 1 Programming & Programs

3 Chap01-2/17 1.1 Program & algorithm What’s program? –Example in life game sourcecode sourcecode –A sequence of instructions –program = algorithm + data structure + programming methodology +languages How to describe algorithm 1.Natural language 2.Flowchart 3.Pseudocode: using a combination of natural language and programming language. A step-by-step procedure for solving a problem in a finite amount of time

4 Chap01-3/17 Example in Pseudo-code For a given value, Limit, what is the smallest positive integer Number for which the sum Sum = 1 + 2 + 3 +... + Number Sum = 1 + 2 + 3 +... + Number is greater than Limit. What is the value for this Sum ? Pseudocode: Input: An integer Limit Ouput: Two integers: Number and Sum Input: An integer Limit Ouput: Two integers: Number and Sum 1. Enter Limit 2. Set Number = 0. 3. Set Sum = 0. 4. Repeat the following: a. If Sum > Limit, terminate the repitition, otherwise. b. Increment Number by one. c. Add Number to Sum and set equal to Sum. 5. Print Number and Sum. Pseudocode: Input: An integer Limit Ouput: Two integers: Number and Sum Input: An integer Limit Ouput: Two integers: Number and Sum 1. Enter Limit 2. Set Number = 0. 3. Set Sum = 0. 4. Repeat the following: a. If Sum > Limit, terminate the repitition, otherwise. b. Increment Number by one. c. Add Number to Sum and set equal to Sum. 5. Print Number and Sum.

5 Chap01-4/17 1.2 Programming language-1 Machine Code ---- The first generation language ( about in 1946 ) – M ade up of binary-coded instructions Machine Code Meaning 10 20 load value from unit 20 to register A 30 21 value of register A add value of unit 21 and put sum to register A 20 22 store value of register A to unit 22 00 00 program end

6 Chap01-5/17 1.2 Programming language-2 Assembler ------the second generation language(early of 1950s) –Allow for the use of symbolic names instead of just numbers,such as load, store, add,etc. –Each new machine had its own assembler AssemblerMeaning LOAD X LOAD X load value from X load value from X ADD Y ADD Y X add Y X add Y STORE SUM STORE SUM store value to Sum store value to Sum HALT HALT program end program end

7 Chap01-6/17 Higher level language ------The third generation language (middle of 1950s) –Fortran born in 1956 –a classification by decribing problem, including procedural language,functional language, declarative language,OOP. TypeCharacteristic Classic Language procedual language step-by-step procedure FORTRAN,COBOL,ALGOL,BASIC,C, PASCAL,Ada,APL functional language composed by functionLISP,ML declarative language declarative language decribe what’s problemPROLOG,GPSS OOP based on object Smalltalk,C++,Visual Basic,Java 1.2 Programming language-3

8 Chap01-7/17 5254565860626466687072747678808284868890929496 Ada ALGOL60ALGOL68 Pascal Modula-2 CPLBCPLB C C++ Java LISP PROLOG COBOL FORTRAN77 FORTRAN PL/1 Simula 67 Smalltalk 80 BASIC ANSI-BASIC QBASIC VB FORTRAN90

9 Chap01-8/17 the fourth generation language ( 1980s ) –a programming language or programming environment designed with a specific purpose in mind, such as the development of commercial business software. –Including: SQL, report-generator, GUI creators, application generator, more higher level language, application software package etc. Example : SELECT name,department,teaching_age FROM d:\zg.dbf WHERE teaching_age >=30 1.2 Programming language-4

10 Chap01-9/17 Types of program –Source program: a program written in advanced programming language. –Object program: the machine language version of a source program –Compiler: a program that translates an advanced programming language into machine language. 1.2 Programming language-5

11 Chap01-10/17 C++ Language history 1.2 Programming language-6 CPLBCPLB C99 C++ C89 Classic C C++98 Christopher Strachey, Cambridge, mid-1960s Martin Richards, Cambridge, 1967 Ken Thompson, BTL, 1972 Dennis Ritchie, BTL, 1974 C++0x Bjarne Stroustrup, BTL, 1985

12 Chap01-11/17 //1-1.cpp The first C++ Program 1#include 2using namespace std; 3 int main( ) 4 { 5cout<<“Hello, World!\n”; 6 7 return 0; 8 } //get the library facilities needed for now // main() is where a C++ program starts //function begin //function end Example 1-1 The first program Hello, World! 1.3 Basic Framework of C++ Program -1 // output the 13 characters Hello, world! // followed by a new line // return a value indicating success //comment

13 Chap01-12/17 1.3 Basic Framework of C++ Program -2 //1-2.cpp 1 #include 1 #include 2 #include 2 #include 3 using namespace std; 4 int main( ) 5 { 6 float a,b; 7 b=30.0; 8 a=sin(b*3.14159/180); 9 cout<< “ a= “ <<a<<endl; 10 return 0; 11 } //Define variable //assign value to b //assign value calculating sin to a

14 Chap01-13/17 1.3 Basic Framework of C++ Program -3 Pre-process command int main( ) { //function begin declarations; //define variable declarations; //define variable executions ; //input,assignment, calculate, control,output etc. executions ; //input,assignment, calculate, control,output etc. return 0; return 0; } //function end

15 Chap01-14/17 –Function and main function A C++ program is a collection of one function or more functions, each function performs some particular task.A C++ program is a collection of one function or more functions, each function performs some particular task. Every C++ program must have a function named main.Every C++ program must have a function named main. Excecution of the program always begins with the main function. You can think of main as the master and the other functions as the servants.Excecution of the program always begins with the main function. You can think of main as the master and the other functions as the servants. –Program statement A C++ program is composed by statements;A C++ program is composed by statements; using “;” as ending of statementusing “;” as ending of statement –Comment Using “//” as commentUsing “//” as comment don’t generate compiling codedon’t generate compiling code 1.3 Basic Framework of C++ Program – Summary

16 Chap01-15/17 1.4 Compilation and linking You write C++ source code –Source code is (in principle) human readable The compiler translates what you wrote into object code (sometimes called machine code) –Object code is simple enough for a computer to “understand” The linker links your code to system code needed to execute –E.g. input/output libraries, operating system code, and windowing code The result is an executable program –E.g. a.exe file on windows or an a.out file on Unix C++ compiler C++ source code Object code linker Executable program Library Object code

17 Chap01-16/17 Key Points To master what’s program. To understand what’s algorithm. To undestand what’s framework of a C++ program. To understand how to translate into machine code.

18 Chap01-17/17 Exercises 1 、 Write a program to output your code and your name.


Download ppt "C++ Programming Chapter 1 Programming & Programs."

Similar presentations


Ads by Google