1 Chapter Ten Modular Development. 2 Stepwise Refinement At first, simple programs consist of only one source file and one function (the function main.

Slides:



Advertisements
Similar presentations
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Advertisements

Modular Programming With Functions
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Procedural programming in Java
Chapter 4 Function By C. Shing ITEC Dept Radford University.
1 Chapter Nine Characters and Strings. 2 Text Data These days, computers work less with numeric data than with text data To unlock the full power of text.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
String Processing Eric Roberts CS 106A February 3, 2010.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 8 Characters and Strings. Principle of enumeration Computers tend to be good at working with numeric data. The ability to represent an integer.
CSC204 – Programming I Lecture 4 August 28, 2002.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
String Manipulation. Java String class  The String class represents character strings “Tammy Bailey”  All strings (arrays of characters) in Java programs.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 8: Simple Data Types, Namespaces, and the string Type.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
String Processing Eric Roberts CS 106A February 1, 2016.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Lesson #6 Modular Programming and Functions.
Jerry Cain and Eric Roberts
Chapter 2 - Introduction to C Programming
Lesson #6 Modular Programming and Functions.
Chapter 2 - Introduction to C Programming
Lesson #6 Modular Programming and Functions.
Structured Program
10.1 Character Testing.
Chapter 3 – Introduction to C# Programming
slides courtesy of Eric Roberts and Jerry Cain
C Programming Getting started Variables Basic C operators Conditionals
Lesson #6 Modular Programming and Functions.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Presentation transcript:

1 Chapter Ten Modular Development

2 Stepwise Refinement At first, simple programs consist of only one source file and one function (the function main ) Then, larger programs consist of only one source file, while it may contain several functions At last, complex programs may consist of several source files and each file contains several functions

3 Modules Each source file in a complex program is called a module The module containing the function main is called the main module Each of the other modules acts as a library for the main module

4 Modules When dividing a program into modules, it is important to limit the extent to which the modules depend on each other Each module should be carefully designed so that it can be reused in many different programs

5 A Case Study – Pig Latin Pig Latin is an invented language formed by transforming each English word based on: If the word begins with a vowel, add the suffix way If the word begins with a consonant, move the initial consonant string to the end and add the suffix ay

6 An Example apple  appleway scram  amscray Enter a line: this is pig latin. isthay isway igpay atinlay.

7 To-Down Design With top-down design, you successively solve a problem by dividing it into a series of simpler problems It is usually easier to write these simpler problems in English first Programs that consist of a mixture of English and C code are said to be written in psudocode

8 An Example main() { Read in a line of text from the user. Translate the line of text into Pig Latin. }

9 An Example main() { string line; printf(“Enter a line: ”); line = getLine(); Translate the line of text into Pig Latin. }

10 An Example main() { string line; printf(“Enter a line: ”); line = getLine(); translateLine(line); }

11 An Example void translateLine(string line) { Divide the line into words. Translate each word into Pig Latin. Display each translated word. Display a newline to complete the line. }

12 An Example void translateLine(string line) { while (there are any words left on the line) { Get the next word. Translate that word into Pig Latin. Display the translated word. } Display a newline to complete the line. }

13 Spaces and Punctuation Enter a line: this is pig latin. isthayiswayigpayatinlay

14 Refining the Definition of a Word A sequence of characters that acts as a coherent unit is called a token this is pig latin. 4 words 8 words

15 An Example void translateLine(string line) { while (there are any tokens left on the line) { Get the next token. if (the token is a regular English word) { Replace the token by is Pig Latin translation. } Display the token. } Display a newline to complete the line. }

16 Token Scanning The division of the input text into a sequence of tokens is called token scanning Token scanning occurs in all language processing applications, e.g., compilers and WWW browser. Hence, it is suitable to be designed as a separate module

17 The Token Scanner The token scanner needs two functions –atEndOfLine( ) –getNextToken( ) How could getNextToken return different tokens each time it is called? –maintain the internal state within the module –the drawLine function in graphics library –the rand function in stdlib library

18 The Token Scanner void initScanner(string line); string getNextToken(void); bool atEndOfLine(void);

19 An Example void translateLine(string line) { string token; initScanner(line); while (!atEndOfLine( )) { token = getNextToken( ); if (isLegalWord(token)) token = translateWord(token); printf(“%s”, token); } printf(“\n”); }

20 An Example bool isLegalWord(string token) { int i; for (i = 0; i < stringLength(token); i++) { if (!isalpha((ithChar(token, i))) return FALSE; } return TRUE; }

21 An Example string translateWord(string word) { Find the position of the first vowel. if (the vowel appears at the beginning of the word) { return the word concatenated with “way”. } else { head = extract the initial consonant substring. tail = extract the rest substring. Return the tail, concatenated with head, concatenated with “ay”. }

22 An Example string translateWord(string word) { int vp; string head, tail; vp = findFirstVowel(word); if (vp == -1) { return word; } else if (vp == 0) { return concat(word, “way”); } else { head = subString(word, 0, vp – 1); tail = subString(word, vp, stringLength(word) – 1); return concat(tail, concat(head, “ay”)); }

23 An Example int findFirstVowel(string word) { int i; for (i = 0; i < stringLength(word); i++) { if (isVowel((ithChar(word, i))) return i; } return -1; }

24 An Example bool isVowel(char ch) { switch (tolower(ch)) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: return TRUE; default: return FALSE; }

25 Local Variables Variables declared inside a function are called local variables The memory locations for local variables exist when the function is called, and vanish when the function is returned These memory locations are created in a region called the stack area

26 An Example int fact(int m) { int product, i; product = 1; for (i = 1; i <= m; i++) product *= i; return product; } int comb(int n, int k) { return fact(n) / (fact(k) * fact(n – k); main( ) { int n, k; scanf(“%d %d”, &n, &k); printf(“C(%d, %d) = %d\n”, n, k, comb(n, k)); }

27 An Example combmainfact m, i n, k knkn knkn knkn knkn knkn imim

28 Global Variables Variables declared outside functions are called global variables The memory locations for global variables exist when the program starts, and vanish when the program terminates These memory locations are created in a region called the static area

29 An Example int n = 1; int genNumber(void) { return n++; } main() { int i; for (i = 0; i < 10; i++) { printf(“%d\n”, getNumber()); }

30 An Example Static Area: n Stack Area: i Data section for a program:

31 Scope of Variables The portion of the program in which a variable can be used is called the scope of the variable A local variable can only be used within a single function A global variable can be used within all functions in the program

32 The Dangers of Global Variables Parameter passing is one way to communicate information between functions –Access of information is under better control Using global variables is another way to communicate information between functions –Access of information may be out of control Global variables should be used carefully

33 Keep Global Variables Private It is best to ensure that each global variable is never used by more than one module A global variable can be declared as private to a single module using the keyword static static int n;

34 Initialization of Global Variables A global variable can be assigned an initial value before program starts. This is called static initialization static int n = 1; A global variable can also be assigned an initial value after program starts. This is called dynamic initialization initScanner(line);

35 Cases for Static Initialization If the value of a variable is constant throughout the lifetime of the program If a variable usually has a particular initial value that only a few clients might want to change Values that are used within a program unless a client specifically changes them are called default values

36 Private Functions The keyword static can also be used to indicate that a function is private to a particular module All functions other than main and those explicitly exported by an interface should be declared as static

37 pigLatin.c static void translateLine(string line); static string translateWord(string word); static bool isLegalWord(string token); static int findFirstVowel(string word); static bool isVowel(char ch);

38 The Token Scanner static string buffer; static int buflen; static int cpos;

39 initScanner void initScanner(string line) { buffer = line; buflen = stringLength(buffer); cpos = 0; }

40 atEndOfLine bool atEndOfLine(void) { return cpos >= buflen; }

41 getNextToken string getNextToken(void) { char ch; int start; if (cpos >= buflen) { printf(“Error: No more tokens”); exit(1); } ch = ithChar(buffer, cpos); /* next page */ }

42 getNextToken if (isalnum(ch)) { start = cpos; while (cpos < buflen && isalnum(ithChar(buffer,cpos))) { cpos++; } return subString(buffer, start, cpos – 1); } else { cpos++; return charToString(ch); }

43 Summary /* xxx.h */ f1(); f2(); f3(); /* xxx.c */ static int v1; static int v2; f1() { …} f2() { …} f3() { …} static h1() { …} static h2() { …}