1 Chapter 1 Data structure and Programming Principles.

Slides:



Advertisements
Similar presentations
纺纱学. 2 绪 论 基本要求:了解纺纱系统的类别 重点掌握:棉纺系统的工艺流程 3 一、纺纱原理与设备 纺纱:用物理或机械的方法将纺织纤维纺成纱 线的过程。 纺纱原理:初加工、原料的选配、开松除杂、 混和、梳理、精梳、并合、牵伸、加捻、卷绕等。 纺纱方法:传统纺纱方法、新型纺纱方法。 纺纱设备:开清棉联合机、梳棉机、精梳机、
Advertisements

Data Structures Chapter 1 Programming Principles Andreas Savva.
概率统计( ZYH ) 节目录 3.1 二维随机变量的概率分布 3.2 边缘分布 3.4 随机变量的独立性 第三章 随机向量及其分布 3.3 条件分布.
Software Engineering 2007/2008 Chapter 2 Modeling the Process and Life Cycle.
第 12 章位运算 C 语言兼具高级语言及低级语言的特性,因此 适合编写系统软件。 C 语言具备低级语言的特性 就在于它能直接对硬件进行操作,即位运算。 所谓位运算是指,按二进制位进行的运算。 例如,将一个存储单元中各二进位左移或右移一 位等。
计算机 在分析化学的应用 ( 简介 ) 陈辉宏. 一. 概述 信息时代的来临, 各门学科的研究方法都 有了新的发展. 计算机的介入, 为分析化学的进展提供了 一种更方便的研究方法.
编译程序 构造原理和实现技术 授课教师:吕江花. 第一章 编译程序概述 主要内容: 几个基本概念 编译器的工作过程概述 编译器各个阶段的功能描述 编译程序的实现途径.
主要内容  LR(0) 分析. 0 S→  E # E→  E+T E→  T T→  id T→  ( E ) 1 S→E  # E→E  +T 5 T→id  3 E→E+  T T→  id T→  (E) 4 E→E+T  9 E→T  6 T→(  E) E→
嵌入式操作系统 陈香兰 Fall 系统调用 10/27/09 嵌入式 OS 3/12 系统调用的意义  操作系统为用户态进程与硬件设备进行交互提供 了一组接口 —— 系统调用  把用户从底层的硬件编程中解放出来  极大的提高了系统的安全性  使用户程序具有可移植性.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第六十二讲 ) 离散数学. 最后,我们构造能识别 A 的 Kleene 闭包 A* 的自动机 M A* =(S A* , I , f A* , s A* , F A* ) , 令 S A* 包括所有的 S A 的状态以及一个 附加的状态 s.
Viterbi 算法 viterbi 实验要求:见 experiment3.doc – 将 TRUNC_LENGTH 取值为 4 , 8 , 16 , 32 , 64 ,看看程序运行结果会有什么变化并分析原 因。 – 将 NUMSIM 取值为 10 1 , 10.
1 为了更好的揭示随机现象的规律性并 利用数学工具描述其规律, 有必要引入随 机变量来描述随机试验的不同结果 例 电话总机某段时间内接到的电话次数, 可用一个变量 X 来描述 例 检测一件产品可能出现的两个结果, 也可以用一个变量来描述 第五章 随机变量及其分布函数.
有限自动机 (Finite Automata) 描述程序设计语言中的单词的识别过程。 主要内容: 确定有限自动机 DFA(Deterninistic FA) 确定有限自动机 DFA 的实现 非确定有限自动机 NFA(Nondeterninistic FA) NFA 到 DFA 的转换 DFA 的化简.
C 语言程序设计 2008 版. C 语言程序设计 教学要求  掌握程序设计语言的基本知识  常用算法  初步的程序设计能力 学习方法  自主学习  重视上机实践.
第 10 章 数据表的基本操作 计算机文化基础. 第一节 表结构显示与修改 1 、显示表的结构 格式: LIST/DISPLAY STRUCTURE [TO PRINT] 功能:显示(打印)当前已打开的数据库文件结构 说明:首先必须打开库文件。 LIST 是连续显示全部内容; DISPLAY 是分屏显示.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第三十八讲 ) 离散数学. 第八章 格与布尔代数 §8.1 引 言 在第一章中我们介绍了关于集 合的理论。如果将 ρ ( S )看做 是集合 S 的所有子集组成的集合, 于是, ρ ( S )中两个集合的并 集 A ∪ B ,两个集合的交集.
南航计算机系 南航计算机系 数据结构. 第一章 绪论  什么是数据结构 什么是数据结构  基本概念和术语 基本概念和术语  抽象数据类型的表示和实现 抽象数据类型的表示和实现  算法和算法分析 算法和算法分析.
第十一章 曲线回归 第一节 曲线的类型与特点 第二节 曲线方程的配置 第三节 多项式回归.
Chapter1 PROGRAMMING PRINCIPLES. Problems of Large Programs 1.The patchwork approach --- Disadvantage: if any change must be made, it will have unpredictable.
线性代数习题课 吉林大学 术洪亮 第一讲 行 列 式 前面我们已经学习了关 于行列式的概念和一些基本 理论,其主要内容可概括为:
第 4 章 过程与变量的作用范围. 4.1 Visual Basic 的代码模块 Visual Basic 的应用程序是由过程组成的, 过程代码存放在模块中。 Visual Basic 提供了 三类模块,它们是窗体模块、标准模块和类 模块。 窗体模块 窗体模块是大多数 Visual Basic.
自顶向下分析 —— 递归下降法 递归下降法 (Recursive-Descent Parsing) 对每个非终极符按其产生式结构产生相应语 法分析子程序. 终极符产生匹配命令 非终极符则产生调用命令 文法递归相应子程序也递归,所以称这种方 法为递归子程序方法或递归下降法。
实验三: 用双线性变换法设计 IIR 数字滤波器 一、实验目的 1 熟悉用双线性变换法设计 IIR 数字滤波器的原理与方法。 2 掌握数字滤波器的计算机仿真方法。 3 通过观察对实际心电图信号的滤波作用, 获得数字滤波的感性知 识。
OS 进程调度模拟演示 制作人: 钱晶 高上上. OS 进程调度模拟-实验原理 静态优先级原理 在这种方式下,系统一旦把处理机分配给就绪队 列中的优先权最高的进程后,该进程便一直执行下去, 直至完成。或因为发生某事件使该进程放弃处理机,系 统方可再将处理机分配给另一优先级最高的进程。这些 事件包括有优先级更高的进程进入,或是因为某些原因.
第二章 贝叶斯决策理论 3学时.
操作系统原理课程设计指南 姜海燕 设计考核幻灯制作  1.1 封皮:系统名称,研制人员  1.2 目的及意义  1.3 功能设计:功能框图、用例图  1.4 结构设计:系统结构  1.5 核心技术及技术路线:画图  1.6 进度安排  1.7 人员安排  1.8.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 5 章 解线性方程组的直接法 实际中,存在大量的解线性方程组的问题。很多数值方 法到最后也会涉及到线性方程组的求解问题:如样条插值的 M 和.
在发明中学习 线性代数 概念的引入 李尚志 中国科学技术大学. 随风潜入夜 : 知识的引入 之一、线性方程组的解法 加减消去法  方程的线性组合  原方程组的解是新方程的解 是否有 “ 增根 ” ?  互为线性组合 : 等价变形  初等变换  高斯消去法.
第十章 目标代码生成  目标代码 ( 单寄存器 )  临时变量的存储空间分配  寄存器的分配和释放.
1 学籍归档工作 年学籍归档工作安排 从 2008 年开始,改为按届移交档案 从 2008 年开始,改为按届移交档案 今年的归档工作范围: 今年的归档工作范围: 2014 年应届毕业班级 2014 年应届毕业班级 2014 年办理毕业证的往届生。 2014 年办理毕业证的往届生。 整理内容:根据.
第5章 键盘与鼠标接口 5.1 键盘概述 5.2 键盘的工作原理 5.3 PC扩展键盘接口 5.4 键盘中断与键盘I/O 5.5 鼠标接口.
Chapter 1 Program Design
第 3 章 控制流分析 内容概述 – 定义一个函数式编程语言,变量可以指称函数 – 以 dynamic dispatch problem 为例(作为参数的 函数被调用时,究竟执行的是哪个函数) – 规范该控制流分析问题,定义什么是可接受的控 制流分析 – 定义可接受分析在语义模型上的可靠性 – 讨论分析算法.
实验三: 用双线性变换法设计 IIR 数字滤波器 一、实验目的 1 熟悉用双线性变换法设计 IIR 数字滤波器的原理与方法。 2 掌握数字滤波器的计算机仿真方法。 3 通过观察对实际心电图信号的滤波作用, 获得数字滤波的感性知 识。
编译原理总结. 基本概念  编译器 、解释器  编译过程 、各过程的功能  编译器在程序执行过程中的作用  编译器的实现途径.
主要学习内容: 1、用描述法分析样品的风味特征 2 、学习味觉试验方法。 闽北职业技术学院食品与生物工程系.
 符号表  标识符的作用: 声明部分:定义了各种对象及对应的属性和 使用规则。 程序体:对所定义的对象进行各种操作。 $ididname IdnameAttributeIR  必要性 Token : 新表-符号表(种类、类型等信息):
1 数控车削加工 重庆交通大学工程实训中心 Chongqing Jiaotong University.
网上预约集港操作指南 一、登录系统 登陆下面图片显示网址:输入堆场用户名、密码和校验码登陆系统.
学生成长成才导图填报指南 填报网址:
《 UML 分析与设计》 交互概述图 授课人:唐一韬. 知 识 图 谱知 识 图 谱知 识 图 谱知 识 图 谱.
College of Computer and Information Science Chapter 14 Programming and Languages.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Chapter 1 PROGRAMMING PRINCIPLES 1. Introduction : Problems with large programs 2.The Game of Life (a continuing example) 3. Programming style 4. Coding,
1 Data Structures CSCI 132, Spring 2014 Lecture 3 Programming Principles and Life Read Ch. 1.
College of Computer and Information Science Chapter 14 Programming and Languages.
Kruse/Ryba Ch011 Object Oriented Data Structures Programming Principles Introduction Programming Style Coding, Testing, Further Refinement Program Maintenance.
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
1 Signals and Systems Lecture 26 Properties of Laplace Transform Analysis LTI System using LT System Function.
Chapter 3 Programming Languages Unit 1 The Development of Programming Languages.
项目七: PLC 功能指令应用 带进位循环左移指令 XXXXX. 项目七: PLC 功能指令应用 FX2 系列可编程控制器移位控制指令有移位、循环移位、字移位 及先进先出 FIFO 指令等 10 条指令。 带进位循环右移指令 RCR 带进位循环左移指令 RCL 字右移位指令 WSFR 先入先出读出指令.
§10.2 对偶空间 一、对偶空间与对偶基 二、对偶空间的有关结果 三、例题讲析.
企业产品标准信息公共服务平台 操作介绍 目录 一 、系统架构介绍 二 、企业产品标准自我声明填报系统 三 、企业产品标准公示系统.
表单自定义 “ 表单自定义 ” 功能是用于制作表单的 工具,用数飞 OA 提供的表单自定义 功能能够快速制作出内容丰富、格 式规范、美观的表单。
7 生产费用在完工产品与在产 品之间分配的核算. 2 第七章 生产费用在完工产品与在产品之 间的分配  知识点 :  理解在产品的概念  掌握生产费用在完工产品与在产品之间的分 配.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
力的合成 力的合成 一、力的合成 二、力的平行四边形 上一页下一页 目 录 退 出. 一、力的合成 O. O. 1. 合力与分力 我们常常用 一个力来代替几个力。如果这个 力单独作用在物体上的效果与原 来几个力共同作用在物体上的效 果完全一样,那么,这一个力就 叫做那几个力的合力,而那几个 力就是这个力的分力。
逻辑设计基础 1 第 7 章 多级与(或)非门电路 逻辑设计基础 多级门电路.
工程实训中心 超外差式收音机制作 重庆交通大学工程实训中心 Chongqing Jiaotong University.
一、 版 面 构 成 的 概 念 版 面 构 成 的 概 念 二、 版 面 构 成 的 发 展 趋 势 版 面 构 成 的 发 展 趋 势 三、 广 告 文 字 的 版 面 构 成 广 告 文 字 的 版 面 构 成 四、 广 告 版 面 的 视 觉 流 程 广 告 版 面 的 视 觉 流 程.
1 A Brief Introduction to the Game of Life. 2 Example demonstration ——game of life  Definition  It takes place on an unbounded rectangular grid in which.
1 文件的查找. 2 回收站的管理 3 磁盘管理 格式化软盘 复制文件到软盘 整理磁盘碎片 4 格式化软盘.
Innovation Intelligence ® Workshop – Riser VIV. Copyright © 2015 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved. 学习内容 实用流固耦合的设置方法.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
Generation of Chinese Character Based on Human Vision and Prior Knowledge of Calligraphy 报告人: 史操 作者: 史操、肖建国、贾文华、许灿辉 单位: 北京大学计算机科学技术研究所 NLP & CC 2012: 基于人类视觉和书法先验知识的汉字自动生成.
§7.2 估计量的评价标准 上一节我们看到,对于总体 X 的同一个 未知参数,由于采用的估计方法不同,可 能会产生多个不同的估计量.这就提出一 个问题,当总体的一个参数存在不同的估 计量时,究竟采用哪一个好呢?或者说怎 样评价一个估计量的统计性能呢?下面给 出几个常用的评价准则. 一.无偏性.
SME.USTB Human Factors 人机工程学 By Wei Dong Department of Industry Design, SME, USTB.
About the Presentations
Presentation transcript:

1 Chapter 1 Data structure and Programming Principles

2 Contents Points  What is Data Structure? What is Data Structure?  General process of processing problems by computer. General process of processing problems by computer.  Program Design in C++. Program Design in C++.  Example demonstration. Example demonstration.

3 1 What is Data Structure?  Fundamental Terms Data Layers: Data (数据): a set of all the symbols that computer can manage.( 所有能输入到计算机中并被计 算机所识别和处理的符号的总称。 ) Data Elements (数据元素) : 数据中的一个个体。 a character ‘*’ or a student when it is stored in a computer. Data Item (数据项) :when the data element is a record,it can be divided into data items. Example:a student record may be combined by number, name, score and so on.

4 Data Structure: Collection of relational Data Elements  Logical Structure (逻辑结构) the intrinsic relations between data elements According to the difference of the relation,we can classify them into four kinds. Lists Trees Graphics Set

5 What is Data Structure? Storage Structure: (存储结构) Physical Structure in computer Contiguous structure Linked structure

6 2 Processing problems by computer algorithm implement Outside representation Management Demand Logical structure Basic operation Storage structure question Mathematic model Build model refine

7  Fundamental Tasks:  Definition of Data Structure (including logical structure and base operations);  Realization of Data Structure (including storage structure and base operations);  Estimation and selection of Data Structure (analysis of algorithms)  Asymptotic Time Complexity (时间复杂度)  Asymptotic Space Complexity (空间复杂度) Processing problems by computer

8 Problems of Large Programs 1. Problem specification (需求分析) 2. Program organization 3. Choice of data structures 4. Algorithm analysis 5. write the whole program 6. Debugging 7. Testing and verification 8. Maintenance The patchwork approach is doomed to fail. And we must also be careful to observe important principles of program design.

9 3 Program Design in C++  C++ features:  Data Abstraction (数据抽象)  Object-oriented design (面向对象的设计)  Top-down approach  Code reuse (代码重用)  Refinement, improvement, extension of C  Introduction of OO (Object Oriented)  Encapsulation (封装性)  Inheritance (继承性)  Polymorphism (多态性)

10 4 Example demonstration ——game of life  Definition  It takes place on an unbounded rectangular grid in which each cell can either be occupied by an organism or not.  Occupied cells are called alive; unoccupied cells are called dead.  Which cells are alive or not changes from generation to generation according to the number of neighboring cells that are alive, as follows: 4.1 Rules for the game of life

11 Rules of the Game of life  1.The neighbors of a given cell are the eight cells that touch it vertically, horizontally, or diagonally. Every cell is either living or dead. (only two states)  2.A living cell stays alive in the next generation if it has either 2 or 3 living neighbors; it dies if it has 0, 1, 4, or more living neighbors. (for a living cell, conditions of still living and die)

12 Rules of the Game of life 3.A dead cell becomes alive in the next generation if it has exactly three neighboring cells, no more or fewer, that are already alive. All other dead cells remain dead in the next generation. (for a dead cell when the state will be change?) 4.All births and deaths take place at exactly the same time, so that a dying cell can help to give birth to another, but cannot prevent the death of others by reducing overcrowding, nor can cells being born either preserve or kill cells living in the previous generation. (interaction each other)

13 A particular arrangement of living and dead cells in a grid is called a configuration. Applying the preceding rules, an initial configuration changes to another at each generation. configuration

14 4.2Examples Moribund example  By rule 2 and 3, the living cells will die in the coming generation, and no cells will become alive, so the configuration dies out

15 Stable example  Each of the living cells has a neighbor count of three, and hence remain alive, but the dead cells all have neighbor counts of two or less, and hence none of them becomes alive. Examples --2

16 Alternate examples:  The two configurations continue to alternate from generation to generation. Examples --3

17 Variety( 多样化 ) : through many generation,a simple initial configuration  grow into large configurations,  die out,  reach a state where they do not change  Go through a repeating pattern every few generations. Popularity (流行) :the life game is very popular. Our goal:write a program that will show how an initial configuration will change from generation to generation.

the solution:classes,objects,and methods  Set up a Life configuration as an initial arrangement of living and dead cells.  Print the Life configuration.  While the user wants to see further generations: Update the configuration by applying the rules of the Life game. Print the current configuration.

19 Review of C++ elements  A class collects data and the methods used to access or change the data.  Such a collection of data and methods is called an object belonging to the given class.  Every C++ class consists of members that represent either variables (called data members) or functions (called methods or member functions). The member functions of a class are normally used to access or alter the data members. Example: For the Life game, Class:life class Object:a configuration Method:initialize(),update(),print()

20  By relying on its specifications, a client can use a method without needing to know how the data are actually stored or how the methods are actually programmed. This important programming strategy known as information hiding.  Data members and methods available to a client are called public; further private variables and functions may be used in the implementation of the class, but are not available to a client. Convention (约定) Methods of a class are public. Functions in a class are private. Review of C++ elements

Life: The Main Program #include “utility.h” //page #include “life.h“//life 类的定义部分 int main( ) // Program to play Conway's game of Life. /*Pre: The user supplies an initial configuration of living cells. Post: The program prints a sequence of pictures showing the changes in the configuration of living cells according to the rules for the game of Life. Uses: The class Life and its methods initialize( ),print( ), and update( ).The functions instructions( ),user_ says_ yes( ). */

22 { Life configuration; instructions( ); configuration.initialize( ); configuration.print( ); cout << "Continue viewing new generations? " << endl; while (user_says_yes( )) { configuration.update( ); configuration.print( ); cout << "Continue viewing new generations? " << endl; }

23 Utility Package We shall assemble a utility package that contains various declarations and functions that prove useful in a variety of programs, even though these are not related to each other as we might expect if we put them in a class. For example, we shall put declarations to help with error processing in the utility package. Our first function for the utility package obtains a yes-no response from the user: bool user_says_ yes( ) Pre: None. Post: Returns true if the user enters ‘y’ or ‘Y’; returns false if the user enters ‘n’ or ‘N’; otherwise requests new response

24  Clients, that is, user programs with access to a particular class, can declare and manipulate objects of that class. For example:declare a Life object by: Life configuration;  member selection operator We can now apply methods to work with configuration, using the C++ operator. For example, we can print out the data in configuration by writing: configuration.print( );  The specifications of a method, function, or program are statements of precisely what is done. Preconditions state what is required before; postconditions state what has happened when the method, function, or program has finished. Programming Precept Include precise specifications with every program, function, and method that you write. Other C++ elements

25 Programming Style( 自学 page 10-20)  Names (命名) : Guidelines of names: see p11-12  Documentation and Format (文档化和书写格式) o Some commonly accepted guidelines: see p13  Refinement and Modularity (求精和模块化) : o Top-down refinement

26 o As we write the main program,we decide exactly how the work will be divided among them.,how to divide the work? 自学

27 o Data Categories  Input parameters--passed by value or by reference(const)  Output parameters—passed by reference (suggest)  Inout parameters---used for both input and output  Local variables—defined in the function and exist only while the function is being executed.  Global variables----dangerous,cause side effect

28

Coding, Testing and Further Refinement Coding (编码) :writing an algorithm in the correct syntax of a computer language like C++ Testing (测试) :running the program on sample data chosen to find errors Further Refinement (进一步细化) :turn to the functions not yet written and repeat these steps

30 Example demonstration ——game of life  Coding 1.Stubs (占位程序) compile each function and class separately to compile the main program correctly, there must be something in the place of each function that is used, so put in short, dummy functions.——which named stubs the simplest stubs are those that do little or nothing at all: void instruction() {} bool user_says_yes(){ return false;}

31 Coding of game of life 2.Definition of the class Life const int maxrow=20,maxcol=60; //living cells 共有 20 行, 60 列 class Life{// 存放在 *.h 文件中的类体声明 public: void initialize(); // 初始化 living cells 的状态 void print(); // 打印输出当前 living cells 的状态 void update(); // 进行 dead  living 间的转换 private: int grid[maxrow+2][maxcol+2]; // 借助两维数组存放 living cells // 注意:为处理一致,添加了虚拟的 2 行 2 列,放围墙 int neighbor_count(int row,int col); // 统计邻居 cell 的状态 };

32 Stub example2: To check if the class definition is correct,We should supply the following stubs for its methods in life.cpp: void Life :: initialize( ) {} void Life :: print( ) {} void Life :: update( ) {}

33 Instructions void instructions( ) /* Pre: None. Post: Instructions for using the Life program have been printed. */ { cout << "Welcome to Conway’s game of Life." << endl; cout << "This game uses a grid of size " << maxrow << " by " << maxcol << " in which each" << endl; cout << "cell can either be occupied by an organism or not." << endl; cout << "The occupied cells change from generation to generation"<< endl; cout << "according to how many neighboring cells are alive."<< endl; }

34 User_says_yes( ) bool user says yes( ) { int c; bool initial response = true; do { // Loop until an appropriate input is received. if (initial_response) cout << " (y,n)? " << flush; else cout << "Respond with either y or n: " << flush; do { // Ignore white space. c = cin.get( ); } while (c == ‘\n’ || c == ‘ ’ || c == ‘\t’); initial_response = false; } while (c != ‘y‘ && c != ‘Y‘ && c != ‘n‘ && c != ‘N‘); return (c == ‘y‘ || c == ‘Y‘); }

35 Functions and Methods of class Life 3.Counting neighbors:  sentinel (岗哨) (hedge):A sentinel is an extra entry put into a data structures so that boundary conditions need not be treated as a special case. int Life::neighbor_count(int row,int col){ int i,j,count=0; for (i=row-1;i<=row+1;i++) for (j=col-1;j<=col+1;j++) count+=grid[i][j]; // 如果存活,则累加;否则为 0 count-=grid[row][col]; // 去除自己 return count; }

36

37 Functions and Methods of Class Life 4.Updating the grid void Life::update(){ int row,col,new_grid[maxrow+2][maxcol+2]; for (row=1;row<=maxrow;row++) for (col=1;col<=maxcol;col++) switch(neighbor_count(row,col)){// 调用统计函数, 按结果分情况 case 2: new_grid[row][col]=grid[row][col]; break;// 不变 case 3: new_grid[row][col]=1; break; // 激活 default: new_grid[row][col]=0; //dead } for (row=1;row<=maxrow;row++) for (col=1;col<=maxcol;col++) grid[row][col]=new_grid[row][col];// 将临时数组中的数据拷贝回原 grid 数组 }

38 Functions and Methods of Class Life 5.Input and Output:

39 Functions and Methods of Class Life  Input——initialize void Life::initialize(){ int row,col; for (row=0;row<=maxrow+1;row++) for (col=0;col<=maxcol+1;col++) grid[row][col]=0; cout<<“List the coordinates for living cells.”<<endl; cout<<“Terminate the list with the special pair (-1,-1)”<<endl; cin>>row>>col; while (row!=-1||col!=-1){ if (row>=1&&row<=maxrow) if (col>=1&&col<=maxcol) grid[row][col]=1; else cout<<“Column “<<col<<“ is out of range.”<<endl; else cout<<“Row “<<row<<“ is out of range.”<<endl; cin>>row>>col; }

40 Functions and Methods of class Life  Output——print: void Life::print(){ int row,col; cout<<“\nThe current Life configuration is: “<<endl; for (row=1;row<=maxrow;row++){ for (col=1;col<=maxcol;col++) if (grid[row][col]==1) cout<<“ * ”; else cout<<“ ”; cout<<endl; } cout<<endl; }

41 Functions and Methods of Class Life 6.Drivers: Driver is a short auxiliary program whose purpose is to provide the necessary input for the function, and evaluate the result——so that each function can be isolated and studied by itself

42 Example 1:driver for neighbor_ count(): int main ( ) // driver for neighbor_count( ) /* Pre: None. Post: Verifies that the method neighbor_count ( ) if it returns the correct values. Uses: The class Life and its method initialize( ). */ { Life configuration; configuration.initialize( ); for (row = 1; row <= maxrow; row++) { for (col = 1; col <= maxrow; col++) cout << configuration.neighbor_count(row, col) << " "; cout << endl; }

43 Example 2 :driver for initialize() and print(): Sometimes two functions can be used to check each other. configuration.initialize( ); configuration.print( ); Both methods can be tested by running this driver and making sure that the configuration printed is the same as that given as input.

44 7.Methods for debugging: (self-study) 1). Structured walkthrough 2). Trace tools and snapshots 3). Scaffolding 4). Static analyzer

45 Example demonstration ——game of life 8.Test(self-study) Principles of program Testing: Black-Box Method (黑盒法) (a) Easy values (b) Typical, realistic values (c) Extreme values (d) Illegal values Glass-Box Method (白盒法) : Trace all the paths through the Program,for a single small method, it is an excellent debugging and test method Ticking-Box Method——Don't do anything Just hope for the best!

46 Action: In practice, black-box testing is usually more effective in uncovering errors. One reason is that most subtle programming errors often occur not within a function but in the interface between functions, in misunderstanding of the exact conditions and standards of information interchange between function.

47 Example demonstration ——game of life  Program Maintenance (self-study) For a large and import program, more than half the work comes in the maintenance phase, after it has been completely debugged, tested, and put into use. First step of program maintenance is to begin the continuing process of review, analysis, and evaluation. (for some useful question, see p34)

48 Example demonstration ——game of life  Program Maintenance Review of the Life program problem specification (问题规范) program correctness (程序纠错) user interface (用户接口) modularity and structure (模块化与结构化) documentation (文档) efficiency (效率) Program revision and redevelopment (修订与发布)

49 Pointers and Pitfalls  1. To improve your program, review the logic. Don't optimize code based on a poor algorithm.  2. Never optimize a program until it is correct and working.  3. Don't optimize code unless it is absolutely necessary.  4. Keep your functions short; rarely should any function be more than a page long.  5.Be sure your algorithm is correct before starting to code.  6. Verify the intricate parts of your algorithm.

50 本章习题  P10 Exercise1.2 (a)(e)(I)  P17 Exercise1.3 E1(a)(d) E2 E9  P32 Exercise1.4 E1  P38 Exercise1.5 E1 E2  P32 Programming projects 1.4  P39 Programming projects 1.5 p1-p5

51 Algorithm and program  Description of the particular steps of the process of a problem( 处 理某一问题的步骤的描述 )  characteristics  Finity  Certainty  Feasibility  Input interface  Output interface  Format is flexible , it can be described by Natural Language, Advanced Language,and the syntax is not the most important thing,  Nothing to do with the environment  The whole process of the problem (用计算机处理问题的全部代码的整 体)  Infinity  Syntax must be accurate  Closely linked with the environment