![]() |
School of Engineering and
Applied
Science Department of Computer Science CSci 49 -- Introduction to C Computing http://www.seas.gwu.edu/~csci49/spring06 Prof. Michael B. Feldman mfeldman@gwu.edu |
Copy, compile, and run this program. What is the output?
| |
In line 14, change <
to <=. Compile and
run.
What is the output?
| |
Now add a semicolon after the ) of
line
14. Compile; describe the result.
| |
Remove the spurious semicolon. Now modify the program so it counts
backwards
from 10 to 1. Write down the modified lines:
|
|
Now modify the program so it displays only the odd numbers.
|
|
Now add statements so that the user is prompted for the starting and
ending values. You'll need variables for these -- call them start
and end. Run the program several times; experiment with
different
starting and ending values. Try at least one pair such that the
starting
value is greater than the ending value.
Now let's look at another example, programs49/NestedWhile.c.
1.
/*------------------------------------------------------------
2. | NestedWhile.c
3. | Very simple example illustrating nested while-loop
construct
4. | Author: M. B. Feldman, The George Washington
University
5. | Last Modified: February 2006
6.
------------------------------------------------------------*/
7. #include <stdio.h>
8. int main()
9. {
10. int
Outer;
/* counter for outer loop */
11. int
Inner;
/* counter for inner loop */
12.
13. Outer =
1;
/* INITIALIZATION */
14. while (Outer <
5) /* TERMINATION COND. */
15. {
16. printf("%3d |", Outer);
17.
18. Inner =
1;
/* inner loop initialization */
19. while (Inner <=
10) /* inner loop termination cond. */
20. {
21. printf("%3d ",
Inner);
22. Inner = Inner +
1; /* inner loop modification */
23. }
24.
25. printf("\n");
26. Outer = Outer +
1; /* MODIFICATION */
27. }
28. printf("\n");
29. return 0;
30. }
Copy, compile, and run it. What is the output?
|
|
In line 14, change 5 to 7, and in line 18, change 1 to 6. Now what
is
the output?
|
|
Now in line 18, change 6 to Outer. Now what is the output?
|
|
Modify the program so it displays a triangular shape with the point
at the top, like
1 | 1
2 | 1 2
3 | 1 2 3
4 | 1 2 3 4
etc. Write down the modified lines here.
|
|
(end of lab)