UMBC CS 201, Fall 08
UMBC CMSC 201
Fall '08

UMBC | CSEE | 201 | 201 F'08 | lectures | projects | help

Practice Problems

Here are a tracing problem and some random number problems to give you some practice.
The comments have purposely been omitted from the trace code.
After performing the problems click the problem or problem name to see the answer.
Explanations are also given for each of the problems.

  1. Tracing Problem

    #include <stdio.h> int F (int a); int G (int b); int main() { int a, b, c; a = b = c = 0; a = F(b); c = G(a); printf("main: a = %d, b = %d, c= %d\n", a, b , c); return 0; } int F (int a) { int b, c; b = 2 * a + 5; c = G(b); printf("F: a = %d, b = %d, c = %d\n", a, b, c); return c; } int G (int b) { int a, c; c = b + 7; a = c - 2; printf("G: a = %d, b = %d, c = %d\n", a, b, c); return a; }

    Random Number Problems

  2. What header file do you have to #include to use the functions srand() and rand() ?


  3. Seed the random number generator with the value 10.


  4. Give a single statement that makes a call to rand() and produces a number in the range of 0 to 4 making use of the % operator.


  5. Give a single statement that makes a call to rand() and produces a number in the range of 7 to 11 making use of the % operator.



  1. Output from the Trace

    G: a = 10, b = 5, c = 12 F: a = 0, b = 5, c = 10 G: a = 15, b = 10, c = 17 main: a = 10, b = 0, c= 15
    Explain


  2. #include <stdlib.h>

    Explain


  3. srand(10);

    Explain


  4. rand() % 5;

    Explain


  5. rand() % 5 + 7;

    Explain



  1. Explanation 1

    For this explanation the color blue will be associated with main(), red with the function F(),
    and green with the function G().

    The line: int a, b, c;
    in main() declares three integer variables known only to main() called a, b, and c.

    On the next line a = b = c = 0;
    all of those variables are assigned the value 0.
    Since the associativity of the assignment operator is from right to left,
    first c gets assigned the value 0,
    then b gets assigned the value of c, which is 0.
    Finally a gets assigned the value of b, which is 0.

    Which yields:

    The next line a = F(b);
    is a function call to the function F()
    The value of the variable b gets passed to the function.
    So the value 0 gets passed to F()

    Which yields:

    Execution of the program now leaves main() and enters the function F().

    When we examine the function header for the function F() , we see int F (int a).
    We find that the value passed into the function will be stored in F()'s only parameter,
    an integer variable called a.

    Within the function F() , there are two other local variables declared, int b, c;
    So F() actually has three local variables, a , b , and c , known only to this function, F() .
    The memory locations for these variables are different from the locations of main()'s a, b, and c.
    After the function F()'s declarations, we have:

    where the variables b and c have been declared, but still contain garbage, indicated by ~ .

    The next line executed within the function F() is b = 2 * a + 5;.
    Substituting 0, the value of a into the expression, we get 2 * 0 + 5;
    multiplying first, we get 0 + 5 = 5.
    So the value 5 gets assigned into b yielding :

    The next line of code, c = G(b); is a call to the function G() passing the value of b , which is 5.

    Which yields:

    Execution of the program now leaves F() and enters the function G()

    When we examine the function header for the function G(), we see int G (int b) .
    We find that the value passed into the function will be stored in G()'s only parameter,
    an integer variable called b.

    Within the function G() , there are two other local variables declared, int a, c;
    So G() actually has three local variables, a , b , and c , known only to this function, G() .
    The memory locations for these variables are different from the locations of F()'s a, b, and c
    and main()'s a, b, and c.
    After the function G()'s declarations, we have:

    where the variables a and c have been declared, but still contain garbage, indicated by ~ .

    The next statement c = b + 7; is executed.
    Substituting the value of b , 5, into the expression, we get c = 5 + 7;
    So c gets assigned the value 12. Yielding:

    The next statement a = c - 2;is executed.
    Substituting the value of c , 12, into the expression, we get a = 12 - 2;
    So a gets assigned the value 10. Yielding:

    The next statement, printf("G: a = %d, b = %d, c = %d\n", a, b, c); is executed.
    This is the first statement of the program that produces output. The value of G()'s variable a gets printed in the position of the first %d in the string of the printf statement, since it is in the first position in the list of variables to be printed. Similarly, the value of G()'s variable b gets printed in the position of the second %d and c gets printed in the position of the last %d.
    So the letter G: and the values of G()'s variables a, b, and c get printed to the screen as shown in the output box below.

    When the next statement, return a; gets executed, the value of G()'s a, 10, gets returned back to the calling function, F().

    The program execution now leaves the function G() and returns to the calling function, F().
    All of G()'s variables go out of scope, yielding:

    Where does the execution pick up in F() and what happens to the value being returned, 10 ?
    The execution begins on the same line that called G() , c = G(b); and the value being returned, 10, replaces the call (the part of the statement on the right-hand side of the assignment operator). Basically, you can think of the statement as looking like this: c = 10; So the value 10 gets assigned into F()'s c yielding:

    The next statement, printf("F: a = %d, b = %d, c = %d\n", a, b, c); is executed.
    This is the second statement of the program to produce output.
    So the letter F: and the values of F()'s variables a, b, and c get printed to the screen as shown in the output box below.

    When the next statement, return c; gets executed, the value of F()'s c, 10, gets returned back to the calling function, main().

    The program execution now leaves the function F() and returns to the calling function, main().
    All of F()'s variables go out of scope, yielding:

    The execution begins on the same line that called F() , a = F(b); and the value being returned, 10, replaces the call (the part of the statement on the right-hand side of the assignment operator). Basically, you can think of the statement as looking like this: a = 10; So the value 10 gets assigned into main()'s a yielding:

    The next line c = G(a);
    is a function call to the function G()
    The value of the variable a gets passed to the function.
    So the value 10 gets passed to G()

    Which yields:

    Execution of the program now leaves main() and enters the function G().

    When we examine the function header for the function G(), we see int G (int b).
    We find that the value passed into the function will be stored in G()'s only parameter,
    an integer variable called b.

    Within the function G() , there are two other local variables declared, int a, c;
    So G() actually has three local variables, a , b , and c , known only to this function, G() .
    The memory locations for these variables are different from the locations of main()'s a, b, and c.
    After the function G()'s declarations, we have:

    where the variables a and c have been declared, but still contain garbage, indicated by ~ .

    The next statement c = b + 7; is executed.
    Substituting the value of b , 10, into the expression, we get c = 10 + 7;
    So c gets assigned the value 17. Yielding:

    The next statement a = c - 2; is executed.
    Substituting the value of c , 17, into the expression, we get a = 17 - 2;
    So a gets assigned the value 15. Yielding:

    The next statement, printf("G: a = %d, b = %d, c = %d\n", a, b, c); is executed, yielding :

    When the next statement, return a; gets executed, the value of G()'s a, 15, gets returned back to the calling function, main().

    The program execution now leaves the function G() and returns to the calling function, main().
    All of G()'s variables go out of scope, yielding:

    The execution begins on the same line that called G() , c = G(a); and the value being returned, 15, replaces the call (the part of the statement on the right-hand side of the assignment operator). Basically, you can think of the statement as looking like this: c = 15; So the value 15 gets assigned into main()'s c yielding:

    The next statement, printf("main: a = %d, b = %d, c = %d\n", a, b, c); is executed.

    Finally, the return 0; statement in main() is reached.
    The value 0 is returned back to the operating system, indicating to the operating system that the program ran normally to completion.


  2. Explanation 2

    The function definitions for the functions srand() and rand() are both found in the stdlib library
    and their function prototypes are found in the associated header file called stdlib.h.
    So in order to be able to call the functions from your program without warnings and possible errors,
    you must #include <stdlib.h> so the prototypes can be seen before the calls to those functions.

  3. Explanation 3

    The function srand() seeds the random number generator with the value passed in. Since the call in this case is srand(10);, the value 10 is used as the seed. That means that the calculations that will produce the first pseudo-random number will use a beginning value of 10.
    You can think of a call to srand() as turning on the random number generator. No random numbers are produced until there is a call to rand().

  4. Explanation 4

    The function rand() is called first and an integer is returned. This is typically a large integer and not at all within a range of values that we will be able to use in our programs. If we want to use values between 0 and 4, we can now modify the integer returned by moding it by 5.
    Why 5?
    If you divide any integer by 5, it will either be evenly divisible by 5, producing a remainder of 0
    or it could have a remainder of 1, or 2, or 3, or 4.
    So rand() % 5; will produce numbers in the range of 0 to 4.

  5. Explanation 5

    Since rand() % 5; produces numbers in the range of 0 to 4,
    then to get values in the range of 7 to 11, you just need to add 7 to the result.
    0 + 7 = 7
    1 + 7 = 8
    2 + 7 = 9
    3 + 7 = 10
    4 + 7 = 11
    Hence: rand() % 5 + 7;

Last Modified - Thursday, 11-Sep-2008 16:57:27 EDT


UMBC | CSEE | 201 | 201 F'08 | lectures | projects | help