HW4 - C Functions
CS 241 (HD)
A. Function Specifications
-
Additional problems (PDF)func
spec --Function specifications. A function specification is a structured description of the behavior of a function, in terms of arguments, return values, etc. For example, themeanfunction defined bydouble mean(double num1, double num2) { return (num1 + num2)/2; }satisfies the following specification: This spec describes the functionmean-
2 arguments:
double(double-precision floating-point numbers -
Return: A
double, the average value of arg1 and arg2.
mean()unambiguously.-
The number of arguments for that function are indicated, and their types are identified.
-
The return value is described in terms of the argument(s).
-
The spec uses arg1 and arg2 to refer to the individual arguments for that function.
-
The type of the return value is also indicated.
mean(5, 8) --> 6.5For another example of a spec, consider the functionsum_input()defined byint sum_input() { int val; /* holds next integer value from standard input */ int sum = 0; /* sum of input integers so far */ while (scanf("%d", &val) != EOF) sum = sum + val; return sum; }This function has no arguments, but reads values from standard input. Here is a spec: Example call:sum_input- No arguments
- State change:
- Integer values are read from standard input, which is assumed to consist only of a series of integer values.
-
Return: A
long, the sum of those input integers.
sum_input()
If the standard input contains16 40 20 82
then the return value will be 158-
A state change is an effect of a function other than a return value. Input and output (as in
sum_input()) are examples of state changes.
Write a program
specs.cthatincludes the two function definitions
mean()andsum_input()(these should be entered
after#include <stdio.h>and beforemain());calls
mean() with arguments 5 and 8, and prints the return value along with a descriptive label (e.g.,The call mean(5, 8) returns 6.5
Prompts for the user to enter input integers, followed by CTRL/D; and
calls
sum_input()with standard input16 40 20 82
and prints the return value with a descriptive label (e.g.,The return value from calling sum_input() is 158
Note:
The function
sum_input()will read input integers until the end of standard input is detected.When running your program in the usual way at the command line, enter CTRL/D (hold down control key and press D) at the beginning of an input line in order to end standard input for your program
./specs./specs 16 40 20 82 CTRL/D
Alternatively, you can enter your input integers in a file
specs.in, and run your program using this command:./specs < specs.in
In this case, the end of that filespecs.inconstitutes the end of standard input for that program./specs.
To submit your work on this problem, enter these commands:
git add specs.c git add specs.in # only if you used a file specs.in as described above git commit -m "hw4: problem 1" git pull origin master git push origin master
-
2 arguments:
-
Write out your function definition and
main()on a piece of paper as correctly as possible -- so correct, if possible, that someone else who didn't know C could type it in exactly and produce a correct C computation. If you are uncertain about anything as you write, look it up. -
Then, enter your program, compile, and verify that it runs correctly
Finally submit your program to stogit using the commit message
"hw4: problem n"
with the appropriate problem numbern.-
areaCircle --[HC] Area of a circle. In Mathematics, the area of a circle with radius r is
.
Use this formula to implement the following C function.
Example call:area- One argument: A non-negative float value, representing the radius of a circle
- Return: A non-negative float value, the area of a circle of radius arg1
area(3) --> 28.274333882308138
Hint:-
You can define a value for π in C as follows:
const double pi = 3.1415926;
Here, the typeconst doublemeans that the variablepiholds adoublevalue and may not be assigned a new value.
-
moInterest --[HC] Monthly interest. In Economics, the amount of monthly interest owed on a loan depends on the principal (or amount of money still owed on the loan) and the interest rate. The interest rate r is often expressed as a percentage per year. Thus, the formula for a monthly interest charge on a loan with current principal P is
Use this formula to implement the following C function.
Example:moInterest- 2 arguments: Non-negative float numbers, a principal amount (of money) and an interest rate (expressed in percent per year)
- Return: A non-negative float number, the amount of one month's interest on a loan with current principal arg1 at interest rate arg2. .
moInterest(10000, 6) --> 50.0
For the problems below:
Write C function definitions for each of the following specs, together with
main() programs that test that function.
For each of these problems marked [HC],



