acp 15 chapter 12 addendum

Upload: 0esmon0

Post on 03-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    1/7

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara March 2014

    1

    Chapter 12Files addendum

    This is an addendum to Chapter 12. It is about the header files and it shows haw

    can be created user-defined header files.

    12-a.1 The header files

    Header files contain definitions of functions, data types, constants, and even

    variables. They can be incorporated into any C program by using the preproces-sor directive #i ncl ude. There are several standard header files that are pro-vided together with each compiler; these cover the most important and frequent-

    ly used operations from different areas: input/output actions, string handling,

    mathematical operations, data conversions, and others.

    Besides these standard library functions, the programmer is able to implement

    his own functions, which can be gathered into a library. The prototypes of these

    functions will form a new header file; it can be used in a program almost the

    same way a standard header file is used. The difference is given by the way this

    header file is specified in the preprocessor directive #i ncl ude. Generally,there are two possibilities to do this (describing two different strategies for file

    searching):

    #i ncl ude

    #i ncl ude "myHeader . h"

    The use of angle brackets informs the compiler to search for the specified

    file in a standard list of systems folders (for instance, the compilers i ncl udefolder). This is typically used for the systems header files.

    The use of the double quotes "" around the filename informs the compiler tosearch in the current folder (or in a specific folder of a project) for the indicated

    file. Usually, this is a user-defined header file.Declaring a new header file is a good idea each time there is a group of related

    declarations (of functions, data types, etc.) and all or most of them are needed in

    several different source files. The alternative is to rewrite (or to copy) the entire

    code into each source file that needs it. This would be time-consuming and

    error-prone. Therefore, one important advantage of the header files is that they

    are written once and they can be used as many times as they are needed. Other

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    2/7

    Computer Programming The C Language

    2April 2014

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara

    advantages are connected to the facility of modifying something: the changes

    are made in one place and all the programs that use the header file will automat-

    ically see this changes when the code is compiled again. This also ensures that

    all programs use the same version of the code that belongs to a header file.

    There are several steps that must be followed when applications with user-

    defined header files are developed. These will be explained creating a library of

    functions that manipulate a vector of f l oat numbers.

    First, the header file is created. It will have the extension . h and willcontain the prototypes of the functions (without the implementation of that

    functions), and other definitions if necessary (data types, constants, varia-bles). This is like an interface of the library to the outside programs (this is

    what the applications know about the library) and programs should use the-

    se prototypes when they call the functions.

    The header file myHeader . h of program 12-a.1.1 contains seven func-tions:

    o mySum( ) , myAverage( ) , myMax( ) , and myMi n( ) re-

    ceive as argument the vector v, together with its dimension nand return a float;

    o myCount ( ) receives as arguments the vector v, together with

    its dimension n, and also a float value xand returns an integer;

    o pr i nt LtoR( ) and pr i nt Rt oL( ) receive as argument the

    vector v, together with its dimension nand doesnt return any-

    thing.

    Program 12-a.1.1 the first part: myHeader . h

    f l oat mySum( i nt n, f l oat v[ ] ) ;f l oat myAver age( i nt n, f l oat v[ ] ) ;i nt myCount ( i nt n, f l oat v[ ] , f l oat x) ;

    voi d pr i nt Lt oR( i nt n, f l oat v[ ] ) ;voi d pr i nt Rt oL( i nt n, f l oat v[ ] ) ;f l oat myMax( i nt n, f l oat v[ ] ) ;f l oat myMi n( i nt n, f l oat v[ ] ) ;

    The second step is to create the file that contains the implementation of the

    functions from the header. It must have the same name as the header file,

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    3/7

    12 Files addendum

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara April 2014

    3

    but the extension . c. This source code will include in a preprocessor di-rective the header file it implements and also other header files it needs. It is

    obvious that this file doesnt contain a mai n( ) function and cannot be run.

    The source code myHeader . c of program 12-a.1.1 gives the followingmeanings for the functions of the header file:

    o mySum( ) calculates and returns the sum of the vectors ele-

    ments;

    o myAverage( ) calls the function mySum( ) , divides the val-ue received from it to the number of vectors elements and re-

    turns the result;

    o myCount ( ) counts and returns the number of elements that

    are greater than or equal to a threshold value x received as ar-gument;

    o pr i nt LtoR( ) displays the vector from the left to the right;

    o pr i nt Rt oL() displays the vector from the right to the left;

    o

    myMax( ) founds and returns the maximum value of the vec-tor;

    o

    myMi n( ) founds and returns the minimum value of the vec-tor.

    Program 12-a.1.1 the second part: myHeader . c

    #i ncl ude #i ncl ude "myHeader . h"

    f l oat mySum( i nt n, f l oat v[ ] ) {i nt i ;f l oat sum=0;

    f or ( i =0; i

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    4/7

    Computer Programming The C Language

    4April 2014

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara

    }

    i nt myCount ( i nt n, f l oat v[ ] , f l oat x) {i nt i , count er =0;

    f or ( i =0; i =x) count er ++;

    r et ur n count er ;}

    voi d pr i nt Lt oR( i nt n, f l oat v[ ] ) {i nt i ;

    f or ( i =0; i =0; i - - )pr i nt f ( "%. 2f " , v[ i ] ) ;

    }

    f l oat myMax( i nt n, f l oat v[ ] ) {i nt i ;f l oat m;

    m=v[ 0] ;f or ( i =1; i m) m=v[ i ] ;r et ur n m;

    }

    f l oat myMi n( i nt n, f l oat v[ ] ) {i nt i ;

    f l oat m;m=v[ 0] ;f or ( i =1; i

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    5/7

    12 Files addendum

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara April 2014

    5

    The final step is to create an application that uses this library. It is a . cfile and should include in a preprocessor directive the new header file (oth-

    erwise, it cannot use the librarys functions).

    The application myMai n. cof program 12-a.1.1 declares and initializes avector of marks. Then, it displays the average mark (calling the function

    myAverage( ) ), the number of students that passed the exam (calling the

    function myCount ( ) with the threshold value 5), and prints the vector ac-cording to a users option regarding the printing direction (left to right or

    right to left).

    Program 12-a.1.1 the third part: myMai n. c

    #i ncl ude #i ncl ude "myHeader . h"

    voi d mai n( voi d) {f l oat mar ks[ ] ={4, 7. 5, 9. 75};i nt n=3, answer ;

    pr i nt f ( "The aver age mar k i s: %. 2f ",myAver age( n, marks) ) ;

    pr i nt f ( " \ n%d st udent s passed t he exam",

    myCount ( n, marks, 5) ) ;pr i nt f ( "\ nThe maxi mum mar k was: %. 2f ",

    myMax(n, marks) ) ;pr i nt f ( " \ nHow do you want t o di spl ay t he r e-

    sul t s ?" ) ;pr i nt f ( " \ n1 - From l ef t t o r i ght " ) ;pr i nt f ( " \ n2 - From r i ght t o l ef t " ) ;pr i nt f ( "\ nAnswer : ") ;scanf ( "%d" , &answer ) ;i f ( answer ==1) pr i nt Lt oR( n, mar ks) ;el se pr i nt Rt oL( n, mar ks) ;

    get ch( ) ;}

    12-a.2 Questions and exercises

    A. Find the error.

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    6/7

    Computer Programming The C Language

    6April 2014

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara

    1.

    ;

    2. ;

    3. ;

    B. Considering the following programs, specify what will be printed on the

    screen once these programs are executed.

    4.

    ;

    5. ;

    C.

    Choose the correct answer (one only).6.

    a); b); c); d); e).

    7.

    a); b); c); d); e) .

    8.

    a); b); c); d); e).

    9.

    a); b); c); d); e).

    10.

    a); b); c) ; d); e).

    D. Write a program to solve the following problem.

    11.Create a new header file, mySt r i ng. h, which contains several func-tions that operate on strings. For instance:

    a. i nt i ndexOf ( char *s, char c) returns the index of

    the first occurrence of a char c in a string s or - 1 if the char

    doesnt belong to that string;

    b. i nt l ast I ndexOf ( char *s, char c) returns the

    index of the last occurrence of a char cin a string s or - 1if thechar doesnt belong to that string;

  • 8/11/2019 ACP 15 Chapter 12 Addendum

    7/7

    12 Files addendum

    Lect.eng. Adriana ALBU, PhDPolitehnica University of Timioara April 2014

    7

    c.

    i nt l enCompar e( char *f i r st , char *second) compares the length of two strings;

    d.

    i nt st ar t sWi t h( char *s, char *pr ef i x) veri-fies if a string starts with a specified prefix;

    e. i nt endsWi t h( char *s, char *suf f i x) verifies ifa string ends with a specified suffix;

    f. voi d r epl aceChar ( char *s, char c1, char c2)

    replaces a char c1with another char c2in a string s ;

    g.

    voi d subSt r i ng( char *sour ce, i ntst ar t I ndex, i nt endI ndex, char *r esul t ) ex-tracts the substring delimited by two specified indexes.

    12-a.3 Answers to questions and exercises

    A.1.

    B.4.

    C.6.