ReviewCh 1~2Overview of Computers and C ProgrammingDr. J.-Y. Pan Dept. Comm. Eng.Nat. Chung Cheng Univ.http://[email protected]
1-2中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Outline
• Von Neumann architecture• Computer languages• Software developing method• Applying the method• Case study:
CONVERTING MILES TO KILOMETERS
1-4中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-6中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Von Neumann architecturea definition from http://computing-dictionary.thefreedictionary.com/
• 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.
1-7中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-8中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-9中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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.
記得,要常常存檔
1-10中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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.
1-11中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-12中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
1.5 APPLYING THE SOFTWARE DEVELOPMENT METHOD
CASE STUDY
CONVERTING MILES TO KILOMETERS
1-13中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
1. 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 = 1.609 kilometers
1-14中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
3. Design (Design the algorithm to solve the problem)
– Algorithm1. Get the distance in miles2. Convert the distance to kilometers3. Display the distance in kilometers
– Algorithm with refinements1. Get the distance in miles2. Convert the distance to kilometers
2.1 The distance in kilometers is 1.609 times the distance in miles
3. Display the distance in kilometers
4. Implementation (Figure 1.14) (talks in Ch3)5. Testing
1-15中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Figure 1.14 Miles-to-Kilometers Conversion Program
Step 1
Step 2
Step 3
1-16中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Question?
• A good question deserve a good grade…
1-17中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-18中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program
1-19中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
2.1 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
1-20中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Preprocessor Directives(1/2)
• #include– gives a program access to a library
• <stdio.h>– standard header file– include printf 、 scanf
#include <stdio.h>– notify the preprocessor that some names used
in the program are found in <stdio.h>
1-21中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-22中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Function Main (2/2)
• Syntax : int
main(void)
{
/* function body */
printf(“Hello world\n”);
return(0);
}
1-23中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Reserved Words
• A word that has special meaning in C
Reserved Word Meaning
int main function returns an integer value
void main function receives no data from the operating system
double the memory cells store real numbers
return control from the main function to the operating system
• Appendix E 背起來
1-24中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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 ( 教到字串時請自學 )• 自行參考其功用,大略記一下
1-25中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-26中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Programs in memory
Figure 2.2 Memory(a) Before and (b) After Execution of a Program
1-27中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Assignment Statement
Figure 2.3 Effect of kms = KMS_PER_MILE * miles;
1-28中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Output operation: The printf functionSyntax 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> ”);
1-29中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Formatting Numbers in Program Output
• Field width – the number of columns used to display a value
Value FormatDisplayed Output
Value FormatDisplayed Output
234 %4d ▓ 234 -234 %4d -234
234 %5d ▓▓234 -234 %5d ▓ -234
234 %6d ▓▓▓234 -234 %6d ▓▓-234
234 %1d 234 -234 %2d -234
1-30中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
Formatting Values of Type double
Value FormatDisplayed Output
Value FormatDisplayed Output
3.14159 %5.2f ▓ 3.14 3.14159 %4.2f 3.14
3.14159 %3.2f 3.14 3.14159 %5.1f ▓▓ 3.1
3.14159 %5.3f 3.142 3.14159 %8.5f ▓ 3.14159
.1234 %4.2f 0.12 -.006 %4.2f -0.01
-.006 %8.3f ▓▓ -0.006 -.006 %8.5f -0.00600
-.006 %.3f -0.006 -3.14159 %.4f -3.1416
1-31中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-33中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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
1-34中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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, 課本有錯 )
Application Example
Avoiding integer division
Average = (double)total_score /
(double)num_students;
Rounding a number Rounded_x = (int)(x + 0.5)
1-35中正大學通訊工程系 潘仁義老師 Advanced Network Technology Lab
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);