Ch06.2-Instr

advertisement
This material is provided to the professors and students using Computer Science: A Structured Approach Using C,
Second Edition. All reprints in any form must cite this copyright.
Copyright(c) 2001 by Brooks/Cole
A division of Thomson Learning
Chapter 6 Repetition
Review Questions
1.
a. True
2.
a. True
3.
b. False
4.
a. True
5.
e. The update for a pretest loop must be a part of the loop body.
6.
c. Initialization code is explicitly required in all loops.
7.
e. The number of updates always equals the number of loop iterations.
8.
d. The number of times a loop iterates must be a constant.
9.
e. Both the for and the while.
10.
b. The limit test in a while loop is made before each iteration.
11.
d. Both statements include initialization within the statements.
12.
e. The limit test in a do…while loop is executed at the beginning of each iteration.
13.
b. case
14.
e. May be linear logarithmic or quadratic
15.
c. Logarithmic
Exercises
16.
a.
b.
c.
17.
a.
b.
c.
18.
a.
b.
19.
20.
a.
b.
c.
a.
b.
21.
a.
prints "12" infinite number of times on separate lines
prints "12" infinite number of times on separate lines
prints "12" infinite number of times on separate lines
On separate lines
12 11 10 9 8
On separate lines
12 11 10 9 8
On separate lines
12 11 10 9 8
On separate lines
12 10 8
On separate lines
12 10 8
nothing
nothing
12
for (x = 0; x < 10; x++)
printf ("%d\n", x);
for (scanf ("%d", &x); x != 9999; scanf ("%d", &x))
printf ("%d\n", x);
x = 0;
if (x < 10)
{
do
{
printf ("%d\n", x);
x++;
} while (x < 10);
Page 6.1
} /* if */
scanf ("%d", &x);
if (x != 9999)
{
do
{
printf ("%d\n", x);
scanf ("%d", &x);
} while (x != 9999);
} /* if */
a. x = 1;
while (x < 100)
{
printf ("%d\n", x);
x++;
} /* while */
b. while (scanf ("%d", &x) != EOF)
printf ("%d\n", x);
a. x = 1;
if (x < 100)
{
do
{
printf ("%d\n", x);
x++;
} while (x < 100);
} /* if */
b. if (scanf ("%d", &x) != EOF)
{
do
{
printf ("%d\n", x);
} while (scanf ("%d", &x) != EOF);
} /* if */
a. x = 0;
printf ("%d\n", x);
x++;
while (x < 100)
{
printf ("%d\n", x);
x++;
} /* while */
b. res = scanf ("%d", &x);
while (res != EOF)
res = scanf ("%d", &x);
a. x = 0;
printf ("%d\n", x);
for (x++; x < 100; x++)
printf ("%d\n", x);
b. res = scanf ("%d", &x);
for (; res != EOF;)
res = scanf ("%d", &x);
This for loop will print the numbers 0 to 9 all on the same line with no spaces. The following would be one
way to correct the code:
for (num = 1; num <= 10; num++)
printf ("%d ", num);
b.
22.
23.
24.
25.
26.
Page 6.2
27.
28.
29.
30.
31.
This for loop will print the numbers 1 to 10 all on the same line with no spaces. The only correction might be
to add a space in the format string of the print statement.
a. On separate lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
b. On separate lines:
1 3 5 7 9 11 13 15 17 19
a. On separate lines:
20 19 18 17 16 15 14 13 12 11 10
b. On separate lines:
20 18 16 14 12 10 8 6 4 2
a. The numbers 1 to 20 would each be printed 5 times on separate lines:
11111
22222
.
.
.
2020202020
b. This will print 20 twenty times on a line, then prints 19 nineteen times on a line, then prints 18 eighteen
times on a line, and so on, until it prints 1 once on a single line.
a.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
b.
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
Page 6.3
4
3
32.
a.
b.
c.
2
1
Since the for loop has no limit test expression and no update expression, the loop will run forever.
Since there is no limit condition, the loop can only be exited by using one of the jump statements: break,
return or goto.
This is not good structured programming style. Good programming style follows the accepted
conventions. Programmers expect to find the expressions associated with the for loop in their proper
places within the for statement. To put them elsewhere in the loop body is not what is expected and will
therefore tend to confuse the reader. Additionally, these statements are much more error prone when
placed in the body of the loop, especially if the body has complex selection statements controlling various
parts of the code.
Problems
33.
/* This program prints a line of 60 asterisks.
Written by:
Date:
*/
#include <stdio.h>
int main (void)
{
/* Local Definitions */
int i;
/* Statements */
printf ("\n*** start of prb0633.c ***\n\n");
for (i = 0; i < 60; i++)
printf ("%c ", ‘*’);
printf ("\n");
34.
}
a.
b.
c.
d.
e.
printf ("\n*** end of prb0633.c ***\n\n");
return 0;
/* main */
for (i = 6; i <= 66; i += 2)
printf ("%d, ", i);
printf ("\b\b\n");
for (i = 7; i <= 67; i += 2)
printf ("%d, ", i);
printf ("\b\b\n");
for (sum = 0, i = 1; i <= 15; i++)
sum += i;
for (sum = 0, i = 15; i <= 45; i += 2)
sum += i;
for (sum = 0, i = 0; i < 50; i++)
{
printf ("%d + ", i * 3 + 1);
sum += (i * 3 + 1);
} /* for */
printf ("\b\b\b = %d", sum);
35.
/* Calculate average of 'n' positive floating point
numbers.
Page 6.4
Written by:
Date:
*/
#include <stdio.h>
int main
{
/* Local
int
int
int
float
float
(void)
Definitions */
i;
n;
count;
num;
sum;
/* Statements */
printf ("\n*** start of prb0635.c ***\n\n");
printf ("Please enter number of entries to use:
scanf ("%d", &n);
count = 0;
sum
= 0;
for (i = 0; i < n; i++)
{
printf ("Please enter a number: ");
scanf ("%f", &num);
if (num > 0)
{
sum += num;
count++;
} /* if */
} /* for */
printf ("\nThe average is: %f.\n", sum / count);
printf ("\n*** end of prb0635.c ***\n\n");
}
return 0;
/* main */
36.
/* Calculate average of 'n' negative floating point
numbers.
Written by:
Date:
*/
#include <stdio.h>
int main
{
/* Local
int
int
int
float
float
(void)
Definitions */
i;
n;
count;
num;
sum;
/* Statements */
printf ("\n*** start of prb0636.c ***\n\n");
Page 6.5
");
printf ("Please enter number of entries to use:
scanf ("%d", &n);
");
count = 0;
sum
= 0;
for (i = 0; i < n; i++)
{
printf ("Please enter a number: ");
scanf ("%f", &num);
if (num < 0)
{
sum += num;
count++;
} /* if */
} /* for */
printf ("\nThe average is: %f.\n", sum / count);
printf ("\n*** end of prb0636.c ***\n\n");
}
return 0;
/* main */
37.
/* Determine largest value in a number series and the
number of times it occurs.
Written by:
Date:
*/
#include <stdio.h>
#include <limits.h>
int main (void)
{
/* Local Definitions */
int num;
int max;
int count;
/* Statements */
printf ("\n*** start of prb0637.c ***\n\n");
count = 0;
max
= INT_MIN;
printf ("Enter a list of integers (EOF to stop): ");
while (scanf("%d", &num) != EOF)
{
if (num > max)
{
max = num;
count = 1;
} /* if */
else if (num == max)
count++;
} /* while !EOF */
printf ("\nLargest value %d--Entered %d times.\n",
Page 6.6
max, count);
printf ("\n*** end of prb0637.c ***\n\n");
}
return 0;
/* main */
38.
/* Create a pattern of sequential numbers.
Written by:
Date:
*/
#include <stdio.h>
int main (void)
{
/* Local Definitions */
int i;
int j;
/* Statements */
printf ("\n*** start of prb0638.c ***\n\n");
}
for (i = 9; i >= 1; i--)
{
for (j = 1; j <= i; j++)
printf ("%d ", j);
printf ("\n");
} /* for */
printf ("\n*** end of prb0638.c ***\n\n");
return 0;
/* main */
39.
/* ====================== pattern =====================
This function creates a pattern given the height and
width.
Pre Given height and width
Post pattern printed
*/
void pattern (int height,
int width)
{
/* Local Definitions */
int i;
int j;
/* Statements */
for (i = 1; i <= height; i++)
{
for (j = 1; j <= width; j++)
printf ("*");
printf ("\n");
} /* for */
}
return;
/* pattern */
40.
/* ================= pattern ================
Create a pattern given a height and width.
Page 6.7
Pre
Post
Given height and width
Pattern printed
*/
void pattern (int height,
int width)
{
/* Local Definitions */
int i;
int j;
/* Statements */
for (i = 1; i <= height; i++)
{
if (i == 1 || i == height)
for (j = 1; j <= width; j++)
printf ("=");
else
{
for (j = 1; j <= width; j++)
{
if (j == 1 || j == width)
printf ("*");
else
printf (" ");
} /* for */
} /* else */
}
printf ("\n");
} /* for */
return;
/* pattern */
41.
/* ==================== pattern ====================
This function creates a pattern given the height.
Pre
Given height
Post Pattern printed
*/
void pattern (int height)
{
/* Local Definitions */
int i;
int j;
/* Statements */
for (i = 0; i < height; i++)
{
for (j = 1; j <= i * 2 + 1; j++)
printf ("*");
printf ("\n");
} /* for */
}
return;
/* pattern */
42.
/* =================== pattern =====================
This function creates a pattern given the height.
Page 6.8
Pre
Post
Given height
Pattern printed
*/
void pattern (int height)
{
/* Local Definitions */
int i;
int j;
/* Statements */
for (i = height - 1; i >= 0; i--)
{
for (j = 1; j <= i * 2 + 1; j++)
printf ("*");
printf ("\n");
} /* for */
}
return;
/* pattern */
43.
/* ==================== pattern ====================
This function creates a pattern given the height.
Pre
Given height
Post Pattern printed
*/
void pattern (int height)
{
/* Local Definitions */
int i;
int j;
/* Statements */
for (i = 1; i <= height / 2; i++)
{
for (j = 1; j < (i * 2); j++)
printf ("*");
printf ("\n");
} /* for */
if (height % 2)
{
for (j = 1; j <= height;
printf ("*");
printf ("\n");
} /* if */
j++)
for (i = height / 2; i >= 1; i--)
{
for (j = 1; j < (i * 2); j++)
printf ("*");
printf ("\n");
} /* for */
}
return;
/* pattern */
44.
/* This program modifies Program 6-3 on page 218 to
Page 6.9
display the total as each number is entered.
Written by:
Date:
*/
#include <stdio.h>
int main (void)
{
/* Local Definitions */
int num;
int sum = 0;
/* Statements */
printf ("\n*** start of prb0644.c ***\n\n");
printf ("\nEnter your numbers: <EOF> to stop.\n");
while (scanf ("%d", &num) != EOF)
{
sum = sum + num;
printf ("Total: %8d\n", sum);
} /* while */
printf ("\n*** end of prb0644.c ***\n\n");
}
return 0;
/* main */
45.
/* This program reads integer data from standard input
and prints the minimum integer read, maximum integer
read, and the average of the list.
Written by:
Date:
*/
#include <stdio.h>
#include <limits.h>
int main (void)
{
/* Local Definitions */
int smallest = INT_MAX;
int largest = INT_MIN;
int num;
int sum
= 0;
int count
= 0;
/* Statements */
printf ("\n*** start of prb0645.c ***\n\n");
printf ("Please enter an integer: ");
while (scanf ("%d", &num) != EOF)
{
if (num < smallest)
smallest = num;
if (num > largest)
largest = num;
sum = sum + num;
count++;
printf ("\nNext integer or <EOF> to stop: ");
Page 6.10
} /* while */
printf ("\n\nThe minimum number is %d\n", smallest);
printf ("\nThe maximum number is %d\n", largest);
printf ("\nThe average
is %7.2f\n",
(float)sum / count);
}
printf ("\n***
return 0;
/* main */
end
of prb0645.c ***\n\n");
46.
/* Test function that reads only positive numbers.
Written by:
Date:
*/
#include <stdio.h>
int main (void)
{
/* Local Definitions */
int num;
/* Statements */
printf ("\n***
begin
of prb0646.c ***\n\n");
while ((num = getValid ()) != EOF)
printf ("Valid number: %8d\n", num);
}
printf ("\n***
return 0;
/* main */
end
of prb0646.c ***\n\n");
/* ===================== getValid =====================
Verify that the input is a positive, even number.
Pre
nothing
Post Reads and returns a valid number or EOF
*/
int getValid (void)
{
/* Local Definitions */
int num;
int valid = 0;
/* Statements */
do
{
printf ("\nEnter an integer or <EOF> to stop: ");
scanf ("%d", &num);
/*Validate input */
if (num == EOF)
valid = 1;
else if (num < 0)
printf ("\n\a Number must be positive.\n");
else if (num % 2)
printf ("\n\aNumber must be even.\n");
else
Page 6.11
}
valid = 1;
} while (!valid);
return num;
/* getValid */
47.
/* ================== allPositiveEOF ==================
This function checks that all inputs are positive.
Pre nothing
Post data read and returns average if all
inputs positive; otherwise, returns the
negative number entered.
*/
float allPositiveEOF (void)
{
/* Local Definitions */
int
allPositive = 1;
int
num;
int
sum
= 0;
int
count
= 0;
float retval;
/* Statements */
printf ("\nEnter a positive integer: ");
while (allPositive && (scanf("%d", &num) != EOF))
{
allPositive = num > 0;
if (allPositive)
{
sum += num;
count++;
printf ("\nNext number or <EOF> to stop: ");
} // if
} /* while */
if (allPositive)
{
if (count == 0)
retval = 0.0;
else
retval = (float)sum / count;
} /* if */
else
retval = (float)num;
}
return retval;
/* allPositiveEOF */
48.
/* ==================== sumEOF2 ====================
Read a series of numbers, terminated by EOF, and
return their sum.
Pre
nothing
Post reads data and returns the sum of inputs.
*/
int sumEOF2 (void)
{
/* Local Definitions */
int num;
Page 6.12
int sum = 0;
int done = 0;
/* Statements */
printf ("Enter a number (EOF to quit): ");
do
{
if (scanf ("%d", &num) == EOF)
done = 1;
else
{
sum += num;
printf ("Next number (EOF to quit):
} // else
} while (!done);
return sum;
} /* sumEOF2 */
");
49.
/* This function finds the smallest number entered
by initializing ‘smallest’ to the first number
read, then going into the loop.
Pre
nothing
Post returns the smallest integer
*/
int smallestEOF (void)
{
/* Local Definitions */
int num;
int smallest;
/* Statements */
printf ("\nEnter an integer: ");
scanf ("%d", &smallest);
printf ("\nEnter next integer or <EOF> to stop: ");
while (scanf ("%d", &num) != EOF)
{
if (num < smallest)
smallest = num;
printf ("\nNext integer or <EOF> to stop: ");
} /* while */
}
return smallest;
/* smallestEOF */
50.
/* This program approximates Euler's number e using a
loop that terminates when the difference between two
successive values differs by less than 0.0000001.
Written by:
Date:
*/
#include <stdio.h>
/* Prototype Declarations */
long factorial (long);
int main (void)
Page 6.13
{
/* Local Definitions */
int
n
= 0;
double old_e = 0.0;
double new_e = 1.0;
/* Statements */
printf ("\n*** start of prb0650.c ***\n\n");
while (new_e - old_e > .0000001)
{
old_e = new_e;
new_e += 1.0 / factorial (++n);
} /* while */
printf ("\n\nSuccessive 'e's are %10.8lf & %10.8lf",
old_e, new_e);
printf (" (n = %d and %d)\n", n - 1 , n);
printf ("\nThe difference is %10.8lf\n",
new_e - old_e);
}
printf ("\n***
return 0;
/* main */
end
of prb0650.c ***\n\n");
51.
/* This program is a test driver for a function that
approximates pi.
Written by:
Date:
*/
#include <stdio.h>
#include <math.h>
/* Prototype Declarations */
double figurePi (int);
int main (void)
{
/* Local Definitions */
double pi;
/* Statements */
printf ("\n*** start of prb0651.c ***\n\n");
pi = figurePi
printf ("\npi
pi = figurePi
printf ("\npi
}
printf ("\n***
return 0;
/* main */
(5);
using 5 terms is %10.8lf\n\n", pi);
(10);
using 10 terms is %10.8lf\n\n", pi);
end
of prb0651.c ***\n\n");
/* ================= figurePi ==================
This function returns an approximation for pi.
Pre
n is number of terms to use in calculation
Post returns approximation for pi
Page 6.14
*/
double figurePi (int n)
{
/* Local Definitions */
int
i;
double series;
double base;
double pi;
/* Statements */
for (series = 0, i = 1; i <= n; i++)
series += 1.0 / pow (i, 2.0);
base = 6 * series;
pi
= sqrt (base);
return pi;
} /* figurePi */
52.
/* This program reads an integer from the keyboard and
then calls a recursive function to print the digits
in reverse order.
Written by:
Date:
*/
#include <stdio.h>
/* Prototype Definitions */
void printReversed (int original);
int main (void)
{
/* Local Definitions */
int original;
/* Statements */
printf ("\n*** start of prb0652.c ***\n\n");
printf ("\nEnter the number: ");
scanf ("%d", &original);
printf ("\nThe original number was: %d", original);
printf ("\nThe reversed number is : ");
printReversed (original);
printf ("\n\n***
}
end
of prb0652.c ***\n\n");
return 0;
/* main */
/* ================= printReversed ==================
This function prints digits in an integer reversed.
Pre
num contains number to be reversed
Post prints number reversed
*/
void printReversed (int num)
{
/* Statements */
if (num != 0)
Page 6.15
{
}
printf ("%d", num % 10);
printReversed (num / 10);
} // if num != 0
return;
/* printReversed */
53.
/* This program prints out a fibonacci series.
Written by:
Date:
*/
#include <stdio.h>
/* Prototype Declarations */
void fib2 (int);
int main (void)
{
/* Local Definitions */
int seriesSize;
/* Statements */
printf ("\n*** start of prb0652.c ***\n\n");
printf ("Print a Fibonacci series.\n");
printf ("How many numbers do you want? ");
scanf ("%d", &seriesSize);
fib2 (seriesSize);
}
printf ("\n\n***
return 0;
/* main */
end
of prb0652.c ***\n\n");
/* ====================== fib2 ===================
This function prints a fibonacci series.
Pre nums is how many numbers to print
Post series printed
*/
void fib2 (int nums)
{
/* Local Definitions */
int i;
int looper;
int seriesSize;
int cnt
= 2;
long fib;
long fib1 = 0;
long fib2 = 1;
/* Statements */
if (nums < 2)
seriesSize = 2;
else
seriesSize = nums;
printf ("First %d Fibonacci numbers: \n",
Page 6.16
seriesSize);
printf ("%14d%14d", 0, 1);
}
for (i = 3 ; i <= seriesSize ;
{
fib
= fib1 + fib2;
fib1 = fib2;
fib2 = fib;
printf ("%14ld", fib);
if ( i % 5 == 0 )
printf ("\n");
} // for
printf ("\n");
return;
/* fib2 */
i++)
Page 6.17
Download