Review Ch 1~2 Overview of Computers and C Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

Slides:



Advertisements
Similar presentations
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Advertisements

ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 1: Overview of Computers & Programming
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
Software Development Method & C Language Elements H&K Chapter 1-2
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Common Programming Errors Syntax Errors (Figure 2.15) –missing semicolon at the end of the variable.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
Topic 2 – Introduction to the C Programming Language.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
CS 201 Overview of Computers & Programming Debzani Deb.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Introduction to C Programming
CIS 260 Computer Programming I in C Prof. Timothy Arndt.
CS 115 Chapter 1 Overview of Programming and Problem Solving.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
CISC105 General Computer Science Class 1 – 6/5/2006.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Input, Output, and Processing
King Saud University College of applied studies and community services CSC 1101 Computer Programming I Lecture 2.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
ELE118 Introduction to Programming
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
Chapter 1 : Overview of Computer and Programming By Suraya Alias
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
CISC105 – General Computer Science Class 2 – 6/7/2006.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture2.
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Overview of Computers and Programming Chapter 1
Basic Elements of C++.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
C++ Programming: From Problem Analysis to Program Design
Lecture2.
Basic Elements of C++ Chapter 2.
CC213 Programming Applications
Overview of Computers and C Programming
Lecture3.
Introduction to C Programming
Top-Down Design with Functions
ICS103 Programming in C 1: Overview of Computers And Programming
Computer Science II CS132/601* Lecture #C-1.
Presentation transcript:

Review Ch 1~2 Overview of Computers and C Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-2 Outline Von Neumann architecture Computer languages Software developing method Applying the method Case study: CONVERTING MILES TO KILOMETERS

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-3 Figure 1.3 Components of a Computer

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-4 Memory Memory cell –an individual storage location Address –relative position in memory Contents –the data stored in a memory cell Stored program concept –an ability to store program instructions in main memory for execution –We can change the computer’s operation by storing a different program in memory

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-5 Figure Memory Cells in Main Memory

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-6 Von Neumann architecture a definition from Has a random-access memory (RAM) –which means that each successive operation can read or write any memory location, independent of the previous access. Has a central processing unit (CPU) –The CPU has a set of registers and a set of built-in operations (its instruction set). –The CPU can interpret the contents of memory either as instructions or as data according to the fetch-execute cycle.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-7 Central Processing Unit Retrieves each instruction in sequence (called fetching an instruction) Interprets the instruction to determine what should be done and then retrieves any data needed to carry out that instruction Performs the actual manipulation, or processing, of the data it retrieved Store the results in main memory

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-8 Computer languages Software developers rarely write in the language directly understood by a computer –The machine language is a collection of binary numbers, not easy to understand by human –It is not standardized, different for different CPU Hence, write in High-level languages –That combine algebraic expressions and symbols taken from English –Computers do NOT understand it… –Translation… By a compiler

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-9 Figure 1.12 Entering, Translating, and Running a High-Level Language Program The loader must copy all its instructions into memory and direct the CPU to begin execution with the first instruction. 記得,要常常存檔

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-10 Figure 1.13 Flow of Information During Program Execution As the program executes, it takes input data from one or more sources and sends results to output and/or secondary storage devices. Although the instructions normally are executed in sequence, it is possible to have the CPU skip over some instructions or execute some instructions more than once.

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-11 Software development method Specify the problem requirements –State the problem clearly and gain a clear understanding of what is required for its solutions Analyze the problem –Identify the problem Input/Output/Extra_requirements Design the algorithm to solve the problem –Develop a list of steps (called algorithm) to solve it Implement the algorithm –Convert each algorithm step into statements in C Test and verify the completed program Maintain and update the program –Modify a program to remove previously undetected errors and to keep it up-to-date

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab APPLYING THE SOFTWARE DEVELOPMENT METHOD CASE STUDY CONVERTING MILES TO KILOMETERS

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Problem (Specify the problem requirements) –Your summer surveying job requires you to study some maps that gives distances in kilometers and some use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion. 2.Analysis (Analyze the problem) –Purpose : Conversion from miles to kilometers. –To solve this problem, you need to know the relationship between miles and kilometers. –Data Requirements Problem input : miles /* The distances in miles */ Problem output : kms /* The distances in kilometers */ Relevant Formula : 1 mile = kilometers

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab Design (Design the algorithm to solve the problem) –Algorithm 1.Get the distance in miles 2.Convert the distance to kilometers 3.Display the distance in kilometers –Algorithm with refinements 1.Get the distance in miles 2.Convert the distance to kilometers 2.1 The distance in kilometers is times the distance in miles 3.Display the distance in kilometers 4.Implementation (Figure 1.14) (talks in Ch3) 5.Testing

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-15 Figure 1.14 Miles-to-Kilometers Conversion Program Step 1 Step 2 Step 3

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-16 Question? A good question deserve a good grade…

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-17 Outline (Part 2) C language elements A running program, what happens? Syntax and semantics –Input and output –Arithmetic operators Program-Controlled Input and Output Files

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-18 Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab C Language Elements Preprocessor –a system program that modifies the text of a C program before it is compiled Preprocessor directives –commands that provides instructions to the C preprocessor Library –a collection of useful functions and symbols that may be accessed by a program –each library has a standard header file

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-20 Preprocessor Directives(1/2) #include –gives a program access to a library –standard header file –include printf 、 scanf  #include –notify the preprocessor that some names used in the program are found in

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-21 Function Main(1/2) Contains two parts Part 1: Declarations –the part of a program that tells the compiler the names of memory cells in a program Part 2: Executable statements –program lines that are converted to machine language instructions and executed by the computer

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-22 Function Main (2/2) Syntax : int main(void) { /* function body */ printf(“Hello world\n”); return(0); }

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-23 Reserved Words A word that has special meaning in C Reserved WordMeaning intmain function returns an integer value voidmain function receives no data from the operating system doublethe memory cells store real numbers returncontrol from the main function to the operating system Appendix E  背起來

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-24 Standard Identifiers A word having special meaning but one that a programmer may redefine –In Figure 2.1, the standard identifiers printf and scanf are names of operations defines in the standard input/output library. –Appendix B shows ANSI C standard libraries P.800~801, stdio.h 所列之 functions P.799, math.h 所列之 functions P.802, string.h 所列之 functions ( 教到字串時請自學 ) 自行參考其功用,大略記一下

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-25 User-Define Identifiers Syntax rules –An identifier must consist only of letters, digits, and underscores –An identifier cannot begin with a digit –A C reserved word cannot be used as an identifier –An identifier defined in a C standard library should not be redefined 大小寫不一樣 –Rate, rate, RATE –Wildly adopted in industry uses all uppercase letters in the names of constant macros

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-26 Programs in memory Figure 2.2 Memory(a) Before and (b) After Execution of a Program

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-27 Assignment Statement Figure 2.3 Effect of kms = KMS_PER_MILE * miles;

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-28 Output operation: The printf function Syntax Display for Function Call Syntax: –printf(format string, print list); –printf(format string); Examples: –printf(“I am %d years old, and my gpa is %f\n”, age, gpa); –printf(“Enter the object mass in grams> ”);

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-29 Formatting Numbers in Program Output Field width –the number of columns used to display a value ValueFormat Displayed Output ValueFormat Displayed Output 234%4d▓ %4d %5d▓▓ %5d▓ %6d▓▓▓ %6d▓▓ %1d %2d-234

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-30 Formatting Values of Type double ValueFormat Displayed Output ValueFormat Displayed Output %5.2f▓ %4.2f %3.2f %5.1f▓▓ %5.3f %8.5f▓ %4.2f %4.2f %8.3f▓▓ %8.5f %.3f %.4f

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-31 Input operation: The scanf Functon scanf(“%lf”, & miles); (Figure 2.5) scanf(“%c%c%c”, & letter_1, & letter_2, & letter_3); (Figure 2.6) & –The C address-off operator –Tell the scanf function where to find each variable into which it is to store a new value Beware the difference between SCANF and PRINTF in input arguments

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-32 Figure 2.5 Effect of scanf("%lf", &miles);

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-33 Arithmetic operators, Appendix C, 背優先權 Category Operator Associativity Postfix () [] -> Left to right Unary + - ! ~ (type) * & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift > Left to right Relational >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left Comma, Left to right

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-34 Type Conversion through Casts type cast –converting an expression to a different type by writing the desired type in parentheses in front of the expression –Table 2.9 (Page 63, 課本有錯 ) ApplicationExample Avoiding integer division Average = (double)total_score / (double)num_students; Rounding a numberRounded_x = (int)(x + 0.5)

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-35 Program-Controlled Input and Output Files declare a file pointer variable –FILE *inp, /* pointer to input file */ *outp ; /* pointer to output file */ the calls to function fopen –inp = fopen(“b:distance.dat”, “r” ) ; –outp = fopen(“b:distance.out”, “w”) ; use of the functions – fscanf(inp, “%lf”, &miles); – fprintf(outp, “The distance in miles is %.2f. \n”, miles); end of use – fclose(inp); – fclose(outp);

中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab 1-36 Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files