Modular Programming. Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
C Language.
CSE 303 Lecture 16 Multi-file (larger) programs
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guide To UNIX Using Linux Third Edition
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Unix Makefiles COP 3330 Lecture Notes Dr. David A. Gaitros.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
리눅스 : Lecture 5 UNIX 유틸리티 : text editor, compilation (make), …
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
The Java Programming Language
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Learners Support Publications Classes and Objects.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Programming Practice (6) - Sorting Algorithms
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Separate Compilation make and makefiles
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Make Make is a system utility that automatically compiles your programs for you Make looks for a file named Makefile (or makefile) in the current directory.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
The make utility (original presentation courtesy of Alark Joshi)
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Objectives Identify the built-in data types in C++
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Makefile Tutorial CIS5027 Prof: Dr. Shu-Ching Chen
Chapter 5 - Functions Outline 5.1 Introduction
Homework K&R chapter 4.
Compiler vs linker The compiler translates one .c file into a .o file
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS1 Introduction to C++.
Presentation transcript:

Modular Programming

Introduction As programs grow larger and larger, it is more desirable to split them into sections or modules. C allows programs to be split into multiple files, compiled separately, and then combined (linked) to form a single program. we will go through a programming example, discussing the C techniques needed to create good modules. You will be shown how to use make to put these modules together to form a pro gram.

Modules A module is a collection of functions that perform related tasks. database functions such as lookup, enter, and sort. An efficient way of splitting up a large project is to assign each programmer a different module. In this manner, each programmer only worries about the internal details of a particular module. Modules are divided into two parts public and private. The public part how to call the functions in the module. It contains the definition of data structures and functions that are to be used outside the module. The private part Anything that is internal to the module is private. Everything that is not directly usable by the outside world should be kept private.

Definition, implementation, and use of the module

The extern Modifier The extern modifier is used to indicate that a variable or function is defined outside the current file. #include /* number of times through the loop */ extern int counter; /* routine to increment the counter */ extern void inc_counter(void); main() { int index; /* loop index */ for (index = 0; index < 10; index++) inc_counter(); printf("Counter is %d\n", counter); return (0); } /* number of times through the loop */ int counter = 0; /* trivial example */ void inc_counter(void) { ++counter; }

Modifiers

Headers Information that is shared between modules should be put in a header file. By convention, all header filenames end with.h. The header should contain all the public information, such as: A comment section describing clearly what the module does and what is available to the user. Common constants. Common structures. Prototypes of all the public functions. extern declarations for public variables.

/******************************************************** * Definitions for the infinite array (ia) package. * * * * An infinite array is an array whose size can grow * * as needed. Adding more elements to the array * * will just cause it to grow. * * * * struct infinite_array Used to hold the information for an infinite * * array. * * * * Routines * * * * ia_init -- Initializes the array. * * ia_store -- Stores an element in the array. * * ia_get -- Gets an element from the array. * ********************************************************/ /* number of elements to store in each cell of the infinite array */ #define BLOCK_SIZE 10 struct infinite_array { /* the data for this block */ float data[BLOCK_SIZE]; /* pointer to the next array */ struct infinite_array *next; }; /******************************************************** * ia_init -- Initializes the infinite array. * * * * Parameters * * array_ptr -- The array to initialize. * ********************************************************/ #define ia_init(array_ptr) {(array_ptr)->next = NULL;}

/******************************************************** * ia_get -- Gets an element from an infinite array. * * * * Parameters * * array_ptr -- Pointer to the array to use. * * index -- Index into the array. * * * * Returns * * The value of the element. * * * * Note: You can get an element that * * has not previously been stored. The value * * of any uninitialized element is zero. * ********************************************************/ extern int ia_get(struct infinite_array *array_ptr, int index); /******************************************************** * ia_store -- Store an element in an infinite array. * * * * Parameters * * array_ptr -- Pointer to the array to use. * * index -- index into the array. * * store_data -- Data to store. * ********************************************************/ extern void ia_store(struct infinite_array * array_ptr, int index, int store_data);

The Makefile for Multiple Files As programs grow, the number of commands needed to create them grows. Typing a series of 10 or 20 commands can be tiresome and error prone, so programmers started writing shell scripts The program make is designed to aid the programmer in compiling and linking programs. The program make was created to make compilation dependent upon whether a file has been updated since the last compilation. The file Makefile (case sensitivity is important in UNIX) contains the rules used by make to decide how to build the program.

Makefile The Makefile contains the following sections: Comments Any line beginning with a hash mark (#) is a comment. Macros A macro has the format: name = data name is any valid identifier and data is the text that will be substituted whenever make sees $(name). Explicit rules Explicit rules tell make what commands are needed to create the program. Default rules make uses a set of built-in rules to determine what command to execute.

Explicit rules Format target is the name of a file to create. It is "made" or created out of the source file source. If the target is created out of several files, they are all listed. The command that generates the target is specified on the next line. Commands are listed one per line. Each is indented by a tab. target: source [source2] [source3] command [command] [command]... hello: hello.c cc -g -ohello hello.c

Makefile CFLAGS = -g OBJ=ia.o hist.o all: hist hist: $(OBJ) $(CC) $(CFLAGS) -o hist $(OBJ) hist.o:ia.h hist.c ia.o:ia.h ia.c Macros Explicit Rules Default Rules $(CC) $(CFLAGS) -c file.c

Example [File: ia/makefile.gcc] # # # Makefile for UNIX systems # # using a GNU C compiler. # # # CC=gcc CFLAGS=-g -Wall -D__USE_FIXED_PROTOTYPES__ -ansi all: hist hist: hist.o ia.o $(CC) $(CFLAGS) -o hist hist.o ia.o hist.o: hist.c ia.h ia.o: ia.c ia.h clean: rm -f hist hist.o ia.o

Modular Programming Exercise Requirements Input the width, length, height from keyboard. Calculate volume, area, girth. Print the value. Files main.c Main function file to call the functions. input.c The functions and data of input-related calc.c The functions and data of calcuate-related print.c The functions and data of print-related global.h The global header file. Makefile

Modules int input_lengths (int * width, int * length, int * height); int calc_volume (int width, int length, int height); int calc_area(int width, int length, int height); int calc_girth(int width, int length, int height); void print_data(char* string, int value);

The End !

Notice Homework Dead Line : PM 1:00 Question & Claim Phone : Visit : 302 dong ho (AM 10:00 ~) Dead Line : 6. 23