Assignment #4 - Functions
Due: Mon, June 26
Objective
This assignment will consist of writing several different small programs
that involve practice writing and calling functions.
Task
Write the following functions, along with test programs, each in
a separate file. Filenames should be:
- temp.c
- seconds.c
- primes.c
(Note that the filenames are all lowercase)
Also note that each exericse involves writing a function that
performs some task, along with a main program that will help test
the function. In each exercise, the function is described in part 1, and
it must do exactly what is specified. The function's job and
main()'s job are different things, and they must not do each
other's jobs. (The "main" program will be calling upon the
function to help in the process, in each exercise).
Exercise 1
Filename: temp.c
- Write a function called CelsToFahr that takes in as a
parameter a Celsius temperature (type double) and returns (as the
return value) the corresponding Fahrenheit temperature (also type double).
Recall that the mathematical formula for converting temperatures between
these scales is:
F = 9 / 5 * C + 32
- To test this function, write a main() routine that asks the
user for a celsius temperature, and then prints out a chart that
illustrates the celsius to fahrenheit conversions for 10 celsius
temperatures starting at the entered value and incrementing the celsius
temperature by 1 each time. Print all numerical
outputs to 3 decimal places. See the sample output for
clarification.
Sample run 1: (user input underlined)
Enter a celsius temperature: 40
Cels. Fahr.
------------------------------------------------------
40.000 104.000
41.000 105.800
42.000 107.600
43.000 109.400
44.000 111.200
45.000 113.000
46.000 114.800
47.000 116.600
48.000 118.400
49.000 120.200
Sample run 2: (user input underlined)
Enter a celsius temperature: 12.54
Cels. Fahr.
------------------------------------------------------
12.540 54.572
13.540 56.372
14.540 58.172
15.540 59.972
16.540 61.772
17.540 63.572
18.540 65.372
19.540 67.172
20.540 68.972
21.540 70.772
Exercise 2
Filename: seconds.c
- Write a function called Seconds that takes in three
integer parameters (representing hours, minutes, and seconds) and returns
the number of seconds since the last time the clock "struck 12" (i.e. was
at 12:00:00 -- AM or PM doesn't matter since you're not tracking
this).
- To test this function, write a main() routine (in the
same file) that:
- prompts the user to enter hours, minutes, and seconds for
two different clock times
- uses the Seconds function to calculate
the shortest amount of time in seconds between the two clock times
(both of which are within one 12-hour cycle of the clock)
- prints out the number of seconds since "striking 12" for both
clocks, as well as the difference in seconds between the two clock
times
Sample run 1: (user input is underlined)
Input first clock time...
Hours: 6
Minutes: 45
Seconds: 30
Input second clock time...
Hours: 4
Minutes: 50
Seconds: 12
It's been 24330 seconds since the first clock struck 12:00
It's been 17412 seconds since the second clock struck 12:00
The two times are 6918 seconds apart.
Sample run 2: (user input is underlined)
Input first clock time...
Hours: 12
Minutes: 43
Seconds: 16
Input second clock time...
Hours: 7
Minutes: 11
Seconds: 59
It's been 2596 seconds since the first clock struck 12:00
It's been 25919 seconds since the second clock struck 12:00
The two times are 23323 seconds apart.
Exercise 3
Filename: primes.c
- Recall that an integer is a prime number if it is divisible only
by 1 and itself. Write a function called IsPrime that
takes in one non-negative integer parameter X and determines whether or
not X is prime. The function should return an int value:
- should return 1 (for true) if X is a prime number,
- should return 0 (for false) if X is not a prime number.
(Hint: The % operator is good for checking for divisibility of
one integer by another, and the only possible factors of a positive value
X are in the range 1 through X.)
- To test this function, write a main() routine that asks the
user to input a positive integer N. Using your IsPrime()
function to test each value, find and print all the prime numbers less
than or equal to N, where the output is formatted to 8 numbers per line
(you can use tab characters to separate numbers on a line). You may
assume that the value input by the user is a positive integer.
Sample run: (user input underlined)
Please input a positive number: 500
The prime numbers less than or equal to 500 are:
2 3 5 7 11 13 17 19
23 29 31 37 41 43 47 53
59 61 67 71 73 79 83 89
97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263
269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457
461 463 467 479 487 491 499
Requirements for all programs
- No global variables, other than constants
- The required tasks must be performed with the functions
specified (not just with a single main() routine)
- Note that each exercise requires the writing of a function, and a
main routine to test that function.
- Each function should do exactly the task specified in item 1
of each exercise
- Item 2 of each exercise specifies what to do in main() --
this will always involve CALLING the function, sometimes more
than once
- Note that there is NO keyboard-input/screen-output specified in the
functions themselves (i.e. "return" does not mean "print")
- All input and output must be done with printf and scanf, using the
library stdio.h
- As always, your source code must be readable and
appropriately documented
Submitting:
Program submissions should be done through the submission web page, linked
from the main course web site. Submit the files:
temp.c
seconds.c
primes.c