Review C: C for Python programmers: Questions

C1.1.

What distinguishes a compiler from an interpreter?

C1.2.

C is called a procedural language. Why is procedural an apt adjective? (Saying basically that C is not object-oriented is not an adequate answer.)

C2.1.

Complete the below C function so that it returns the nth harmonic number for a parameter n. The nth harmonic number is the sum

i = 1n (1 / i) = 1/1 + 1/2 + 1/3 + … + 1/(n − 1) + 1/n
double harmonic(int n) {
C2.2.

The following series computes the value of π.

π = ∑n = 0 8 / [(4 n + 1) (4 n + 3)] = 8 / (1 ⋅ 3) + 8 / (5 ⋅ 7) + 8 / (9 ⋅ 11) + 8 / (13 ⋅ 15) + ….

Complete the C program below to display the result of adding as many terms of the series as requested by the user. For example, given the input 1, the program should display 2.6667 ≈ 8/3; given 2, it should display 2.8952 ≈ 8/3 + 8/35; given 3, it should display 2.9760 ≈ 8/3 + 8/35 + 8/99.

int main() {
    int terms;
    double pi;


    printf("Number of terms: ");
    scanf("%d", &terms);




    printf("%6.1f"pi);
    return 0;
}
C2.3.

Write a C function count_common_factors that given two integer parameters m and n returns the number of integers that are factors both of m and n. For example, count_common_factors(7028) should return 4 since there are four distinct integers dividing into both (1, 2, 7, 14). The below code illustrates how this might be done using Python.

def count_common_factors(mn):
    count = 0
    for i in range(1m + 1):
        if m % i == 0 and n % i == 0:
            count += 1
    return count
C2.4.

The hailstone sequence is defined by the following loop.

while n ≠ 1:
    if n is odd:
        n ← 3 n + 1
    else:
        nn / 2

Starting with 7, this loop iterates n through a sequence of 17 numbers:

7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.

If we started with 5, the sequence would contain just six numbers — 5, 16, 8, 4, 2, 1.

Complete the following function so that it returns how many numbers are in the hailstone sequence starting from its parameter; thus, hailstone(7) should return 17 while hailstone(5) returns 6.

int hailstone(int start) {
C2.5.

The following program has already been written to populate its data array with 100 values entered by the user. Complete the program so that it displays all positive numbers in the array in reverse order (skipping over all non-positive numbers), each on its own line. For example, if the array was length 6 rather than 100, and the user entered <4, 3, −1, −2, −6, 5>, your code would display 5, then 3, then 4.

int main() {
    int data[100];
    int i;

    for (i = 0i < 100i++) {
        printf("Value %d: "i + 1);
        scanf("%d", &(data[i]))
    }
C2.6.

The following program has already been written to populate its data array with 10 values entered by the user. Using a loop, complete the program so that it displays “palindrome” if the array is the same read backwards as forwards (as in <9, 13, 15, 15, 13, 9>, which reversed is the same) and “no” otherwise.

int main() {
    int data[10];
    int i;

    for (i = 0i < 10i++) {
        printf("Value %d: "i + 1);
        scanf("%d", &(data[i]))
    }
C3.1.

Explain the motivation behind using a header file for a C program.

Review C: C for Python programmers: Solutions

C1.1.

A compiler translates code written in a programming language into a more basic encoding that a computer can execute efficiently (most often the machine code that the computer executes natively). An interpreter reads and executes code written in a programming language, without explicit translation into a basic encoding of instructions.

C1.2.

In designing a large C program, we must take the overall program and divide it into smaller and smaller procedures (functions in C-speak). (This is in contrast to object-oriented languages, where we design a large program by identifying the different objects that the program will manipulate.)

C2.1.
double harmonic(int n) {
    double total = 0.0;
    int i;
    for (i = 1i <= ni++) {
        total += 1.0 / i;
    }
    return total;
}
C2.2.
int main() {
    int terms;
    double pi;
    int i;

    printf("Number of terms: ");
    scanf("%d", &terms);

    pi = 0.0;
    for (i = 0i < termsi++) {
        pi += 8.0 / (4 * i + 1) / (4 * i + 3);
    }

    printf("%6.1f"pi);
    return 0;
}
C2.3.
int count_common_factors(int mint n) {
    int count;
    int i;

    count = 0;
    for (i = 1i <= mi++) {
        if (m % i == 0 && n % i == 0) {
            count++;
        }
    }
    return count;
}
C2.4.
int hailstone(int start) {
    int n;
    int count;

    n = start;
    count = 1;
    while (n != 1) {
        if (n % 2 == 1) {
            n = 3 * n + 1;
        } else {
            n = n / 2;
        }
        count++;
    }
    return count;
}
— OR — int hailstone(int start) {
    if (start == 1) {
        return 1;
    } else if (start % 2 == 1) {
        return 1 + hailstone(3 * start + 1);
    } else {
        return 1 + hailstone(start / 2);
    }
}
C2.5.
    for (i = 99i >= 0i--) {
        if (data[i] > 0) {
            printf("%d\n"data[i]);
        }
    }

    return 0;
}
C2.6.
int main() {
    int data[10];
    int i;

    for (i = 0i < 10i++) {
        printf("Value %d: "i + 1);
        scanf("%d", &(data[i]))
    }

    i = 0;
    while (i < 5 && arr[i] == arr[9 - i]) {
        i++;
    }
    if (i >= 5) {
        printf("palindrome\n");
    } else {
        printf("no\n");
    }
}
— OR — int main() {
    int data[10];
    int i;
    int all_match;

    for (i = 0i < 10i++) {
        printf("Value %d: "i + 1);
        scanf("%d", &(data[i]))
    }

    all_match = 1;
    for (i = 0i < 5i++) {
        if (arr[i] != arr[9 - i]) {
            all_match = 0;
        }
    }
    if (all_match) {
        printf("palindrome\n");
    } else {
        printf("no\n");
    }
}
C3.1.

When you have a file defining a library of several functions, any other file using the library needs to include prototypes for each of the functions. Rather than require the other files to explicitly list the prototypes, we write the prototypes into a header file, and each file using the library can simply reference this header file using a single #include directive to pull in the prototypes from the header file.