Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ders 4. Cevaplı sorular ve Problemler 4.3 Write a statement or a set of statements to accomplish each of the following tasks: a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer variables sum and count have been defined. ANS: sum = 0; for ( count = 1; count <= 99; count += 2 ) sum += count; b) Print the value 333.546372 in a field width of 15 characters with precisions of 1, 2, 3, 4 and 5. Left justify the output. What are the five values that print? ANS: printf( "%-15.1f\n", 333.546372 ); /* prints 333.5 */ printf( "%-15.2f\n", 333.546372 ); /* prints 333.55 */ printf( "%-15.3f\n", 333.546372 ); /* prints 333.546 */ printf( "%-15.4f\n", 333.546372 ); /* prints 333.5464 */ printf( "%-15.5f\n", 333.546372 ); /* prints 333.54637 */ c) Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the result with a precision of 2 in a field width of 10 positions. What is the value that prints? ANS: printf( "%10.2f\n", pow( 2.5, 3 ) ); /* prints 15.63 */ 4.4 Find the error in each of the following code segments and explain how to correct it. a) x = 1; while ( x <= 10 ); x++; } ANS: Error: The semicolon after the while header causes an infinite loop. Correction: Replace the semicolon with a { or remove both the ; and the }. b) for ( y = .1; y != 1.0; y += .1 ) printf( "%f\n", y ); ANS: Error: Using a floating-point number to control a for repetition statement. Correction: Use an integer, and perform the proper calculation in order to get the values you desire. for ( y = 1; y != 10; y++ ) printf( "%f\n", ( float ) y / 10 ); c) switch ( n ) { case 1: printf( "The number is 1\n" ); case 2: printf( "The number is 2\n" ); break; default: printf( "The number is not 1 or 2\n" ); break; } ANS: Error: Missing break statement in the statements for the first case. Correction: Add a break statement at the end of the statements for the first case. Note that this is not necessarily an error if you want the statement of case 2: to execute every time the case 1: statement executes. d) The following code should print the values 1 to 10. n = 1; while ( n < 10 ) printf( "%d ", n++ ); ANS: Error: Improper relational operator used in the while repetition-continuation condition. Correction: Use <= rather than <. 4.5 Find the error in each of the following (Note: there may be more than one error): a) For ( x = 100, x >= 1, x++ ) printf( "%d\n", x ); ANS: F in for should be lowercase. The infinite loop can be corrected by switching the 1 and the 100 and changing the relational operator to <=. Semicolons are needed between the for conditions, not comma operators. for ( x = 1; x <= 100; x++ ) printf( “%d\n”, x); b) The following code should print whether a given integer is odd or even: ANS: A break is needed for case 0, otherwise both statements will be printed out. switch ( value % 2 ) { case 0: printf( “Even integer\n” ); break; case 1: printf( “Odd integer\n” ); c) The following code should input an integer and a character and print them. Assume the user types as input 100 A. scanf( "%d", &intVal ); charVal = getchar(); printf( "Integer: %d\nCharacter: %c\n", intVal, charVal ); ANS: charVal will read the blank character when the user types in intVal and hits return. To correct this, scanf should be used to read in charVal. scanf( “%d”, &intVal ); scanf( “\n%c”, &charVal ); /* skips preceding blanks */ printf( “Integer: %d\nCharacter: %c\n”, intVal, charVal ); d) for ( x = .000001; x == .0001; x += .000001 ) printf( "%.7f\n", x ); ANS: In addition, floating-point numbers should never be compared with == or != due to imprecision. This imprecision often causes infinite loops to occur. To correct this, an integer variable should be used in the for loop. e) The following code should output the odd integers from 999 to 1: for ( x = 999; x >= 1; x += 2 ) printf( "%d\n", x ); ANS: loop should be decrementing not incrementing. for ( x = 999; x >= 1; x -= 2 ) printf( “%d\n”, x ); 4.6 State which values of the control variable x are printed by each of the following for statements: a) for ( x = 2; x <= 13; x += 2 ) printf( "%d\n", x ); ANS: 2, 4, 6, 8, 10, 12 b) for ( x = 5; x <= 22; x += 7 ) printf( "%d\n", x ); ANS: 5, 12, 19 c) for ( x = 3; x <= 15; x += 3 ) printf( "%d\n", x ); ANS: 3, 6, 9, 12, 15 d) for ( x = 1; x <= 5; x += 7 ) printf( "%d\n", x ); ANS: 1 e) for ( x = 12; x >= 2; x -= 3 ) printf( "%d\n", x ); ANS: 12, 9, 6, 3 4.7 Write for statements that print the following sequences of values: a) 1, 2, 3, 4, 5, 6, 7 ANS: for ( i = 1; i <= 7; i++ ) printf( “%d ”, i ); b) 3, 8, 13, 18, 23 ANS: /* increments of 5 */ for ( i = 3; i <= 23; i += 5 ) printf( “%d ”, i ); c) 20, 14, 8, 2, -4, -10 ANS: /* decrements of 6 */ for ( i = 20; i >= -10; i -= 6 ) printf( “%d ”, i ); 4.11 (Find the Smallest) Write a program that finds the smallest of several integers. Assume that the first value read specifies the number of values remaining. 4.12 (Calculating the Sum of Even Integers) Write a program that calculates and prints the sum of the even integers from 2 to 30. 4.13 (Calculating the Product of Odd Integers) Write a program that calculates and prints the product of the odd integers from 1 to 15. ANS: /* Exercise 4.13 Solution */ #include <stdio.h> int main( void ) { long i; /* counter */ long product = 1; /* current product */ /* loop through odd integers up to 15 */ for ( i = 3; i <= 15; i += 2 ) { product *= i; /* update product */ } /* end for */ printf("Product of the odd integers from 1 to 15 is: %ld\n", product ); return 0; /* indicate successful termination */ } /* end main */ 4.14 (Factorials) The factorial function is used frequently in probability problems. The factorial of a positive integer n (written n! and pronounced “n factorial”) is equal to the product of the positive integers from 1 to n. Write a program that evaluates the factorials of the integers from 1 to 5. Print the results in tabular format. What difficulty might prevent you from calculating the factorial of 20? 4.19 (Calculating Sales) An online retailer sells five different products whose retail prices are shown in the following table: Prr Product number Retail price 1 $ 2.98 2 $ 4.50 3 $ 9.98 4 $ 4.49 5 $ 6.87 Write a program that reads a series of pairs of numbers as follows: a) Product number b) Quantity sold for one day Your program should use a switch statement to help determine the retail price for each product. Your program should calculate and display the total retail value of all products sold last week. 4.24 Assume i = 1, j = 2, k = 3 and m = 2. What does each of the following statements print? a) printf( "%d", i == 1 ); ANS: 1 b) printf( "%d", j == 3 ); ANS: 0 c) printf( "%d", i >= 1 && j < 4 ); ANS: 1 d) printf( "%d", m < = 99 && k < m ); ANS: 0 e) printf( "%d", j >= i || k == m ); ANS: 1 f) printf( "%d", k + m < j || 3 - j >= k ); ANS: 0 g) printf( "%d", !m ); ANS: 0 h) printf( "%d", !( j - m ) ); ANS: 1 i) printf( "%d", !( k > m ) ); ANS: 0 j) printf( "%d", !( j > k ) ); ANS: 1