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: (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
  1. 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
    
  2. 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
  1. 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).
     
  2. To test this function, write a main() routine (in the same file) that:

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
  1. 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: (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.)
     
  2. 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


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