C Program Design Functions and Program Structure 主講人:虞台文.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II
C Program Design Supplements 主講人:虞台文. Content Who takes the return value of main() ?
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
資料結構實習-一 參數傳遞.
 2007 Pearson Education, Inc. All rights reserved C Functions.
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Functions / Procedures
A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
C Programming A Modern Approach
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Programming Language  C Tutorial Introduction 主講人:虞台文.
1/16 Programski jezik C Vladimir Filipović
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
C Functions Pepper.
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Programmazione I a.a. 2017/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 6 - Functions Outline 5.1 Introduction
Your questions from last session
1-6 Midterm Review.
Presentation transcript:

C Program Design Functions and Program Structure 主講人:虞台文

Content Introduction Function Definitions void Functions Formal Parameters and Actual Parameters Return Statements Call-by-Value and Call-by-Reference Array Parameters Multiple Source Files and Headers String Functions Storage Classes  Global/Local Variables Extern Variables Static Variables Recursion

C Program Design Functions and Program Structure Introduction

Divide & Conquer Construct a program from smaller pieces or components – These smaller pieces are called modules Each piece more manageable than the original program

Functions Modules in C – Programs combine user-defined functions with library functions C standard library has a wide variety of functions

Function Calls

Function invocation – Provide function name and arguments (data) – Function performs operations or manipulations – Function returns results Real-life analogy: – Boss asks a specialist to complete a specific task – The specialist gets information, does the task – The specialist returns the result Information hiding: – Caller (boss) does not know details

Benefits of Functions Divide and conquer – Manageable program development Software reusability – Use existing functions as building blocks for new programs – Abstraction - hide internal details Avoid code repetition

Good Programming Practices Familiarize yourself with the rich collection of functions in the C Standard Library. Avoid reinventing the wheel. – When possible, use C Standard Library functions instead of writing new functions. – This can reduce program development time. Using the functions in the C Standard Library helps make programs more portable.

Some Library Functions C Standard Library C Stdio Library C Math Library C Stdlib Library C Time Library

Using Library Functions #include // printf() and scanf() defined #include // sqrt() defined main() { double a, b; printf("Enter two legs a and b of a right triangle:"); scanf("%lf%lf",&a,&b); printf("The hypotenuse c of the triangle is %10.5lf\n", sqrt(a * a + b * b)); } #include <stdio.h>// printf() and scanf() defined #include <math.h>// sqrt() defined main() { double a, b; printf("Enter two legs a and b of a right triangle:"); scanf("%lf%lf",&a,&b); printf("The hypotenuse c of the triangle is %10.5lf\n", sqrt(a * a + b * b)); } 畢氏定理 (Pythagorean theorem )Pythagorean theorem a 2 + b 2 = c 2 printf printf scanf sqrt

範例:亂數產生器 #include // printf() defined #include // srand(), rand() defined #include // time() defined main() { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ srand( (unsigned) time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); } #include // printf() defined #include // srand(), rand() defined #include // time() defined main() { int i; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ srand( (unsigned) time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() ); } srand rand time

練習 :[5.28] 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 100 times, and count the number of times each side of coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time for a total of approximately 50 heads and 50 tails.]

練習 :[5.32] 2. Write a C program that plays the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to The program then types: I have a number between 1 and Can you guess my number? Please type your first guess. The player then types a first guess. The program responds with one of the following: a) Excellent! You guessed the number! Would you like to play again (y or n)? b) Too low. Try again. c) Too high. Try again. If the player’s guess in incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player too high or too low to help the player “zero in” on the correct answer.

C Program Design Functions and Program Structure Function Definitions

return-type function-name( parameter-list ) { declarations statements } Function body (the detail)

Function Definitions return-type function-name( parameter-list ) { declarations statements } Function body (the detail) any valid identifier data type of the result (default int ) void – indicates that the function returns nothing data type of the result (default int ) void – indicates that the function returns nothing comma separated list, declares parameters

範例: #include double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } #include <stdio.h> double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。

範例: #include double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } #include double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。

範例: #include double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } #include double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。 double ctof(double) double average(double, int) int main() Uses ctof and average 此三函式次序可否任意對調 ? 

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include <stdio.h> // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include <stdio.h> // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。 double ctof(double); // prototype double average(double, int); // prototype

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum=0; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(double, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。 double ctof(); // prototype double average(); // prototype

練習: 1. 重新製作一 C 程式將以上範例中,華氏與攝氏 的角色互換。 2. 製作一方便操作之程式進行 MKS 制 ( 公制 ) 與 FPS 制 ( 英制 ) 間之單位轉換。參考網址參考網址

C Program Design Data Types void Functions

不傳回任何值之函式,即所謂的 Procedures.

範例: #include // forward references int readline(char[]); // prototype void welcome(char[]); // prototype #define MAX_LINE100 char userName[MAX_LINE]; // buffer for user name main() { printf("Please enter your name: "); readline(userName); // ignore returned value welcome(userName); // say hello to login user // } int readline(char strBuffer[]) { int c, count=0; while((c=getchar()) != '\n') strBuffer[count++] = c; strBuffer[count] = '\0'; // null terminated return count; // #char in the line } void welcome(char name[]) { printf("%s, you are welcome.\n", name); } #include // forward references int readline(char[]); // prototype void welcome(char[]); // prototype #define MAX_LINE100 char userName[MAX_LINE]; // buffer for user name main() { printf("Please enter your name: "); readline(userName); // ignore returned value welcome(userName); // say hello to login user // } int readline(char strBuffer[]) { int c, count=0; while((c=getchar()) != '\n') strBuffer[count++] = c; strBuffer[count] = '\0'; // null terminated return count; // #char in the line } void welcome(char name[]) { printf("%s, you are welcome.\n", name); } 用戶輸入姓名登 入後,出現歡迎 訊息。

範例: #include // forward references int readline(char[]); // prototype void welcome(char[]); // prototype #define MAX_LINE100 char userName[MAX_LINE]; // buffer for user name main() { printf("Please enter your name: "); readline(userName); // ignore returned value welcome(userName); // say hello to login user // } int readline(char strBuffer[]) { int c, count=0; while((c=getchar()) != '\n') strBuffer[count++] = c; strBuffer[count] = '\0'; // null terminated return count; // #char in the line } void welcome(char name[]) { printf("%s, you are welcome.\n", name); } #include // forward references int readline(char[]); // prototype void welcome(char[]); // prototype #define MAX_LINE100 char userName[MAX_LINE]; // buffer for user name main() { printf("Please enter your name: "); readline(userName); // ignore returned value welcome(userName); // say hello to login user // } int readline(char strBuffer[]) { int c, count=0; while((c=getchar()) != '\n') strBuffer[count++] = c; strBuffer[count] = '\0'; // null terminated return count; // #char in the line } void welcome(char name[]) { printf("%s, you are welcome.\n", name); } 用戶輸入姓名登 入後,出現歡迎 訊息。

範例: #include // forward references int readline(char[]); // prototype void welcome(char[]); // prototype #define MAX_LINE100 char userName[MAX_LINE]; // buffer for user name main() { printf("Please enter your name: "); readline(userName); // ignore returned value welcome(userName); // say hello to login user // } int readline(char strBuffer[]) { int c, count=0; while((c=getchar()) != '\n') strBuffer[count++] = c; strBuffer[count] = '\0'; // null terminated return count; // #char in the line } void welcome(char name[]) { printf("%s, you are welcome.\n", name); } #include // forward references int readline(char[]); // prototype void welcome(char[]); // prototype #define MAX_LINE100 char userName[MAX_LINE]; // buffer for user name main() { printf("Please enter your name: "); readline(userName); // ignore returned value welcome(userName); // say hello to login user // } int readline(char strBuffer[]) { int c, count=0; while((c=getchar()) != '\n') strBuffer[count++] = c; strBuffer[count] = '\0'; // null terminated return count; // #char in the line } void welcome(char name[]) { printf("%s, you are welcome.\n", name); } 用戶輸入姓名登 入後,出現歡迎 訊息。 readline() 函式有何風險?

練習 1. 執行以上程式,輸入過長之字串,觀察結果。 2. 將 readline 之 prototype 重新定義為: 其中 maxbuf 表示 buffer 的大小。 3. 更改範例程式使用新定義之 readline 函式,檢視 overflow 現象是否解除 ? int readline(char strBuffer[], int maxbuf);

練習 4. Write a function void beep(int n) which emits n audio beeps.

C Program Design Functions and Program Structure Actual Parameters and Formal Parameters

Formal Parameters and Actual Parameters Formal parameters are parameters as they appear in function declarations. Actual parameters are parameters as they appear in function calls.

範例: #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(int, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } #include // forward references double ctof(double celsius); // prototype double average(double total, int count); // prototype main() // in fact it is int main( void ) { double temperature; double faren, avg, sum; int counter = 0; while (counter < 10) { counter++; printf ("Please enter temperature %d in Celsius: ", counter); scanf("%lf",&temperature); faren = ctof(temperature); // call ctof(double) printf ("result: %.2lf C = %.2lf F\n", temperature, faren); sum += faren; } avg = average(sum, counter); // call average(int, int) printf ("average was %.2lf F\n",avg); return 0; // C compiler doesn’t check the existence of return } double ctof(double celsius) { double result; result = celsius * 9.0 / ; return result; } double average(double total, int count) { return (total / (double) count); } 輸入十個攝氏 溫度,將之轉 換成華氏溫度 後求其平均。 Formal Parameters Actual Parameters

Type Conversion  Parameter Passing #include // missing forward references main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; } #include <stdio.h> // missing forward references main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; }

Type Conversion  Parameter Passing #include // missing forward references main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; } #include // missing forward references main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; }

Type Conversion  Parameter Passing #include // forward references int fun(int); // prototypes main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; } #include // forward references int fun(int); // prototypes main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; }

Type Conversion  Parameter Passing #include // forward references int fun(int); // prototypes main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; } #include <stdio.h> // forward references int fun(int); // prototypes main() { int v1, v2, v3, v4; v1 = fun(); v2 = fun(2); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; }

Type Conversion  Parameter Passing #include // forward references int fun(int); // prototypes main() { int v1, v2, v3, v4; v1 = fun(2.0); v2 = fun(2); v3 = fun(2.5); v4 = fun1(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; } #include // forward references int fun(int); // prototypes main() { int v1, v2, v3, v4; v1 = fun(2.0); v2 = fun(2); v3 = fun(2.5); v4 = fun1(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(int a) { return 2 * a; }

Type Conversion  Parameter Passing #include // missing forward references main() { int v1, v2, v3, v4; v1 = fun(2); v2 = fun(2.0); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(double a) { return 2 * a; } #include <stdio.h> // missing forward references main() { int v1, v2, v3, v4; v1 = fun(2); v2 = fun(2.0); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(double a) { return 2 * a; }

Type Conversion  Parameter Passing #include // forward references int fun(double); // prototypes main() { int v1, v2, v3, v4; v1 = fun(2); v2 = fun(2.0); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(double a) { return 2 * a; } #include // forward references int fun(double); // prototypes main() { int v1, v2, v3, v4; v1 = fun(2); v2 = fun(2.0); v3 = fun(2.5); v4 = fun(2.5, 3); printf("v1 = %d\nv2 = %d\nv3 = %d\nv4 = %d\n", v1, v2, v3, v4); } int fun(double a) { return 2 * a; }

C Program Design Functions and Program Structure Return Statements

Return Statement return expression Conversion will be performed if necessary (can be absent) Function returns to the caller immediately after executing a return statement; or no statement remained to be executed.

More on Return Statement The return statement terminates the execution of the function body and passes control to the caller. The caller is free to ignore the returned value. Missing return statement, though improper in some cases, is not illegal – In this case, result is unpredictable

範例: #include int fun1(int), fun2(int), fun3(int), fun4(int); int main() { int m, n, o, p, q; fun1(3.0);/* caller ignores the return value */ m = fun1(2.0);/* m = 4 parameter converted to int before calling */ n = fun2(2);/* n = 1 */ o = fun2(-2);/* o = ? */ p = fun3(2);/* p = ? */ q = fun4(2);/* q = ? */ printf("m=%d\nn=%d\no=%d\np=%d\nq=%d\n", m, n, o, p, q); /* main don’t return any value */ } int fun1(int a) { return 2.0 * a; /* converted to integer before return */ } int fun2(int a) { if(a > 0) return 1; /* return nothing when a <= 0 */ } int fun3(int a) { return; /* missing expression */ } int fun4(int a) { /* do nothing */ } #include <stdio.h> int fun1(int), fun2(int), fun3(int), fun4(int); int main() { int m, n, o, p, q; fun1(3.0);/* caller ignores the return value */ m = fun1(2.0);/* m = 4 parameter converted to int before calling */ n = fun2(2);/* n = 1 */ o = fun2(-2);/* o = ? */ p = fun3(2);/* p = ? */ q = fun4(2);/* q = ? */ printf("m=%d\nn=%d\no=%d\np=%d\nq=%d\n", m, n, o, p, q); /* main don’t return any value */ } int fun1(int a) { return 2.0 * a; /* converted to integer before return */ } int fun2(int a) { if(a > 0) return 1; /* return nothing when a <= 0 */ } int fun3(int a) { return; /* missing expression */ } int fun4(int a) { /* do nothing */ }

範例: #include int fun1(int), fun2(int), fun3(int), fun4(int); int main() { int m, n, o, p, q; fun1(3.0);/* caller ignores the return value */ m = fun1(2.0);/* m = 4 parameter converted to int before calling */ n = fun2(2);/* n = 1 */ o = fun2(-2);/* o = ? */ p = fun3(2);/* p = ? */ q = fun4(2);/* q = ? */ printf("m=%d\nn=%d\no=%d\np=%d\nq=%d\n", m, n, o, p, q); /* main don’t return any value */ } int fun1(int a) { return 2.0 * a; /* converted to integer before return */ } int fun2(int a) { if(a > 0) return 1; /* return nothing when a <= 0 */ } int fun3(int a) { return; /* missing expression */ } int fun4(int a) { /* do nothing */ } #include int fun1(int), fun2(int), fun3(int), fun4(int); int main() { int m, n, o, p, q; fun1(3.0);/* caller ignores the return value */ m = fun1(2.0);/* m = 4 parameter converted to int before calling */ n = fun2(2);/* n = 1 */ o = fun2(-2);/* o = ? */ p = fun3(2);/* p = ? */ q = fun4(2);/* q = ? */ printf("m=%d\nn=%d\no=%d\np=%d\nq=%d\n", m, n, o, p, q); /* main don’t return any value */ } int fun1(int a) { return 2.0 * a; /* converted to integer before return */ } int fun2(int a) { if(a > 0) return 1; /* return nothing when a <= 0 */ } int fun3(int a) { return; /* missing expression */ } int fun4(int a) { /* do nothing */ }

C Program Design Functions and Program Structure Call-by-Value and Call-by-Reference

範例 : Power #include int power(int m, int n); main() { int n, val; n = 5; val = power(2, n); printf("val=%d n=%d\n", val, n); } /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int result; for (result = 1; n > 0; --n) result *= base; return result; } #include int power(int m, int n); main() { int n, val; n = 5; val = power(2, n); printf("val=%d n=%d\n", val, n); } /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int result; for (result = 1; n > 0; --n) result *= base; return result; } n=?

Call-by-Value vs. Call-by-Reference Call by value – Copy of argument passed to function – Changes in function do not effect original – Use when function does not need to modify argument Avoids accidental changes Call by reference – Passes original argument (in fact, the argument’s address) – Changes in function effect original – Only used with trusted functions C only has call-by-value. C++ has both.

C Program Design Functions and Program Structure Array Parameters

Call by Value In C, all function arguments are passed “by value.” – the called function is given the values of its arguments in temporary variables rather than the originals. – Modifying the values of formal parameters doesn’t effect the value of originals How about if a function has arrays (big objects) as its parameters?

範例 #include void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } #include void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } 將一正整數陣 列中之各元素 移除因子 5

範例 #include void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } #include void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } 將一正整數陣 列中之各元素 移除因子 5 Declaring array as a function’s argument. Passing array argument to a function by writing its name. const qualifier tells compiler that array cannot be changed

範例 #include void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } #include void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } 將一正整數陣 列中之各元素 移除因子 5

Declaring Function’s Prototype w/ Array Arguments void listElements(const int vals[], int size); void RemoveFactor5(int vals[], int size); void listElements(const int[], int); void RemoveFactor5(int[], int);

範例 #include void listElements(const int[], int); void RemoveFactor5(int[], int); main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } #include <stdio.h> void listElements(const int[], int); void RemoveFactor5(int[], int); main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactor5(data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } void RemoveFactor5(int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % 5 == 0 && vals[i] > 0) vals[i] /= 5; } 將一正整數陣 列中之各元素 移除因子 5

C Program Design Functions and Program Structure Multiple Source Files and Headers

Headers Header files for C Standard Library – Contain function prototypes for library functions –,, etc – Load with #include #include Custom header files – Create file with functions – Save as filename.h – Load in other files with #include "filename.h" – Reuse functions

範例 /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* IntFunctions.c */ #include <stdio.h> /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int);

範例 /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } /* main.c */ #include <stdio.h> #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); }

/* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); 範例 : Multiple Source Files /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); }

/* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); 範例 : Multiple Source Files /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); }

/* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); /* IntFunctions.h */ void listElements(const int[], int); void RemoveFactorn(int, int[], int); 範例 : Multiple Source Files /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* IntFunctions.c */ #include /* print an int array’s elements separated by space */ void listElements(const int vals[], int size) { int i; for(i=0; i<size; i++) printf("%d ", vals[i]); } /* remove factor n from all elements in an int array */ void RemoveFactorn(int n, int vals[], int size) { int i; for(i=0; i<size; i++) while(vals[i] % n == 0 && vals[i] > 0) vals[i] /= n; } /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } /* main.c */ #include #include "IntFunctions.h" main() { int data[]={12, 55, 6, 21, 35, 90, 91}; printf("Original elements in data[] are: \n"); listElements(data, sizeof(data)/sizeof(int)); RemoveFactorn(5, data, sizeof(data)/sizeof(int)); printf("\n\nRemoving factor 5, elements in data[] become:\n"); listElements(data, sizeof(data)/sizeof(int)); printf("\n"); } Practice the operations in IDE by yourselves.

C Program Design Functions and Program Structure String Functions

Strings in C Null-terminated string  In C, a string is stored as an array of characters containing the characters in the string and terminated with a ' \0 ' to mark the end. char str[]="hello\n"; Example: h (68) e (65) l (6C) o (6F) \n (0A) \0 (00) str str[0] str[1] str[2] str[3] str[4] str[5] str[6] sizeof(str) = 7

Strings in C ASCII

範例 : Print the Longest Line while (there's another line) if (it's longer than the previous longest) (save it) (save its length) print longest line while (there's another line) if (it's longer than the previous longest) (save it) (save its length) print longest line

範例 : Print the Longest Line while (there's another line) if (it's longer than the previous longest) (save it) (save its length) print longest line while (there's another line) if (it's longer than the previous longest) (save it) (save its length) print longest line Implement a getline() function which returns the length of the line read. Implement a copy() function to save string.

範例 : Print the Longest Line /* getline: read a line into s, return length */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* getline: read a line into s, return length */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; }

範例 : Print the Longest Line /* getline: read a line into s, return length */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* getline: read a line into s, return length */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } Simplification s[i++] = c;

範例 : Print the Longest Line /* getline: read a line into s, return length */ /* simplified version */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; } /* getline: read a line into s, return length */ /* simplified version */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; }

範例 : Print the Longest Line /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], const char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], const char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }

範例 : Print the Longest Line #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], const char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], const char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; }......

範例 : Print the Longest Line

練習 1. Write a C function which returns the length of a given string. The length of a string doesn’t include the null character. The function’s prototype is: int stringLength(char[]); You may need this function in the next exercise.

練習 2. Suppose that you are given two strings, say, string1 and string2. Write a function which determines whether string2 is a substring of string1. If true, the function returns the position of the first occurrence of string2 in string1. Otherwise, the function returns -1. For example, if string1 = "I am a boy." and string2 = "a bo", the function returns 5. However, if string2 = "a co", the function returns -1. Define the function’s prototype properly, and write a program to verify the function’s code.

C Program Design Functions and Program Structure Storage Classes  Global/Local Variables

Global/Local Variables Global (External) Variables – defined outside any functions – Scoping rule: accessible by any functions visible (some rules apply) – created (existent) when the program runs, and destroyed when the program terminates. Local (Private, Automatic) Variables – defined within the “top” (C++ is more flexible) of a function or a block statement – scoping rule: accessible only by the function or by the block which defines them – existent (existent) when the function is called, and destroyed when the function returns.

範例: #include // declaration of global variables int data[]={12, 24, 36, 48, 84, 63, 42, 21}; // database int numRecords = sizeof(data)/sizeof(int); // #records // find record in the database using a key // if existent, return its index; otherwise, return -1 int linearSearch(int key) { int index = 0; // local variable while(index < numRecords) if(data[index] == key) return index; else index++; return -1; // reach here if no record found } int main() { int id, index; // local variables printf("Enter an id: "); scanf("%d", &id); index = linearSearch(id); if(index >= 0) printf("Data found in entry %d with id=%d.\n", index, id); else printf("Data inexistent with id=%d.\n", id); } #include <stdio.h> // declaration of global variables int data[]={12, 24, 36, 48, 84, 63, 42, 21}; // database int numRecords = sizeof(data)/sizeof(int); // #records // find record in the database using a key // if existent, return its index; otherwise, return -1 int linearSearch(int key) { int index = 0; // local variable while(index < numRecords) if(data[index] == key) return index; else index++; return -1; // reach here if no record found } int main() { int id, index; // local variables printf("Enter an id: "); scanf("%d", &id); index = linearSearch(id); if(index >= 0) printf("Data found in entry %d with id=%d.\n", index, id); else printf("Data inexistent with id=%d.\n", id); } 於資料庫中搜 尋是否存在某 一 ID 之記錄

範例: #include // declaration of global variables int data[]={12, 24, 36, 48, 84, 63, 42, 21}; // database int numRecords = sizeof(data)/sizeof(int); // #records // find record in the database using a key // if existent, return its index; otherwise, return -1 int linearSearch(int key) { int index = 0; // local variable while(index < numRecords) if(data[index] == key) return index; else index++; return -1; // reach here if no record found } int main() { int id, index; // local variables printf("Enter an id: "); scanf("%d", &id); index = linearSearch(id); if(index >= 0) printf("Data found in entry %d with id=%d.\n", index, id); else printf("Data inexistent with id=%d.\n", id); } #include // declaration of global variables int data[]={12, 24, 36, 48, 84, 63, 42, 21}; // database int numRecords = sizeof(data)/sizeof(int); // #records // find record in the database using a key // if existent, return its index; otherwise, return -1 int linearSearch(int key) { int index = 0; // local variable while(index < numRecords) if(data[index] == key) return index; else index++; return -1; // reach here if no record found } int main() { int id, index; // local variables printf("Enter an id: "); scanf("%d", &id); index = linearSearch(id); if(index >= 0) printf("Data found in entry %d with id=%d.\n", index, id); else printf("Data inexistent with id=%d.\n", id); } 於資料庫中搜 尋是否存在某 一 ID 之記錄

範例: Scope Rule #include int i=0, j=1, k=2; main() { int i=3; printf("1: i=%d, j=%d, k=%d\n", i, j, k); { int i=4, j=5; k = 6; printf("2: i=%d, j=%d, k=%d\n", i, j, k); } printf("3: i=%d, j=%d, k=%d\n", i, j, k); } #include int i=0, j=1, k=2; main() { int i=3; printf("1: i=%d, j=%d, k=%d\n", i, j, k); { int i=4, j=5; k = 6; printf("2: i=%d, j=%d, k=%d\n", i, j, k); } printf("3: i=%d, j=%d, k=%d\n", i, j, k); }

C Program Design Functions and Program Structure Extern Variables

Local (Private) Variables #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], const char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], const char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: read a line into s, return length */ /* simplified version */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], const char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } /* getline: read a line into s, return length */ /* simplified version */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], const char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; }

Local (Private) Variables #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], const char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], const char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: read a line into s, return length */ /* simplified version */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], const char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } /* getline: read a line into s, return length */ /* simplified version */ int getline(char s[],int lim) { int c, i; for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) s[i++] = c; if (c == '\n') s[i++] = c; s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume to is big enough */ void copy(char to[], const char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } Each local variable comes into existence only when the function is called, and disappears when the function is exited. Hence, also known as automatic variables. Each local variable comes into existence only when the function is called, and disappears when the function is exited. Hence, also known as automatic variables.

Global (Extern) Variables Global variables that are defined external to all functions can be accessed by any function. Furthermore, they remain in existence permanently.

範例: Print the Longest Line #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } defined external to all functions

範例: Print the Longest Line #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } defined external to all functions /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; }

範例: Print the Longest Line #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } defined external to all functions /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; }

範例: Print the Longest Line #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } defined external to all functions /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; }

範例: Print the Longest Line #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } defined external to all functions /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } The declarations for referencing external variables are redundant if they are defined in the header part of the same file.

範例: Print the Longest Line (Multiple Source Files)  I /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* functions.c */ /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* functions.c */ /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { }

範例: Print the Longest Line (Multiple Source Files)  I /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* functions.c */ /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* functions.c */ /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* print longest input line; specialized version */ main() { int len; extern int max; extern char longest[]; max = 0; while ((len = getline()) > 0) if (len > max) { max = len; copy(); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } Must be kept.

範例: Print the Longest Line (Multiple Source Files)  II /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* functions.c */ #include #include "externdef.h" /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* functions.c */ #include <stdio.h> #include "externdef.h" /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* externdef.h */ #define MAXLINE 1000 /* maximum input line size */ extern int max; /* maximum length seen so far */ extern char line[]; /* current input line */ extern char longest[]; /* longest line saved here */ /* externdef.h */ #define MAXLINE 1000 /* maximum input line size */ extern int max; /* maximum length seen so far */ extern char line[]; /* current input line */ extern char longest[]; /* longest line saved here */

範例: Print the Longest Line (Multiple Source Files)  II /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* main.c */ #include #define MAXLINE 1000 /* maximum input line size */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ int getline(void); void copy(void); /* print longest input line; specialized version */ main() { } /* functions.c */ #include #include "externdef.h" /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* functions.c */ #include #include "externdef.h" /* getline: specialized version */ int getline(void) { } /* copy: specialized version */ void copy(void) { } /* externdef.h */ #define MAXLINE 1000 /* maximum input line size */ extern int max; /* maximum length seen so far */ extern char line[]; /* current input line */ extern char longest[]; /* longest line saved here */ /* externdef.h */ #define MAXLINE 1000 /* maximum input line size */ extern int max; /* maximum length seen so far */ extern char line[]; /* current input line */ extern char longest[]; /* longest line saved here */ /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i < MAXLINE - 1 && (c=getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; } /* copy: specialized version */ void copy(void) { int i; extern char line[], longest[]; i = 0; while ((longest[i] = line[i]) != '\0') ++i; }

C Program Design Functions and Program Structure Static Variables

Static Local Variables – continue to exist even after the function (block) in which they are defined terminates – may be initialized in their declarations Static External Variables – can be accessed as external variables only in the file in which they are defined

範例: #include #define ACC_ADD0 #define ACC_TOTAL1 #define ACC_COUNT2 int accumulator(); main() { int num; while(1){ printf("Enter a positive integer or -1 to end:"); scanf("%d", &num); if(num<0) break; accumulator(ACC_ADD, num); } printf("You have entered %d intergers. Their sum is %d.\n", accumulator(ACC_COUNT), accumulator(ACC_TOTAL)); } int accumulator(int function, int val) { static int total=0, count=0; switch(function){ case ACC_ADD: count++; total += val; case ACC_TOTAL: return total; default: return count; } #include #define ACC_ADD0 #define ACC_TOTAL1 #define ACC_COUNT2 int accumulator(); main() { int num; while(1){ printf("Enter a positive integer or -1 to end:"); scanf("%d", &num); if(num<0) break; accumulator(ACC_ADD, num); } printf("You have entered %d intergers. Their sum is %d.\n", accumulator(ACC_COUNT), accumulator(ACC_TOTAL)); } int accumulator(int function, int val) { static int total=0, count=0; switch(function){ case ACC_ADD: count++; total += val; case ACC_TOTAL: return total; default: return count; } } 累加器

範例: #include #define ACC_ADD0 #define ACC_TOTAL1 #define ACC_COUNT2 int accumulator(); main() { int num; while(1){ printf("Enter a positive integer or -1 to end:"); scanf("%d", &num); if(num<0) break; accumulator(ACC_ADD, num); } printf("You have entered %d intergers. Their sum is %d.\n", accumulator(ACC_COUNT), accumulator(ACC_TOTAL)); } int accumulator(int function, int val) { static int total=0, count=0; switch(function){ case ACC_ADD: count++; total += val; case ACC_TOTAL: return total; default: return count; } #include #define ACC_ADD0 #define ACC_TOTAL1 #define ACC_COUNT2 int accumulator(); main() { int num; while(1){ printf("Enter a positive integer or -1 to end:"); scanf("%d", &num); if(num<0) break; accumulator(ACC_ADD, num); } printf("You have entered %d intergers. Their sum is %d.\n", accumulator(ACC_COUNT), accumulator(ACC_TOTAL)); } int accumulator(int function, int val) { static int total=0, count=0; switch(function){ case ACC_ADD: count++; total += val; case ACC_TOTAL: return total; default: return count; } 累加器

範例: // file1.c static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } // file1.c static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } // file2.c static int nValue; // file scoped variable float fValue; // global variable int fun() { double dValue; // local variable } // file2.c static int nValue; // file scoped variable float fValue; // global variable int fun() { double dValue; // local variable }

範例: // file1.c static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } // file1.c static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } // file2.c static int nValue; // file scoped variable float fValue; // global variable int fun() { double dValue; // local variable } // file2.c static int nValue; // file scoped variable float fValue; // global variable int fun() { double dValue; // local variable }

範例: // file1.c static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } // file1.c static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable } // file2.c static int nValue; // file scoped variable float fValue; // global variable int fun() { double dValue; // local variable } // file2.c static int nValue; // file scoped variable float fValue; // global variable int fun() { double dValue; // local variable } extern float fValue; // global variable

C Program Design Functions and Program Structure Recursion

Factorial

範例: Factorial #include int factorial(int); main() { int n; while(1){ printf("Enter a positive integer:"); scanf("%d", &n); if(n<=0) break; printf("%d!=%d\n", n, factorial(n)); } // recursive version int factorial(int n) { if(n==1) return 1; return n * factorial(n-1); } #include int factorial(int); main() { int n; while(1){ printf("Enter a positive integer:"); scanf("%d", &n); if(n<=0) break; printf("%d!=%d\n", n, factorial(n)); } } // recursive version int factorial(int n) { if(n==1) return 1; return n * factorial(n-1); }

Greatest Common Divisor gcd(81, 534)= ?

Greatest Common Divisor gcd(81, 534)= ?= 3 = gcd(534 % 81, 81)= gcd(48, 81) = gcd(81 % 48, 48)= gcd(33, 48) = gcd(48 % 33, 33)= gcd(15, 33) = gcd(33 % 15, 15)= gcd(3, 15) 534 % 81 = % 48 = % 33 = % 15 = 3 15 % 3 = 0

範例 :GCD ( Euclidean algorithm) #include int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } int gcd(int a, int b) // non-recursive verison { int temp; while((temp = b % a) != 0){ b = a; a = temp; } return a; } #include <stdio.h> int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } } int gcd(int a, int b) // non-recursive verison { int temp; while((temp = b % a) != 0){ b = a; a = temp; } return a; } non-recursive version

範例 :GCD ( Euclidean algorithm) #include int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } int gcd(int a, int b) // non-recursive verison { int temp; while((temp = b % a) != 0){ b = a; a = temp; } return a; } #include int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } int gcd(int a, int b) // non-recursive verison { int temp; while((temp = b % a) != 0){ b = a; a = temp; } return a; } non-recursive version

範例 :GCD ( Euclidean algorithm) #include int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } int gcd(int a, int b) // recursive verison { if (b % a == 0) return a; return gcd(b % a, a); } #include <stdio.h> int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } } int gcd(int a, int b) // recursive verison { if (b % a == 0) return a; return gcd(b % a, a); } gcd(81, 534) gcd(48, 81) gcd(33, 48) gcd(15, 33) gcd(3, 15) recursive version

範例 :GCD ( Euclidean algorithm) #include int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } int gcd(int a, int b) // recursive verison { if (b % a == 0) return a; return gcd(b % a, a); } #include int gcd(int, int); main() { int a, b; while(1){ printf("Enter two non-zero & postive integers:"); scanf("%d %d", &a, &b); printf("gcd(%d, %d)=%d\n", a, b, gcd(a, b)); } int gcd(int a, int b) // recursive verison { if (b % a == 0) return a; return gcd(b % a, a); } gcd(81, 534) gcd(48, 81) gcd(33, 48) gcd(15, 33) gcd(3, 15) recursive version

A Combinatorial Problem A person has to climb 22 steps; at each point he has two choices; he can either choose to climb one step ahead or two steps ahead; you have to tell the total possible ways of climbing the steps.

A Combinatorial Problem n steps f (n) = ?

A Combinatorial Problem n steps f (n) = ? n – 1 steps f ( n – 1)

A Combinatorial Problem n steps n – 2 steps f ( n – 2) f (n) = ?

A Combinatorial Problem n steps f (n) = ? = f (n  1) + f (n  2)

A Combinatorial Problem n steps f (n) = ? = f (n  1) + f (n  2) A person has to climb 22 steps; at each point he has two choices; he can either choose to climb one step ahead or two steps ahead; you have to tell the total possible ways of climbing the steps. f (22) = f (21) + f (20) f (21) = f (20) + f (19) f (20) = f (19) + f (18) f (1) = ? f (2) = ? f (3) = f (2) + f (1) = 3 f (4) = f (3) + f (2) = f (n) = f (n  1) + f (n  2) f (2) = 1 f (1) = 1

Fibonacci Series … f (n) = f (n  1) + f (n  2) f (2) = 1 f (1) = 1

範例: Fibonacci Series f (n) = f (n  1) + f (n  2) f (2) = 1 f (1) = 1 #include int Fib(int); main() { int n; while(1){ printf("Enter a positive integer:"); scanf("%d", &n); if(n<=0) break; printf("Fib(%d)=%d\n", n, Fib(n)); } // recursive version int Fib(int n) { if(n==1 || n==2) return 1; return Fib(n-1) + Fib(n-2); } #include int Fib(int); main() { int n; while(1){ printf("Enter a positive integer:"); scanf("%d", &n); if(n<=0) break; printf("Fib(%d)=%d\n", n, Fib(n)); } } // recursive version int Fib(int n) { if(n==1 || n==2) return 1; return Fib(n-1) + Fib(n-2); }

練習 :[5.36] 1. Tower of Hanoi

練習 : 2. In combinatorial mathematics, n -choose- k is defined by which has the following recursive property: Write a C function int n_choose_k(int n, int k) which computes n -choose- k recursively. Verify your code. and

練習 : 3. Writing a recursive C function int recursiveMin(int vals[], int size) which returns the minimum value in array vals of size size. Verify your code.

練習 : 4. Writing a recursive C function void reverse(char str[]) which reverses the order of str. For example, if str="hello", after calling reverse(str), str will become "olleh", verify your code. (Hint: You may need to define another help function to facilitate your job.)