TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)

Slides:



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

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
1 TDBA66, vt-04, Lecture Ch9 Text files  Text file contains ASCII-characters  New line and end-of-file are special characters in a text file Ex. 1 
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Programming Review: Functions, pointers and strings.
Using Abstraction to Manage Complexity Abstraction: procedural abstraction & data abstraction. Procedural abstraction=> function development should separate.
Lecture 3 Strings More control statements C functions.
Structure and Union Types 程式設計 潘仁義 CCU COMM. Structure Type Definition struct structured data objects, can be defined by users #define STRSIZ 10 typedef.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
chap13 Chapter 13 Programming in the Large.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
File Handling Spring 2013Programming and Data Structure1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 13: Programming in the Large Problem Solving & Program Design.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
File IO and command line input CSE 2451 Rong Shi.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 12: Programming in the Large By: Suraya Alias 1-1.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
chap11 Chapter 11 Structure and Union Types.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
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.
Problem Solving and Program Design in C Chap. 11 Structure and Union Types Chow-Sing Lin.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Problem Solving and Program Design in C Chap. 11 Text and Binary File Processing Chow-Sing Lin.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Programming application CC213 Week 08 – Struct & Unions– Chapter 10.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
A bit of C programming Lecture 3 Uli Raich.
Functions, Part 2 of 2 Topics Functions That Return a Value
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Structure and Union Types
Functions, Part 2 of 3 Topics Functions That Return a Value
Structure and Union Types
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Presentation transcript:

TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…) Data abstraction (abstract data types) specify the data objects and the operations to be performed on these data objects A queue: how to implement? Array, linked list and the operations: enqueue(), dequeue(), first() Encapsulation of data objects and operations leads to reusable code

TDBA66, Lecture Ch. 11, vt-03 2 Header Files A text file used to create personal librarys name: name_of_area.h Typical contents. 1.A block comment telling the purpose of the library 2.#define directives for naming constants 3.User defined data types 4.Block comments telling the purpose of every library function and prototype declarations of the form extern extern has the effect that the function that is declared extern can be called from any function where #include ”name_of_area.h” is included

TDBA66, Lecture Ch. 11, vt-03 3 Figure 11.2 Header File planet.h for Personal Library with Data Type and Associated Functions /* planet.h * * abstract data type planet * * Type planet_t has these components: * name, diameter, moons, orbit_time, rotation_time * * Operators: * print_planet, planet_equal, scan_planet */ #define PLANET_STRSIZ 10 typedef struct { /* planet structure */ char name[PLANET_STRSIZ]; double diameter; /* equatorial diameter in km*/ int moons; /* number of moons*/ double orbit_time, /* years to orbit sun once*/ rotation_time; /* hours to complete one revolution on axis*/ } planet_t;

TDBA66, Lecture Ch. 11, vt-03 4 Fig 11.2 cont. /* * Displays with labels all components of a planet_t structure */ extern void print_planet(planet_t pl); /* input-one planet structure*/ /* * Determines whether or not the components of planet_1 and planet_2 match */ extern int planet_equal(planet_t planet_1, /* input - planets to */ planet_t planet_2); /* compare*/ /* * Fills a type planet_t structure with input data. Integer returned as * function result is success/failure/EOF indicator. * 1 => successful input of planet * 0 => error encountered * EOF => insufficient data before end of file * In case of error or EOF, value of type planet_t output argument is undefined. */ extern int scan_planet(planet_t *plnp); /* output - address of planet_t structure to fill*/

TDBA66, Lecture Ch. 11, vt-03 5 Figure 11.3 Portion of Program That Uses Functions from a Personal Library /* * Beginning of source file in which a personal library and system I/O library are used. */ #include /* system's standard I/O functions* / #include "planet.h" /* personal library with planet_t data type and operators*/...

TDBA66, Lecture Ch. 11, vt-03 6 Implementation Files Figure 11.4 Implementation File planet.c Containing Library with Planet Data Type and Operators /* * planet.c */ #include #include "planet.h" /* * Displays with labels all components of a planet_t structure */ void print_planet(planet_t pl) /* input - one planet structure */ { printf("%s\n", pl.name); printf(" Equatorial diameter: %.0f km\n", pl.diameter); printf(" Number of moons: %d\n", pl.moons); printf(" Time to complete one orbit of the sun: %.2f years\n", pl.orbit_time); printf(" Time to complete one rotation on axis: %.4f hours\n", pl.rotation_time); }

TDBA66, Lecture Ch. 11, vt-03 7 Fig 11.4 cont. /* * Determines whether or not the components of planet_1 and planet_2 match */ int planet_equal(planet_t planet_1, /* input - planets to*/ planet_t planet_2) /* compare*/ { return (strcmp(planet_1.name, planet_2.name) == 0 && planet_1.diameter == planet_2.diameter && planet_1.moons == planet_2.moons && planet_1.orbit_time == planet_2.orbit_time && planet_1.rotation_time == planet_2.rotation_time); }

TDBA66, Lecture Ch. 11, vt-03 8 Fig 11.4 cont. /* * Fills a type planet_t structure with input data. Integer returned as * function result is success/failure/EOF indicator. * 1 => successful input of planet * 0 => error encountered * EOF => insufficient data before end of file * In case of error or EOF, value of type planet_t output argument is * undefined. */ int scan_planet(planet_t *plnp) /* output - address of planet_t structure to fill*/ { int result; result = scanf("%s%lf%d%lf%lf", plnp->name, &plnp->diameter, &plnp->moons, &plnp->orbit_time, &plnp->rotation_time); fflush(stdin); if (result == 5) result = 1; else if (result != EOF) result = 0; return (result); }

TDBA66, Lecture Ch. 11, vt-03 9 Storage classes There are 4 storage classes autolocal variables in functions. Exist only within the function externwhen declared outside a function and reachable from everywhere (gtlobal variables). E.g. Prototypes See examples in Fig and 11.7 static static float num; /* creates local variable num that ”lives for ever” */ Used to remember its value form one call to another register(not used very often)

TDBA66, Lecture Ch. 11, vt Exit() Figure 11.8 Function factorial with Premature Exit on Negative Data /* * Computes n! * n is greater than or equal to zero -- premature exit on negative data */ int factorial(int n) { int i, /* local variables */ product = 1; if (n < 0) { printf("\n***Function factorial reports "); printf("ERROR: %d! is undefined***\n", n); exit(1); } else { /* Compute the product nx(n-1)x(n-2)x...x2x1 */ for (i = n; i > 1; --i) { product = product * i; } /* Return function result */ return (product); }

TDBA66, Lecture Ch. 11, vt Ch. 11.6, not included in course Ch Arguments to function main Figure File Backup Using Arguments to Function main (only beginning) /* * Makes a backup of the file whose name is the first command line argument. * The second command line argument is the name of the new file. */ #include int main(int argc, /* input-argument count(including program name) */ char *argv[]) /* input - argument vector */ { FILE *inp, /* file pointers for input */ *outp; /* and backup files */ char ch; /* one character of input file */ int status; /* Open input and backup files if possible */ inp = fopen(argv[1], "r"); if (inp == NULL) { printf("\nCannot open file %s for input\n", argv[1]); exit(1); } if ((outp = fopen(argv[2], "w")) == NULL) { printf("\nCannot open file %s for output\n", argv[2]); exit(1); }