Homework 16: Writing and using functions
Name: _____________________________________________ Alpha: ___________________
Describe help received: ________________________________________________________
- Due before class on Friday, February 24
- This homework contains code to be submitted electronically.
Put your code in a folder called
hw16
and submit using the204sub
command. - This is a written homework - be sure to turn in a hard-copy
of your completed assignment before the deadline. Use the
codeprint
command to print out your code and turn that in as well.
Assignment
-
Circle one to indicate how you did for the reading assignment from
Homework 15 before class on Wednesday:
How carefully did you complete the reading? (Circle one)
Not at allSkimmed itRead someRead all - Given the following prototypes:
and the following variable definitions:int abs(int j); // returns the absolute value of j int round(double x); // rounds to the nearest integer double str2num(cstring str); // converts a string to a number
Assuming the functions actually work the way they say they do, fill in the following table with the type and value of each expression. Write ERROR for both if the expression would be a compiler error.int a = 10; double b = 3.3; cstring c = "12.8";
expression type value c
str2num(c)
a + abs(-2)
b + abs(-2)
round(b) + 2.4
round(b + 2.4)
round(round(b) + 2.4)
round(c) + 2.4
str2num(abs("-4"))
round(str2num("1.2"))
- In the program below, circle and identify all function prototypes, function definitions,
function calls, function parameters, and function arguments.
#include <stdio.h> int readRow(int len); int main() { int rows; int cols; scanf(" %i , %i", &rows, &cols); int count = 0; for(int r = 0; r < rows; r++) { if (readRow(cols)) { count++; } } printf("%i\n", count); return 0; } int readRow(int len) { int n = 0; for(int i = 0; i < len; i++) { char c; scanf(" %c", &c); if (c == 'Y') { n++; } } return (n > 0); }
-
Download the partially-completed program conv.c
and complete it by writing the
main()
function. You should make use of the functions I've defined for you! When you're finished, the program should work like this:roche@ubuntu$
./conv
convert 45 miles to kilometers
72.4205 kilometers
roche@ubuntu$
./conv
convert 3.3 inches to light-years
8.85997e-18 light-years
roche@ubuntu$
./conv
convert 378.1 nautical-miles to millimeters
7.00242e+08 millimeters
-
Download the partially-completed program harm.c
and complete it by writing a prototype and definition for a function
harm(n)
which computes the sum of the first \(n\) terms in the harmonic series: \[\frac{1}{1} + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \cdots + \frac{1}{n}\] When you're finished, the program should work like this:roche@ubuntu$
./harm
Enter target:
2.5
The 7th harmonic number is 2.59286, which is the first greater than 2.5.
roche@ubuntu$
./harm
Enter target:
3.25
The 14th harmonic number is 3.25156, which is the first greater than 3.25.
roche@ubuntu$
./harm
Enter target:
3.5
The 19th harmonic number is 3.54774, which is the first greater than 3.5.
roche@ubuntu$
./harm
Enter target:
9.75
The 9631th harmonic number is 9.75001, which is the first greater than 9.75.