1 Chapter 1 Overview of Programming and Problem Solving By C.K. Liang
Introduction 2 Why should we have to learn how to program? To learn a skill To find a job To solve a problem To understand the logic Programming is about problem solving Programming is about logic reasoning Programming is Art
Can you solve? 3 Find the largest of 3 integers Find the sum of 1+2+3+ +N Find the sum of 1+3+5+ +99 Find the sum of 2+4+6+ +100 Find the sum of the first N terms of sequence 1,2,4,7,11, Find the N-th term of Fibonacci sequence 0,1,1,2,3,5, Find the GCD of two integers M and N Sort N integers into nonincreasing order Determine if the year is a leap year
4 Machine languages, Assembly languages and High-level languages Three types of computer languages 1. Machine language Only language computer directly understands Natural language of computer Defined by hardware design Machine-dependent Generally consist of strings of numbers Ultimately 0s and 1s Instruct computers to perform elementary operations One at a time Example: +1300042774 +1400593419 +1200274027
5 Machine languages, Assembly languages and High-level languages Three types of computer languages 1. Machine language 2. Assembly language 3. High-level language
6 Machine languages, Assembly languages and High-level languages Three types of computer languages 2. Assembly language English-like abbreviations representing elementary computer operations Clearer to humans Incomprehensible to computers Translator programs (assemblers) Convert to machine language Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY
7 Machine languages, Assembly languages and High-level languages Three types of computer languages 3. High-level languages Similar to everyday English, use common mathematical notations Single statements accomplish substantial tasks Assembly language requires many instructions to accomplish simple tasks Translator programs (compilers) Convert to machine language Interpreter programs Directly execute high-level language programs Example: grosspay = basepay + overtimepay
History of C 8 History of C : 1972 Evolved from two other programming languages BCPL (1967) and B (1970) Typeless languages Dennis Ritchie (Bell Laboratories) Added data typing, other features Development language of UNIX Hardware independent Portable programs 1989: ANSI standard 1990: ANSI and ISO standard published ANSI/ISO 9899: 1990
History of C++ 9 History of C++ Extension of C Early 1980s: Bjarne Stroustrup (Bell Laboratories) Spruces up C Provides capabilities for object-oriented programming Objects: reusable software components Model items in real world Object-oriented programs Easy to understand, correct and modify Hybrid language C-like style Object-oriented style Both
History of C++ 10 C++ programs Built from pieces called classes and functions C++ standard library Rich collections of existing classes and functions Building block approach to creating programs Software reuse
Java 11 Java 1991: Sun Microsystems Green project 1995: Sun Microsystems Formally announced Java at trade show Web pages with dynamic and interactive content Develop large-scale enterprise applications Enhance functionality of web servers Provide applications for consumer devices Cell phones, pagers, personal digital assistants,
Visual Basic, Visual C++, and C# 12 BASIC Beginner s All-Purpose Symbolic Instruction Code Mid-1960s: Prof. John Kemeny and Thomas Kurtz (Dartmouth College) Visual Basic 1991 Result of Microsoft Windows graphical user interface (GUI) Developed late 1980s, early 1990s Powerful features GUI, event handling, access to Win32 API, object-oriented programming, error handling Visual Basic.NET
Visual Basic, Visual C++, and C# 13 Visual C++ Microsoft s implementation of C++ Includes extensions Microsoft Foundation Classes (MFC) Common library GUI, graphics, networking, multithreading, Shared among Visual Basic, Visual C++, C#.NET platform Web-based applications Distributed to great variety of devices Cell phones, desktop computers Applications in disparate languages can communicate
Visual Basic, Visual C++, and C# 14 C# Anders Hejlsberg and Scott Wiltamuth (Microsoft) Designed specifically for.net platform Roots in C, C++ and Java Easy migration to.net Event-driven, fully object-oriented, visual programming language Integrated Development Environment (IDE) Create, run, test and debug C# programs Rapid Application Development (RAD) Language interoperability
Three Program Stages 15 myprog.c myprog.obj myprog.exe SOURCE OBJECT EXECUTABLE written in C written in machine language written in machine language via compiler via linker other code from libraries, etc.
Six Phases 16 Phases of C Programs: Editor Disk Program is created in the editor and stored on disk. 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load Preprocessor Compiler Linker Loader Disk Disk Disk Disk Primary Memory... Preprocessor program processes the code. Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Loader puts program in memory. 6. Execute CPU Primary Memory... CPU takes each instruction and executes it, possibly storing new data values as the program executes.
Computer Components 17 Central Processing Unit ( CPU ) Input Device Control Unit Arithmetic Logic Unit Output Device Memory Unit ( RAM & Registers ) Auxiliary Storage Device
How to program? 18 建議 : Do not program immediately! Try to find the logic reasoning of the problem! 步驟 : Find a solution(thinking) for the problem Formulate the thinking as an algorithm Verify the correctness of the algorithm Translate the algorithm into program
Programming Life Cycle 19 1 Problem-Solving Phase Analysis and Specification General Solution ( Algorithm Development ) 2 Implementation Phase Concrete Solution ( Program ) Test & Debug 3 Maintenance Phase Use Maintain
Sample Problem 20 Problem: A programmer needs an algorithm to determine an employee s weekly wages. How would the calculations be done by hand?
One Employee s Wages 21 During one week an employee works 52 hours at the hourly pay rate $24.75 How much is the employee s wages? Assume a 40.0 hour normal work week. Assume an overtime pay rate factor of 1.5 40 x $ 24.75 = $ 990.00 12 x 1.5 x $ 24.75 = $ 445.50 $ 1435.50
Problem-Solving Phase( 分析問題 ) 22 What information will be used? INPUT VALUE from outside the program CONSTANT VALUE assumed in program COMPUTED VALUE produced by program OUTPUT VALUE written to file or screen by program
Problem-Solving Phase( 分析問題 ) 23 INPUT CONSTANTS OUTPUT Hourly payrate Hours worked Normal work hours ( 40.0 ) Overtime pay rate factor (1.5) COMPUTED VALUE Wages Hourly payrate Hours worked Wages
Problem-Solving Phase( 分析問題 ) 24 If hours is over 40.0, then wages = (40.0 * payrate) + (hours - 40.0) * 1.5 *payrate 範例 : ( 40 x $ 24.75 ) + ( 12 x 1.5 x $ 24.75 ) = $ 1435.50 otherwise, wages = hours * payrate
拼湊出解決問題的步驟或流程 25 工具 : Flow Chart Algorithm
Flow Chart( 流程圖 ) 26 處理符號 輸入 / 輸出符號 流程符號 決策符號 結束 / 開始符號 連接符號
範例 27
範例 28 讀入整數 N 讀入 x 和 y sum=0 i=1 列印 x 和 y i N No x > y? Yes No z = y z = x 輸入 x Yes 印出 sum sum=sum+x 列印 z i=i+1
Algorithm steps( 計算步驟 ) 29 自然語言 令 如果條件則 如果條件 A 且條件 B 則 ; 否則 跳至步驟 X 讀入 印出 結束
Algorithm steps( 計算步驟 ) 30 1. 讀入 payrate 及 hours 2. 如果 hours > 40 則計算 wages = (40 * payrate) + (hours 40) * 1.5 * payrate 否則計算 wages = hours * payrate 3. 印出 wages 4. 結束
Implementation Phase:Program 31 Translate your algorithm into a programming language.
Implementation Phase: Test & Debug 32 TEST the program using sample input data for which correct results are already known, or can be manually checked. Use a variety of sample data to test many situations. If you find errors, analyze the program and algorithm to determine their source, and make corrections.
Maintenance Phase 33 USE and MODIFY the program to meet changing requirements or correct errors that show up in using it.
練習 34 輸入 N 輸入 x i=1 輸入 N S=0 i=0 i N Yes 輸入 x S=S+x No 輸出 S Yes i=i+1 y=x i y N No 輸出 i i=i+1