Instructors Manual for C How to Program, 4 e (Deitel Deitel) (Z-Library)

Author: Deitel & Deitel

教育

No Description

📄 File Format: PDF
💾 File Size: 2.6 MB
46
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Instructor’s Manual for C How to Program, 4/e Deitel & Deitel
📄 Page 2
Contents 1 Introduction to Computers, the Internet and the World Wide Web 1 2 Introduction to C Programming 5 3 Structured Program Development in C 19 4 C Program Control 55 5 C Functions 97 6 C Arrays 169 7 Pointers 233 8 C Characters and Strings 283 9 C Formatted Input/Output 319 10 Structures, Unions, Bit Manipulations and Enumerations 333 11 C File Processing 353 12 Data Structures 375
📄 Page 3
II 13 The Preprocessor 441 14 Other C Topics 447 15 C++ as a “Better C” 457 16 C++ Classes and Data Abstraction 463 17 C++ Classes: Part II 485 18 C++ Operator Overloading 493 19 C++ Inheritance 499 20 C++ Virtual Functions and Polymorphism 511 21 C++ Stream Input/Output 519 22 C++ Templates 537 23 C++ Exception Handling: Solution 543 24 Introduction to Java Applications and Applets 547 25 Beyond C & C++: Operators, Methods & Arrays in Java 557 26 Java Object-Based Programming 585 27 Java Object-Oriented Programming 603 28 Java Graphics and Java2D 617 29 Java Graphical User Interface Components 633 30 Java Multimedia: Images, Animation, and Audio 661
📄 Page 4
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Introduction to Computers, the Internet and the World Wide Web: Solutions SOLUTIONS 1.3 Categorize each of the following items as either hardware or software: a) CPU ANS: hardware. b) C compiler ANS: software. c) ALU ANS: hardware. d) C preprocessor ANS: software. e) input unit ANS: hardware. f) a word processor program ANS: software. 1.4 Why might you want to write a program in a machine-independent language instead of a machine-dependent language? Why might a machine-dependent language be more appropriate for writing certain types of programs? ANS: Machine independent languages are useful for writing programs to be executed on multiple computer platforms. Machine dependent languages are appropriate for writing programs to be executed on a single platform. Machine dependent languages tend to exploit the efficiencies of a particular machine. 1.5 Translator programs such as assemblers and compilers convert programs from one language (referred to as the source lan- guage) to another language (referred to as the object language). Determine which of the following statements are true and which are false: a) A compiler translates high-level language programs into object language. ANS: True. b) An assembler translates source language programs into machine language programs. ANS: True. c) A compiler converts source language programs into object language programs. ANS: False. d) High-level languages are generally machine-dependent. ANS: False. e) A machine language program requires translation before the program can be run on a computer. ANS: False.
📄 Page 5
2 Introduction to Computers, the Internet and the World Wide Web: Solutions Chapter 1 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1.6 Fill in the blanks in each of the following statements: a) Devices from which users access timesharing computer systems are usually called . ANS: terminals. b) A computer program that converts assembly language programs to machine language programs is called . ANS: an assembler. c) The logical unit of the computer that receives information from outside the computer for use by the computer is called . ANS: The input unit. d) The process of instructing the computer to solve specific problems is called . ANS: computer programming. e) What type of computer language uses English-like abbreviations for machine language instructions? . ANS: a high-level language. f) Which logical unit of the computer sends information that has already been processed by the computer to various de- vices so that the information may be used outside the computer? . ANS: The output unit. g) The general name for a program that converts programs written in a certain computer language into machine language is . ANS: compiler. h) Which logical unit of the computer retains information? . ANS: memory unit and secondary storage unit. i) Which logical unit of the computer performs calculations? . ANS: arithmetic and logical unit. j) Which logical unit of the computer makes logical decisions? . ANS: arithmetic and logical unit k) The commonly used abbreviation for the computer's control unit is . ANS: CPU. l) The level of computer language most convenient to the programmer for writing programs quickly and easily is . ANS: high-level language. m) The only language that a computer can directly understand is called that computer's . ANS: machine language. n) Which logical unit of the computer coordinates the activities of all the other logical units? . ANS: central processing unit. 1.7 State whether each of the following is true or false. If false, explain your answer. a) Machine languages are generally machine dependent. ANS: True. Machine languages are closely related to the hardware of a particular machine. b) Timesharing truly runs several users simultaneously on a computer. ANS: False. Time sharing systems split CPU time amongst several users so that the users appear to be operating simulta- neously c) Like other high-level languages, C is generally considered to be machine independent. ANS: True. C programs can be written on most machines, and with some care, C programs can be written on one machine and run on many machines with few changes or no changes. 1.8 Discuss the meaning of each of the following names: a) stdin ANS: This refers to the standard input device. The standard input device is normally connected to the keyboard b) stdout ANS: This refers to the standard output device. The standard output device is normally connected to the computer screen. c) stderr ANS: This refers to the standard error device. Error messages are normally sent to this device which is typically connected to the computer screen. 1.9 Why is so much attention today focused on object-oriented programming in general and C++ in particular? ANS: Object-oriented programming enables the programmer to build reusable software components that model items in the real world. Building software quickly, correctly, and economically has been an elusive goal in the software industry. The modular, object-oriented design and implementation approach has been found to increase productivity 10 to 100 times over conventional programming languages while reducing development time, errors, and cost. C++ is used for object-ori- ented programming because it is a superset of the C programming language and C is widely used.
📄 Page 6
Chapter 1 Introduction to Computers, the Internet and the World Wide Web: Solutions 3 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1.10 Which programming language is best described by each of the following? a) Developed by IBM for scientific and engineering applications. ANS: FORTRAN b) Developed specifically for business applications. ANS: COBOL c) Developed for teaching structured programming. ANS: Pascal d) Named after the world’s first computer programmer. ANS: Ada e) Developed to familiarize novices with programming techniques. ANS: BASIC f) Specifically developed to help programmers migrate to .NET. ANS: C# g) Known as the development language of UNIX. ANS: C h) Formed primarily by adding object-oriented programming to C. ANS: C++ i) Succeeded initially because of its ability to create Web pages with dynamic content. ANS: Java
📄 Page 7
4 Introduction to Computers, the Internet and the World Wide Web: Solutions Chapter 1 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
📄 Page 8
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Introduction to C Programming: Solutions SOLUTIONS: 2.7 Identify and correct the errors in each of the following statements (Note: there may be more than one error per statement): a) scanf( "d", value ); ANS: scanf( “%d”, &value ); b) printf( "The product of %d and %d is %d"\n, x, y ); ANS: printf( “The product of %d and %d is %d\n”, x, y, z ); c) firstNumber + secondNumber = sumOfNumbers ANS: sumOfNumbers = firstNumber + secondNumber; d) if ( number => largest ) largest == number; ANS: if ( number >= largerst ) largest = number; e) */ Program to determine the largest of three integers /* ANS: /* Program to determine the largest of three integers */ f) Scanf( "%d", anInteger ); ANS: scanf( “%d”, &anInteger ); g) printf( "Remainder of %d divided by %d is\n", x, y, x % y ); ANS: printf( “Remainder of %f divided by %d is %d\n”, x, y, x % y ); h) if ( x = y ); printf( %d is equal to %d\n", x, y ); ANS: if ( x == y ) printf( “%d is equal to %d\n”, x, y ); i) print( "The sum is %d\n," x + y ); ANS: printf( “The sum is %d\n”, x + y ); j) Printf( "The value you entered is: %d\n, &value ); ANS: printf( “The value you entered is: %d\n”, value ); 2.8 Fill in the blanks in each of the following: a) are used to document a program and improve its readability. ANS: comments. b) The function used to display information on the screen is . ANS: printf.
📄 Page 9
6 Introduction to C Programming: Solutions Chapter 2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. c) A C statement that makes a decision is . ANS: if. d) Calculations are normally performed by statements. ANS: assignment. e) The function inputs values from the keyboard. ANS: scanf. 2.9 Write a single C statement or line that accomplishes each of the following: a) Print the message “Enter two numbers.” ANS: printf( “Enter two numbers\n” ); b) Assign the product of variables b and c to variable a. ANS: a = b * c; c) State that a program performs a sample payroll calculation (i.e., use text that helps to document a program). ANS: /* Sample payroll calculation program */ d) Input three integer values from the keyboard and place these values in integer variables a, b and c. ANS: scanf( “%d%d%d”, &a, &b, &c ); 2.10 State which of the following are true and which are false. If false, explain your answer. a) C operators are evaluated from left to right. ANS: False. Some operators are evaluated left to right and others are evaluated from right to left depending on their asso- ciativity (see Appendix C). b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales, his_account_total, a, b, c, z, z2. ANS: True. c) The statement printf("a = 5;"); is a typical example of an assignment statement. ANS: False. The statement prints a = 5; on the screen. d) A valid arithmetic expression containing no parentheses is evaluated from left to right. ANS: False. Multiplication, division, and modulus are all evaluated first from left to right, then addition and subtraction are evaluated from left to right. e) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h. ANS: False. Those beginning with a number are invalid. 2.11 Fill in the blanks in each of the following: a) What arithmetic operations are on the same level of precedence as multiplication? . ANS: division, modulus. b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? . ANS: The innermost pair of parenthesis. c) A location in the computer's memory that may contain different values at various times throughout the execution of a program is called a . ANS: variable. 2.12 What, if anything, prints when each of the following C statements is performed? If nothing prints, then answer “nothing.” Assume x = 2 and y = 3. a) printf( "%d", x ); ANS: 2 b) printf( "%d", x + x ); ANS: 4 c) printf( "x=" ); ANS: x= d) printf( "x=%d", x ); ANS: x=2 e) printf( "%d = %d", x + y, y + x ); ANS: 5 = 5 f) z = x + y; ANS: Nothing. Value of x + y is assigned to z. g) scanf( "%d%d", &x, &y ); ANS: Nothing. Two integer values are read into the location of x and the location of y. h) /* printf( "x + y = %d", x + y ); */ ANS: Nothing. This is a comment. i) printf( "\n" );
📄 Page 10
Chapter 2 Introduction to C Programming: Solutions 7 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ANS: A newline character is printed, and the cursor is positioned at the beginning of the next line on the screen. 2.13 Which, if any, of the following C statements contain variables involved in destructive read-in? a) scanf( "%d%d%d%d%d", &b, &c, &d, &e, &f ); b) p = i + j + k + 7; c) printf( "Destructive read-in" ); d) printf( "a = 5" ); ANS: (a). 2.14 Given the equation y = ax3 + 7, which of the following, if any, are correct C statements for this equation? a) y = a * x * x * x + 7; b) y = a * x * x * ( x + 7 ); c) y = ( a * x ) * x * ( x + 7 ); d) y = ( a * x ) * x * x + 7; e) y = a * ( x * x * x ) + 7; f) y = a * x * ( x * x + 7 ); ANS: (a), (d), and (e). 2.15 State the order of evaluation of the operators in each of the following C statements and show the value of x after each state- ment is performed. a) x = 7 + 3 * 6 / 2 - 1; ANS: * is first, / is second, + is third, and - is fourth. Value of x is 15. b) x = 2 % 2 + 2 * 2 - 2 / 2; ANS: % is first, * is second, / is third, + is fourth, - is fifth. Value of x is 3. c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ); ANS: 5 6 4 2 3 1. Value of x is 338. 2.16 Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, quotient and remainder of the two numbers. ANS: 1 /* Exercise 2.16 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int x; /* define first number */ 7 int y; /* define second number */ 8 9 printf( "Enter two numbers: "); /* prompt user */ 10 scanf( "%d%d", &x, &y ); /* read values from keyboard */ 11 12 /* output results */ 13 printf( "The sum is %d\n", x + y ); 14 printf( "The product is %d\n", x * y ); 15 printf( "The difference is %d\n", x - y ); 16 printf( "The quotient is %d\n", x / y ); 17 printf( "The modulus is %d\n", x % y ); 18 19 return 0; /* indicate successful termination */ 20 21 } /* end main */ Enter two numbers: 20 5 The sum is 25 The product is 100 The difference is 15 The quotient is 4 The modulus is 0
📄 Page 11
8 Introduction to C Programming: Solutions Chapter 2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.17 Write a program that prints the numbers 1 to 4 on the same line. Write the program using the following methods. a) Using one printf statement with no conversion specifiers. b) Using one printf statement with four conversion specifiers. c) Using four printf statements. ANS: 2.18 Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words “is larger.” If the numbers are equal, print the message “These numbers are equal.” Use only the single-selection form of the if statement you learned in this chapter. ANS: 1 /* Exercise 2.17 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 printf( "1 2 3 4\n\n" ); /* part a */ 7 8 printf( "%d %d %d %d\n\n", 1, 2, 3, 4 ); /* part b */ 9 10 printf( "1 " ); /* part c */ 11 printf( "2 " ); 12 printf( "3 " ); 13 printf( "4\n" ); 14 15 return 0; /* indicates successful termination */ 16 17 } /* end main */ 1 2 3 4 1 2 3 4 1 2 3 4 1 /* Exercise 2.18 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int x; /* define first number */ 7 int y; /* define second number */ 8 9 printf( "Enter two numbers: " ); /* prompt */ 10 scanf( "%d%d", &x, &y ); /* read two integers */ 11 12 /* compare the two numbers */ 13 if ( x > y ) { 14 printf( "%d is larger\n", x ); 15 } /* end if */ 16 17 if ( x < y ) { 18 printf( "%d is larger\n", y ); 19 } /* end if */ 20 21 if ( x == y ) { 22 printf( "These numbers are equal\n" ); 23 } /* end if */ 24
📄 Page 12
Chapter 2 Introduction to C Programming: Solutions 9 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.19 Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the product, the smallest and the largest of these numbers. Use only the single-selection form of the if statement you learned in this chapter. The screen dialogue should appear as follows: ANS: 25 return 0; /* indicate successful termination */ 26 27 } /* end main */ Enter two numbers: 5 20 20 is larger Enter two numbers: 239 92 239 is larger Enter two numbers: 17 17 These numbers are equal Input three different integers: 13 27 14 Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27 1 /* Exercise 2.19 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int a; /* define first integer */ 7 int b; /* define second integer */ 8 int c; /* define third integer */ 9 int smallest; /* smallest integer */ 10 int largest; /* largest integer */ 11 12 printf( "Input three different integers: " ); /* prompt user */ 13 scanf( "%d%d%d", &a, &b, &c ); /* read three integers */ 14 15 /* output sum, average and product of the three integers */ 16 printf( "Sum is %d\n", a + b + c ); 17 printf( "Average is %d\n", ( a + b + c ) / 3 ); 18 printf( "Product is %d\n", a * b * c ); 19 20 smallest = a; /* assume first number is the smallest */ 21 22 if ( b < smallest ) { /* is b smaller? */ 23 smallest = b; 24 } /* end if */ 25 26 if ( c < smallest ) { /* is c smaller? */ 27 smallest = c; 28 } /* end if */
📄 Page 13
10 Introduction to C Programming: Solutions Chapter 2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.20 Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the constant value 3.14159 for π. Perform each of these calculations inside the printf statement(s) and use the conversion specifier %f. [Note: In this chapter, we have discussed only integer constants and variables. In Chapter 3 we will discuss floating-point numbers, i.e., values that can have decimal points.] ANS: 29 30 printf( "Smallest is %d\n", smallest ); 31 32 largest = a; /* assume first number is the largest */ 33 34 if ( b > largest ) { /* is b larger? */ 35 largest = b; 36 } /* end if */ 37 38 if ( c > largest ) { /* is c larger? */ 39 largest = c; 40 } /* end if */ 41 42 printf( "Largest is %d\n", largest ); 43 44 return 0; /* indicate successful termination */ 45 46 } /* end main */ 1 /* Exercise 2.20 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int radius; /* circle radius */ 7 8 printf( "Input the circle radius: " ); /* prompt user */ 9 scanf( "%d", &radius ); /* read integer radius */ 10 11 /* calculate and output diameter, circumference and area */ 12 printf( "\nThe diameter is %d\n", 2 * radius ); 13 printf( "The circumference is %f\n", 2 * 3.14159 * radius ); 14 printf( "The area is %f\n", 3.14159 * radius * radius ); 15 16 return 0; /* indicate successful termination */ 17 18 } /* end main */ Input the circle radius: 9 The diameter is 18 The circumference is 56.548620 The area is 254.468790
📄 Page 14
Chapter 2 Introduction to C Programming: Solutions 11 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.21 Write a program that prints a box, an oval, an arrow and a diamond as follows: ANS: 2.22 What does the following code print? printf( "*\n**\n***\n****\n*****\n" ); ANS: 2.23 Write a program that reads in five integers and then determines and prints the largest and the smallest integers in the group. Use only the programming techniques you have learned in this chapter. ANS: ********* *** * * * * * * *** * * * * * * ***** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ********* *** * * 1 /* Exercise 2.21 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 printf( "********* *** * *\n" ); 7 printf( "* * * * *** * *\n" ); 8 printf( "* * * * ***** * *\n" ); 9 printf( "* * * * * * *\n" ); 10 printf( "* * * * * * *\n" ); 11 printf( "* * * * * * *\n" ); 12 printf( "* * * * * * *\n" ); 13 printf( "* * * * * * *\n" ); 14 printf( "********* *** * *\n" ); 15 16 return 0; /* indicates successful termination */ 17 18 } /* end main */ * ** *** **** ***** 1 /* Exercise 2.23 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int largest; /* largest integer */ 7 int smallest; /* smallest integer */ 8 int int1; /* define int1 for user input */ 9 int int2; /* define int2 for user input */
📄 Page 15
12 Introduction to C Programming: Solutions Chapter 2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.24 Write a program that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2.] ANS: 10 int int3; /* define int3 for user input */ 11 int temp; /* temporary integer for swapping */ 12 13 printf( "Input 5 integers: " ); /* prompt user and read 5 ints */ 14 scanf( "%d%d%d%d%d", &largest, &smallest, &int1, &int2, &int3 ); 15 16 if ( smallest > largest ) { /* make comparisons */ 17 temp = largest; 18 largest = smallest; 19 smallest = temp; 20 } /* end if */ 21 22 if ( int1 > largest ) { 23 largest = int1; 24 } /* end if */ 25 26 if ( int1 < smallest ) { 27 smallest = int1; 28 } /* end if */ 29 30 if ( int2 > largest ) { 31 largest = int2; 32 } /* end if */ 33 34 if ( int2 < smallest ) { 35 smallest = int2; 36 } /* end if */ 37 38 if ( int3 > largest ) { 39 largest = int3; 40 } /* end if */ 41 42 if ( int3 < smallest ) { 43 smallest = int3; 44 } /* end if */ 45 46 printf( "The largest value is %d\n", largest ); 47 printf( "The smallest value is %d\n", smallest ); 48 49 return 0; /* indicate successful termination */ 50 51 } /* end main */ Input 5 integers: 9 4 5 8 7 The largest value is 9 The smallest value is 4 1 /* Exercise 2.24 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int integer; /* integer input by user */ 7 8 printf( "Input an integer: " ); /* prompt */
📄 Page 16
Chapter 2 Introduction to C Programming: Solutions 13 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.25 Print your initials in block letters down the page. Construct each block letter out of the letter it represents as shown below. ANS: 9 scanf( "%d", &integer ); /* read integer */ 10 11 /* test if integer is even */ 12 if ( integer % 2 == 0 ) { 13 printf( "%d is an even integer\n", integer ); 14 } /* end if */ 15 16 /* test if integer is odd */ 17 if ( integer % 2 != 0 ) { 18 printf( "%d is an odd integer\n", integer ); 19 } /* end if */ 20 21 return 0; /* indicate successful termination */ 22 23 } /* end main */ Input an integer: 78 78 is an even integer Input an integer: 79 79 is an odd integer PPPPPPPPP P P P P P P P P JJ J J J JJJJJJJ DDDDDDDDD D D D D D D DDDDD 1 /* Exercise 2.25 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 printf( "PPPPPPPPP\n" ); 7 printf( " P P\n" ); 8 printf( " P P\n" ); 9 printf( " P P\n" ); 10 printf( " P P\n" ); 11 printf( "\n" ); 12 printf( " JJ\n" ); 13 printf( " J\n" ); 14 printf( "J\n" );
📄 Page 17
14 Introduction to C Programming: Solutions Chapter 2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.26 Write a program that reads in two integers and determines and prints if the first is a multiple of the second. [Hint: Use the remainder operator.] ANS: 15 printf( " J\n" ); 16 printf( " JJJJJJJ\n" ); 17 printf( "\n" ); 18 printf( "DDDDDDDDD\n" ); 19 printf( "D D\n" ); 20 printf( "D D\n" ); 21 printf( " D D\n" ); 22 printf( " DDDDD\n" ); 23 24 return 0; /* indicate successful termination */ 25 26 } /* end main */ 1 /* Exercise 2.26 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int integer1; /* first integer */ 7 int integer2; /* second integer */ 8 9 printf( "Input two integers: " ); /* prompt user */ 10 scanf( "%d%d", &integer1, &integer2 ); /* read two integers */ 11 12 /* use remainder operator */ 13 if ( integer1 % integer2 == 0 ) { 14 printf( "%d is a multiple of %d ", integer1, integer2 ); 15 printf( "by a factor of %d\n", integer1 / integer2 ); 16 } /* end if */ 17 18 if ( integer1 % integer2 != 0 ) { 19 printf( "%d is not a multiple of %d\n", integer1, integer2 ); 20 } /* end if */ 21 22 return 0; /* indicate successful termination */ 23 24 } /* end main */ Input two integers: 88 11 88 is a multiple of 11 by a factor of 8 Input two integers: 777 5 777 is not a multiple of 5
📄 Page 18
Chapter 2 Introduction to C Programming: Solutions 15 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.27 Display the following checkerboard pattern with eight printf statements and then display the same pattern with as few printf statements as possible. ANS: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1 /* Exercise 2.27 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 printf( "With eight printf() statements: \n" ); 7 8 printf( "* * * * * * * *\n" ); 9 printf( " * * * * * * * *\n" ); 10 printf( "* * * * * * * *\n" ); 11 printf( " * * * * * * * *\n" ); 12 printf( "* * * * * * * *\n" ); 13 printf( " * * * * * * * *\n" ); 14 printf( "* * * * * * * *\n" ); 15 printf( " * * * * * * * *\n" ); 16 17 printf( "\nNow with one printf() statement: \n" ); 18 19 printf( "* * * * * * * *\n * * * * * * * *\n" 20 "* * * * * * * *\n * * * * * * * *\n" 21 "* * * * * * * *\n * * * * * * * *\n" 22 "* * * * * * * *\n * * * * * * * *\n" ); 23 24 return 0; /* indicate successful termination */ 25 26 } /* end main */ With eight printf() statements: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Now with one printf() statement: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
📄 Page 19
16 Introduction to C Programming: Solutions Chapter 2 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.28 Distinguish between the terms fatal error and non-fatal error. Why might you prefer to experience a fatal error rather than a non-fatal error? ANS: A fatal error causes the program to terminate prematurely. A nonfatal error occurs when the logic of the program is incorrect, and the program does not work properly. A fatal error is preferred for debugging purposes. A fatal error imme- diately lets you know there is a problem with the program, whereas a nonfatal error can be subtle and possibly go unde- tected. 2.29 Here’s a peek ahead. In this chapter you learned about integers and the type int. C can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. C uses small integers internally to represent each different character. The set of characters a computer uses and the corresponding integer representations for those characters is called that computer’s character set. You can print the integer equivalent of uppercase A for example, by executing the statement printf( "%d", 'A' ); Write a C program that prints the integer equivalents of some uppercase letters, lowercase letters, digits and special symbols. As a minimum, determine the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character. ANS: 2.30 Write a program that inputs one five-digit number, separates the number into its individual digits and prints the digits sep- arated from one another by three spaces each. [Hint: Use combinations of integer division and the remainder operation.] For exam- ple, if the user types in 42139, the program should print 1 /* Exercise 2.29 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 char intEquivalent; /* letter, digit or character */ 7 8 printf( "Input a letter, digit, or character: " ); /* prompt */ 9 scanf( "%c", &intEquivalent ); /* read user input */ 10 11 printf( "%c's integer equivalent is %d\n", intEquivalent, 12 intEquivalent ); 13 14 return 0; /* indicate successful termination */ 15 16 } /* end main */ Input a letter, digit, or character: % %'s integer equivalent is 37 Input a letter, digit, or character: y y's integer equivalent is 121 Input a letter, digit, or character: 0 0's integer equivalent is 48 4 2 1 3 9
📄 Page 20
Chapter 2 Introduction to C Programming: Solutions 17 © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ANS: 2.31 Using only the techniques you learned in this chapter, write a program that calculates the squares and cubes of the numbers from 0 to 10 and uses tabs to print the following table of values: ANS: 1 /* Exercise 2.30 Solution */ 2 #include <stdio.h> 3 4 int main() 5 { 6 int number; /* number input by user */ 7 int temp1; /* first temporary integer */ 8 int temp2; /* second temporary integer */ 9 10 printf( "Enter a five-digit number: " ); /* prompt user */ 11 scanf( "%d", &number ); /* read integer */ 12 13 printf( "%d ", number / 10000 ); /* print left-most digit */ 14 temp2 = number % 10000; 15 16 printf( " %d ", temp2 / 1000 ); 17 temp1 = temp2 % 1000; 18 19 printf( " %d ", temp1 / 100 ); 20 temp2 = temp1 % 100; 21 22 printf( " %d ", temp2 / 10 ); 23 temp1 = temp2 % 10; 24 25 printf( " %d\n", temp1 ); /* print right-most digit */ 26 27 return 0; /* indicate successful termination */ 28 29 } /* end main */ Enter a five-digit number: 23456 2 3 4 5 6 number square cube 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 1 /* Exercise 2.31 Solution */ 2 #include <stdio.h> 3 4 int main() 5 {
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List