Homework Assignment #3

Download Report

Transcript Homework Assignment #3

Homework Assignment #4
J. H. Wang
May 24, 2007
Homework Assignment #4
• Chap. 5
–
–
–
–
–
–
–
Exercise 5.2: Prog. 1 (p.215)
Exercise 5.3: 1 (p.221)
Exercise 5.4: 4, Prog. 2 (p.230)
Exercise 5.7: 1, Prog. 1 (p.249)
Exercise 5.8: Prog. 1 (p.254)
Quick-Check Exercises: 7, 10 (p. 270)
Programming Projects: 2 (p. 273)
• Deadline:
– Two weeks
Details
•
Exercise 5.2: (p.215)
– Prog. 1. Write a program fragment that
produces this output:
0 1
1 2
2 4
3 8
4 16
5 32
6 64
• Exercise 5.3: (p.221)
– 1. What output values are displayed by the following while loop
for a data value of 5? Of 6? Of 7?
printf(“Enter an integer> “);
scanf(“%d”, &x);
product = x;
count = 0;
while (count <4)
{
printf(“%d\n”, product);
product *= x;
count += 1;
}
In general, for a data value of any number n, what does this loop
display?
• Exercise 5.4: (p.230)
– 4. What values are assigned to n, m, and p, given the
initial values i=3 and j=9?
n = ++i *--j;
m = i + j--;
p = i + j;
– Prog. 2. Write a program to display an inches-tocentimeters conversions table. The smallest and
largest number of inches in the table are input values.
Your table should give conversions in 6-inch intervals.
One inch equals 2.54 cm.
• Exercise 5.7: (p.249)
– 1. What is displayed by the following program segments,
assuming m is 3 and n is 5?
• a. for (i=1; i<=n; ++i)
{
for (j=0; j<i; ++j)
printf(“*”);
printf(“\n”);
}
• b. for (i=n; i>0; --i)
{
for (j=m; j>0; --j)
printf(“*”);
printf(“\n”);
}
– Prog. 1. Write a program that displays the multiplication table for
numbers 0 to 9.
• Exercise 5.8: (p.254)
– Prog. 1. Design an interactive input loop that
scans pairs of integers until it reaches a pair
in which the first integer evenly divides the
second.
• Quick-Check Exercises: (p. 270)
– 7. During execution of the following program segment:
a. How many times does the first call to printf execute?
b. How many times does the second call to printf
execute?
c. what is the last value displayed?
for (i=0; i<7; ++i)
{
for (j=0; j<i; ++j)
printf(“%4d”, i*j);
printf(“\n”);
}
• Quick-Check Exercises: (p. 270)
– 10. What are the values of x, y, and z after
execution of this three-statement fragment?
(initial values: x=3, y=5, z=2)
x *= y + z;
y /= 2 * z + 1;
z += x;
• Programming Projects: 2 (p. 273)
– a. Write a program that will find the smallest, largest,
and average values in a collection of N numbers. Get
the value of N before scanning each value in the
collection of N numbers.
– b. Modify your program to compute and display both
the range of values in the data collection and the
standard deviation of the data collection. To compute
the standard deviation, accumulate the sum of
squares of the data values (sum_squares) in the main
loop. After loop exits, use the formula: