Homework 4

On page 307, Question 2

Show the table of values for x, y, and z that is the output displayed by the following program. You will notice that the function sum does not follow the suggestion in the last Program Style segment of Section 6.2. You can improve this program in the programming exercise that follows.

void sum (int a, int b, int *cp);

int main ()
{
   int x, y, z;

   x = 7; y = 2;

   printf ("   x   y   z\n\n");

   sum (x, y, &z);
   printf ("%4d%4d%4d\n", x, y, z);

   sum (y, x, &z);
   printf ("%4d%4d%4d\n", x, y, z);

   sum (z, y, &x);
   printf ("%4d%4d%4d\n", x, y, z);

   sum (z, z, &x);
   printf ("%4d%4d%4d\n", x, y, z);

   sum (y, y, &y);
   printf ("%4d%4d%4d\n", x, y, z);

   return 0;
}

void sum (int a, int b, int *cp)
{
   *cp = a + b;
}

On page 308, Programming 1

Rewrite the sum function in Self-Check Exercise 2 as a function that takes just two input arguments. The sum computed should be returned as the function's type int result. Also, write an equivalent function main that calls your sum function.

On page 314, Question 2
for (a), use the terms received, passed back, and received/passed back, instead of input, output, and input/output

void double_trouble (int *p, int y);
void trouble (int *x, int *y);

int main ()
{
   int x, y;
   trouble (&x, &y);
   printf ("x = %d, y = %d\n", x, y);
   return 0;
}

void double_trouble (int *p, int y)
{
   int x;
   x = 10;
   *p = 2 * x - y;
}

void trouble (int *x, int *y)
{
   double_trouble (x, 7);
   double_trouble (y, *x);
}

(a) Classify each formal parameter of double_trouble and trouble as received, passed back, or received/passed back.

(b) What values of x and y are displayed by the program? (Hint: Sketch the data areas of main, trouble, and double_trouble as the program executes.)

On pages 331, Question 1

Write a function called letter_grade that has a type int input parameter called points and returns through an output parameter gradep the appropriate letter grade using a straight scale (90-100 is an A, 80-89 is a B, and so on). Return through a second output parameter (just_missedp) an indication of whether the student just missed the next higher grade (true for 89, 79, and so on).

Homework 3

On pages 227-8, Questions 1, 2

1. Predict the output of this program fragment:

i = 0;
while (i <= 5) {
   printf ("%3d %3d", i, 10 - i);
   i = i + 1;
}

2. What is displayed by this program fragment for an input of 8?

scanf("%d", &n);
ev = 0;
while (ev < n) {
   printf ("%3d", ev);
   ev = ev + 2;
}
printf ("\n");

On page 233, Questions 1, 2
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?

2. What values are displayed if the call to printf comes at the end of the loop instead of at the beginning?

On page 249, Programming 1

There are 9870 people in a town whose population increases by 10 percent each year. Write a loop that displays the annual population and determines how many years (count_years) it will take for the population to surpass 30,000.

On page 266, Programming 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.

On pages 283-284, Questions 2, 5
2. Write a program that computes and displays the sum of a collection of Celsius temperatures entered at the terminal until a sentinel value of -275 is entered.

5. Rewrite the program segment that follows, using a for loop:

count = 0;
i= 0;
while (i < n) {
   scanf ("%d", &x);
   if (x ==i)
      ++count;
   ++i;
}