1 Code tracing

1.0.1 1)

After running this code, what is the value of a and b?

f <- function(x) {
    x = x + 1
    if ((x %% 2) == 0) {
        x = x - 1
    }
    x = 2*x
}

a <- f(7)
b <- f(12)

1.0.2 2)

Write the output of this code by hand:

f <- function(x) {
    if ((x %% 3) == 0) {
        cat('woo!')
        return(x %/% 3)
    }
    return(x %% 2)
}

cat(f(9))
cat(f(11))

1.0.3 3)

Write the output of this code by hand:

f <- function(x) {
    if (x > 0) {
        cat('abc')
        x = 2*x
    } else if (x <= 0) {
        x = abs(x)
        cat('cba')
    }
    cat(x)
}

cat(f(-9))
cat(f(15))

2 Write functions

Here are some functions you should be able to write. Any of these may appear (directly or modified) on a quiz or exam! Your function should be able to pass the test functions provided.

2.1 isPositiveMultipleOf4Or7(n)

2.1.1 a)

Write the function isPositiveMultipleOf4Or7(n) that returns TRUE if n is a positive multiple of 4 or 7 and FALSE otherwise. Note than n could be any data type.

2.1.2 b)

Write the test function testIsPositiveMultipleOf4Or7() that tests the function isPositiveMultipleOf4Or7(n) for a variety of values of n. Consider cases that you might not expect, such as cases where n is not a number.


EMSE 6574, Sec. 11: Programming for Analytics (Fall 2019)
George Washington University | School of Engineering & Applied Science
Dr. John Paul Helveston | jph@gwu.edu | Mondays | 6:10–8:40 PM | Phillips Hall 108 | |
This work is licensed under a Creative Commons Attribution 4.0 International License.
See the licensing page for more details about copyright information.
Content 2019 John Paul Helveston