Review C: C for Python programmers: Questions
What distinguishes a compiler from an interpreter?
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.)
Complete the below C function so that it returns the n
th harmonic
number for a parameter n
. The nth harmonic number is
the sum
double harmonic(int n) {
The following series computes the value of π.
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;
}
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(70, 28)
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(m, n):
count = 0
for i in range(1, m + 1):
if m % i == 0 and n % i == 0:
count += 1
return count
The hailstone sequence is defined by the following loop.
while n ≠ 1:
if n is odd:
n ← 3 n + 1
else:
n ← n / 2
Starting with 7, this loop iterates n through a sequence of 17 numbers:
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) {
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 = 0; i < 100; i++) {
printf("Value %d: ", i + 1);
scanf("%d", &(data[i]))
}
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 = 0; i < 10; i++) {
printf("Value %d: ", i + 1);
scanf("%d", &(data[i]))
}
Explain the motivation behind using a header file for a C program.
Review C: C for Python programmers: Solutions
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.
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.)
double harmonic(int n) {
double total = 0.0;
int i;
for (i = 1; i <= n; i++) {
total += 1.0 / i;
}
return total;
}
int main() {
int terms;
double pi;
int i;
printf("Number of terms: ");
scanf("%d", &terms);
pi = 0.0;
for (i = 0; i < terms; i++) {
pi += 8.0 / (4 * i + 1) / (4 * i + 3);
}
printf("%6.1f", pi);
return 0;
}
int count_common_factors(int m, int n) {
int count;
int i;
count = 0;
for (i = 1; i <= m; i++) {
if (m % i == 0 && n % i == 0) {
count++;
}
}
return count;
}
int hailstone(int start) { | — OR — | int hailstone(int start) { |
for (i = 99; i >= 0; i--) {
if (data[i] > 0) {
printf("%d\n", data[i]);
}
}
return 0;
}
int main() { | — OR — | int main() { |
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.