1. Trang chủ
  2. » Công Nghệ Thông Tin

THEORY AND PROBLEMS OF PROGRAMMING WITH Second Edition phần 10 ppsx

46 488 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 46
Dung lượng 1,19 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

nonzero value if true; 0otherwise.. Return a nonzero value if true; 0otherwise.. Return a nonzero value if true; 0otherwise.. Return a nonzero value if true; 0otherwise... Valid real Val

Trang 1

Appendix

Library Functions

abs ( 1 ) int Return the absolute value of i s t d l i b h

a t a n 2 ( d l , d 2 ) double Return the arc tangent of d l /d2 math h

a t o f (s) double Convert string s to a double-precision quantity s t d l i b h

a t o i (s) int Convert string sto an integer s t d l i b h

a t o l(s) long Convert string sto a long integer s t d l i b h

c a l l o c ( u l , u 2 ) void* Allocate memory for an array having u l m a l l o c h, or

elements, each of length u2bytes Return a s t d l i b h pointer to the beginning of the allocated space

c e i l (d ) double Return a value rounded up to the next higher integer math h

cosh ( d ) double Return the hyperbolic cosine of d math h

d i f f t i r n e ( l l , 1 2 ) double Return the time difference 11 - 12, where 11and t i m e h

12represent elapsed times beyond a designated base time (see the t i m e function)

e x i t ( u ) void Close all files and buffers, and terminate the program s t d l i b h

(Value of U is assigned by function, to indicate termination status.)

double Raise e to the power d (e =2.71 82818 - - is the base math h

of the natural (Naperian) system of logarithms)

f c l o s e ( f ) int Close file f Return 0 if file is successfully closed s t d i o h

f e o f ( f ) int Determine if an end-of-file condition has been reached s t d i o h

If so, return a nonzero value; otherwise, return 0

f g e t c ( f ) int Enter a single character from file f s t d i o h

f g e t s ( s , i , f ) char* Enter string s,containing icharacters, from file f s t d i o h

f l o o r ( d ) double Return a value rounded down to the next lower integer math h fmod(dl,d2) double Return the remainder of d l /d2 (with same sign as dl) math.h

f open ( s l ,s2) file* Open a file named s l of type s2 Return a pointer to the file s t d i o h

f p r i n t f ( f , ) int Send data items to file f (remaining arguments are s t d i o h

complicated -see Appendix G)

f p u t c ( c , f ) int Send a single character to file f s t d i o h

f p u t s ( s , f ) int Send string s to file f s t d i o h

487

Trang 2

Function Type Purpose inc1ude File

file f to string s

free(P1 void Free a block of allocated memory whose beginning is malloc h, or

complicated -see Appendix G)

location i(imay represent the beginning of the file, the current pointer position, or the end of the file)

string s to file f

int Enter a single character from the standard input device stdio h

char* Enter string s from the standard input device stdio h

int Determine if argument is alphanumeric Return a ctype h

nonzero value if true; 0 otherwise

nonzero value if true; 0 otherwise

nonzero value if true; 0 otherwise

Return a nonzero value if true; 0 otherwise

nonzero value if true; 0otherwise

(hex 0x21-0x7e;octal 041 -1 76) Return a nonzero value if true; 0otherwise

nonzero value if true; 0otherwise

nonzero value if true; 0otherwise

(hex Ox20-0x7e; octal 040-176) Return a nonzero value if true; 0otherwise

Return a nonzero value if true; 0 otherwise

Return a nonzero value if true; 0otherwise

Return a nonzero value if true; 0 otherwise

Return a nonzero value if true; 0 otherwise

Trang 3

489

l o g 10 ( d ) double Return the logarithm (base 10) of d math h

m a l l o c ( u ) void* Allocate U bytes of memory Return a pointer m a l l o c h, or

to the beginning of the allocated space s t d l i b h pow(dl,d2) double Return d l raised to the d2 power math h

p r i n t f ( ) int Send data items to the standard output device s t d i o h

(arguments are complicated -see Appendix G)

int Send a single character to file f s t d i o h int Send a single character to the standard output device s t d i o h int Send string s to the standard output device s t d i o h

void Move the pointer to the beginning of file f s t d i o h int Enter data items from the standard input device s t d i o h

(arguments are complicated -see Appendix G)

s i n h ( d ) double Return the hyperbolic sine of d math.h

srand ( u ) void Initialize the random number generator s t d l i b h

s t rcmp ( s l ,s2) int Compare two strings lexicographically Return a s t r i n g h

negative value if s l < s2; 0 if s l and s2 are identical; and a positive value if s l s2

s t r c m p i ( s l , s 2 ) int Compare two strings lexicographically, without regard s t r i n g h

to case Return a negative value if s l < s2; 0 if s l and s2 are identical; and a positive value if s l > s2

s t r c p y ( s 1 , s 2 ) char* Copy string s2 to string s l s t r i n g h

s t r l e n ( s ) int Return the number of characters in a string s t r i n g h

s t r s e t ( s , c ) char* Set all characters within s to c (excluding the s t r i n g h

terminating null character \O)

system( s) int Pass command s to the operating system s t d l i b h

Return 0 if the command is successfully executed;

otherwise, return a nonzero value, typically -1

t a n h ( d ) double Return the hyperbolic tangent of d math h

t i m e ( p ) long int Return the number of seconds elapsed t i m e .h

beyond a designated base time

t o a s c i i ( c ) int Convert value of argument to ASCII c t y p e h

t o l o w e r ( c ) int Convert letter to lowercase c t y p e h, or

s t d l i b h

t o u p p e r ( c ) int Convert letter to uppercase c t y p e h, or

s t d l i b h

Notes: Type refers to the data type of the quantity that is returned by the function An asterisk (*) denotes a pointer

c denotes a character-type argument

d denotes a double-precision argument

Trang 4

f denotes a file argument

i denotes an integer argument

1denotes a long integer argument

p denotes a pointer argument

s denotes a string argument

U denotes an unsigned integer argument

Most commercial C compilers are accompanied by many more library functions Consult the C reference manual for your particular compiler for more detailed information on each of the above functions, and for a listing of additional functions

Trang 5

Chapter 1

does not contain any variables, The line containing p r i n t f is an output statement There are no assignment or input statements

This program also prints the message Welcome t o the Wonderful World o f Computing! The program does not contain any variables (MESSAGE is a symbolic constant, not a variable.) The line containing p r i n t f is an output statement There are no assignment or input statements

This program calculates the area of a triangle from its base and height The variables are base, h e i g h t and area The alternating p r i n t f - scanf statements provide interactive input The final p r i n t f statement is an output statement The statement that begins with area = is an assignment statement

(6) This program calculates net (after tax) salary, given the gross salary and the tax rate (which is expressed as

a constant 14%) The variables are gross, t a x and net The initial p r i n t f -scanf statements provide interactive input The final two p r i n t f statements are output statements Note that the statements containing t a x = and net = are assignment statements

This program uses a function to determine the smaller of two integer quantities The variables are a, b and min The alternating pairs of p r i n t f -scanf statements provide interactive input The final p r i n t f statement is an output statement The statement min = smaller (a, b ) references the function, which is called smaller This function contains an i f - e l s e statement that returns the smaller of the two quantities to the main portion of the program

v) This program processes n pairs of integer quantities, and determines the smaller of each pair A f o r loop is used to process the multiple pairs of integer quantities In all other respects, this program is similar to that shown in part (e)

(9) This program processes an unspecified number of pairs of integer quantities, and determines the smaller of each pair The computation continues until a pair of zeros are entered into the computer A w h i l e loop is used to process the multiple pairs of integer quantities In all other respects, this program is similar to that shown in part v)

This program processes an unspecified number of pairs of integer quantities, and determines the smaller of each pair The original values and the corresponding minimum values are stored in the arrays a, b and min

Each array can store as many as 100 integer values

After all of the data have been entered and all of the minimum values have been determined, the number of data sets is “tagged” with the assignment statement n = - -i;then a f o r loop is used to display the data In all other respects, this program is similar to that shown in part (9)

Dash (minus sign) is not allowed

An identifier must begin with a letter or an underscore

Distinct ( c ) Identical (e) Distinct

Valid (real)

Illegal character (,)

Valid (real)

491

Trang 6

Valid (real)

Valid (decimal integer)

Valid (long integer)

Valid (real)

Illegal character (blank space)

Valid (octal constant)

Illegal characters (C , 0, F), if intended as an octal constant If intended as a hexadecimal constant, an X

or an x must be included (Le., OX18CDF)

Valid (hexadecimal long integer)

Valid (null-character escape sequence)

A character constant cannot consist of multiple characters

Valid (octal escape sequence) Note that octal 52 is equivalent to decimal 42 In the ASCII character set, this value represents an asterisk (*)

A string constant must be enclosed in double quotation marks

Quotation marks and (optionally) the apostrophe within the string must be expressed as escape sequences;

i.e., "The p r o f e s s o r s a i d , \ " P l e a s e don\ I t s l e e p i n c l a s s \ It

c h a r c l = Owl, c 2 = I & ' ; c h a r eol = ' \ , I ;

double d l = 2 8 8 e - 8 , d2 = - 8 4 e 5 ; (d) char message[] = " E R R O R " ;

i n t U = 0711, v = O x f f f f ;

Subtract the value of b from the value of a

Add the values of b and c, then multiply the sum by the value of a

Add the values of b and c and multiply the sum by the value of a Then assign the result to d

Determine whether or not the value of a is greater than or equal to the value of b The result will be either true or false, represented by the value 1 (true) or 0 (false)

Divide the value of a by 5, and determine whether or not the remainder is equal to zero The result will be either true or false

v) Divide the value of b by the value of c, and determine whether or not the value of a is less than the quotient The result will be either true or false

Decrement the value of a; i.e., decrease the value of a by 1

Expression statement

Control statement containing a compound statement (The compound statement is enclosed in braces.)

Trang 7

493

ANSWERS TO SELECTED PROBLEMS

Control statement

Compound statement containing expression statements and a control statement

Compound statement containing an expression statement and a control statement The control statement

itself contains two compound statements

Trang 8

Return the absolute value of the integer expression ( i - 2 * j )

Return the absolute value of the floating-point expression ( x + y )

Determine if the character represented by c is a printing ASCII character

Determine if the character represented by c is a decimal digit

Convert the character represented by c to uppercase

Round the value of x up to the next higher integer

Round the value of ( x + y ) down to the next lower integer

Determine if the character represented by c is lowercase

Determine if the character represented by j is uppercase

Return the value ex

Return the natural logarithm of x

Return the square root of the expression ( x * x + y*y )

Determine if the value of the expression ( 10 * j ) can be interpreted as an alphanumeric character

Determine if the value of the expression ( 10 * j ) can be interpreted as an alphabetic character Determine if the value of the expression ( 10 * j ) can be interpreted as an ASCII character

Convert the value of the expression ( 10 * j ) to an ASCII character

Divide the value of x by the value of y, and return the remainder with the same sign as x

Convert the ASCII character whose numerical code is 65 to lowercase

Trang 9

ANSWERS TO SELECTED PROBLEMS 495

(s) Determine the difference between the value of x and the value of y, then raise this difference to the 3.0

power

( f ) Evaluate the expression ( x - y ) and return its sine

(U) Return the number of characters in the string 'I he110"

(v) Return the position of the first occurrence of the letter e in the string h e l l o"

or scanf ("%c %c %c", &a, &b, &c);

The method used in Prob 4.53 indicates the number of characters that have been read

( 6 ) scanf ( " % d %o %x', &i,& j , & k ) ;

(c) scanf ("%x %x %on, &i,& j , &k) ;

Trang 10

decimal integer with a maximum field-width of 5; c and d will be assigned double-precision quantities with maximum field-widths of 15

a will be assigned a long hexadecimal integer with a maximum field-width of 10; b will be assigned a short octal integer with a maximum field-width of 6; c will be assigned a short unsigned integer with a maximum field-width of 6; and d will be assigned a long unsigned integer with a maximum field-width of 14

a will be assigned a long decimal integer with a maximum field-width of 12; b will be assigned a short decimal integer whose maximum field-width is unspecified; c and d will be assigned floating-point quantities with maximum field-widths of 15

a will be assigned a decimal integer with a maximum field-width of 8; another decimal integer will then be

read into the computer but not assigned; c and d will then be assigned double-precision quantities with maximum field-widths of 12

or s c a n f ( " % d %d %f % l f " , &i,& j , &x, &dx);

s c a n f ( ' % d % I d %d %f% U " , &i, & i x , &I,&x, & U ) ;

s c a n f ( ' % d %U % c " , &i,&U, & c ) ;

scanf ('%c %f %If %hd", &c, & x , &dx, as);

or scanf ( "%c %e % l e %hd" , &c, &x, &dx, a s );

&j,

or s c a n f ( " % 4 d %4d %8f % 1 5 1 f n , &i,&x, & d x ) ;

scanf("5d %121d %5d %10f % 5 u " , &i, & i x , & j , &x, & U ) ;

scanf ("%6d %6u % c " , &i,& U , & c ) ;

scanf ( "%c %9f %161f %6hd" &c, &x, &dx, a s );

or s c a n f ( " % c %9e %161e %6hd", &c, &x, &dx, & s ) ;

4.63 scanf ( " % [ ^ * I " , t e x t ) ;

$ * @ (one or more blank spaces between the characters)

$ * @ (one or more whitespace characters between the input characters)

$ * @ (one or more blank spaces between the characters Other whitespace characters may also appear with the blank spaces.)

$*@(no spaces separating the characters)

Trang 11

ANSWERS TO SELECTED PROBLEMS 497

Note: e-or g-type conversion could also be used, e.g.,

Trang 12

2.500000e+000 5.000000e-004 3.000000e+003

2.500000e+000 5.OOOOOOe-004 3.000000e+003

2.5000e+000 5.0000e-004 3.0000e+003

2.50e+000 5.00e-004 3.00e+003

Program Program

4.83 ( a ) p r i n t f ( "Please e n t e r your name: I' ) ;

Trang 13

499

1

ANSWERS TO SELECTED PROBLEMS

( b ) / * "WELCOME - LET'S BE FRIENDS" program * /

Trang 14

U> / * mass o f a i r i n an automobile t i r e * /

p r i n t f ( " \ n \ n D e c o d e d ( o r i g i n a l ) t e x t : \ n " ) ;

f o r ( c o u n t = 0; count c t a g ; ++count)

p u t c h a r ( t e x t [ c o u n t ] + 3 0 ) ;

Trang 15

ANSWERS TO SELECTED PROBLEMS 501

0) / * r e v e r s i n g uppercase and lowercase l e t t e r s i n a l i n e o f t e x t * /

1

Chapter 6

positive value, and the value of -xmin is assigned to x if x has a negative value or if x equals zero This is not a compound statement, and there are no embedded compound statements

(2) The do -w h i l e statement, which is embedded in the program segment, contains a compound statement

(3) The i f statement, which is embedded in the do -w h i l e statement, contains a compound statement

Trang 17

ANSWERS TO SELECTED PROBLEMS 503

case ' g o :

case ' G ' :

p r i n t f ("GREEN' ) ; break;

Trang 18

A s w i t c h statement cannot be used because:

(a) The tests involve floating-point quantities rather than integer quantities

(b) The tests involve ranges of values rather than exact values

7.32 f accepts an integer argument and returns an integer quantity

f accepts two arguments and returns a double-precision quantity The first argument is a double-precision quantity, and the second is an integer

f accepts three arguments and returns nothing The first function is a long integer, the second is a short integer and the third is an unsigned integer

f does not accept any argument but returns a single character

f accepts two unsigned integer arguments and returns an unsigned integer

7.33 f accepts two floating-point arguments and returns a floating-point value

f accepts a long integer and returns a long integer

f accepts an integer and returns nothing

f accepts nothing but returns a character

Trang 20

e x t e r n double f u n c t l (double x, double y ) ; / * added * /

e x t e r n double f u n c t 2 ( d o u b l e x, double y ) ; / * added * /

name is a one-dimensional, 30-element character array

c is a one-dimensional, 6-element floating-point array

a is a one-dimensional, 50-element integer array

params is a two-dimensional, 25-element integer array ( 5 rows, 5 columns)

memo is a two-dimensional, 8712-element character array (66 rows, 132 columns)

accounts is a three-dimensional, 80,000-element double-precision array (50 pages, 20 rows, 80 columns)

c is a one-dimensional, 8-element floating-point array

c [ O ] = 2 c [ l ] = 5 c [ 2 ] = 3 c [ 3 ] = -4

c [ 4 ) = 12 c [ 5 ] = 12 c [ 6 ] = 0 c [ 7 ] = 8

Trang 21

507

ANSWERS TO SELECTED PROBLEMS

c is a one-dimensional, 8-element floating-point array

zis a one-dimensional, 12-element integer array

z[21 = 8 z[51 = 6 All other elements are assigned zeros

flag[41 is assigned zero

c is a three-dimensional, 2 x 3 x 4 integer array

Trang 22

(d) float consts[6] = (0.005, -0,032, le-6, 0.167, -0.3e8, 0,015);

float x ; double values[50];

Trang 23

9.3 1 20 (sum of the array elements whose values are even)

25 (sum of the even array elements)

Will not run (automatic arrays cannot be initialized)

25 (sum of the external array elements whose values are odd)

1 (smallest value)

1 5 9 (smallest value within each row)

9 10 1 1 12 (largest value within each column)

8 10 10 1 2 (if the value of an element is odd, reduce its value by 1;then display the entire array) PPoorrmmiiggwwtt aa e e g g e e t t f f nn (skip the even-numbered array elements; print each odd- numbered array element twice)

Chapter 10

( b ) a and b are floating-point variables; pa and pb are pointers to floating-point quantities (though not necessarily to a and b)

( c ) a is a floating-point variable whose initial value is -0.167; pa is a pointer to a floating-point quantity; the address of a is assigned to pa as an initial value

(6) c l , c2 and c3 are char-type variables; p c l , pc2 and pc3 are pointers to characters; the address of c l is assigned to pc3

( e ) f u n c t is a function that accepts three arguments and returns a double-precision quantity The first two arguments are pointers to double-precision quantities; the third argument is a pointer to an integer quantity

Ngày đăng: 13/08/2014, 18:20

TỪ KHÓA LIÊN QUAN