In this lab you will create three small programs using what you've learned about:
if
,
else
,
and switch
statements
&&
,
||
,
!
)
>
,
<
,
<=
,
>=
,
==
,
!=
)
You will also learn how to convert strings to floating-point and integer values, and how to manipulate random numbers using functions in the <stdlib.h>
library.
Write a program that accepts a temperature reading as input, and outputs that temperature converted to the Kelvin, Celsius, and Fahrenheit scales.
Your program should take two command line arguments: a number for the temperature reading, and a letter denoting the scale used (eg. C, K, or F). If the user does not provide both parameters, or the provided values are invalid, your program should print an error message and a short description of the expected usage.
Command line arguments always come into your program as string values. In order to use these as numbers that you can do math with, you will first need to parse the string using an appropriate function from the <stdlib.h>
library.
In this case, you'll want to use the strtof()
function. Here's a simple example of how to use it:
The first parameter is the string that you want to convert into a number. The second parameter, if not NULL
, will be updated to point just after the last parsed character from the input string. This is useful for checking whether or not the value was successfully parsed: if the result is the same as the input string pointer, the value was not recognized.
There is a similar function in <stdlib.h>
for parsing strings as integers: strtol()
. This one takes a third parameter that specifies the numeric base, which you'll normally want to set to 10.
If you don't need to do any error-checking, you can simplify things considerably by using the atof()
and atoi()
functions instead.
The Fahrenheit temperature scale and the Celsius temperature scale are related by the equation: F = C * 1.8 + 32
Celsius and Kelvin are related by the equation: C = K - 273.15
Write a program that takes a year number and a month number as input from the command line and prints the number of days in the given month. Use a switch statement with a nested if/else structure to determine the correct number of days.
Do not use any of the functions from the standard <time.h>
library in your solution.
Note: February has a different number of days depending on the year; 28 days in common years, and 29 in leap years. Leap years are divisible by 4, but not by 100 unless also divisible by 400.
Write a program to run a simple lottery. The program should randomly generate a lottery number between 0 and 99, accept a users's guess from the command line, and determine the user's winnings according to the following rules:
Because computers are deterministic, producing actual random data values is quite tricky. The usual approach is to take some external data value that is unpredictable (such as the current time) and use it as the "seed" to a bunch of math that produces a sequence of psuedo-random values. The values you get are actually still deterministic, but as long as the seed isn't known and the math is well-designed they will appear to be random.
C99 includes srand()
and rand()
functions in the <stdlib.h>
library that set a seed and produce psuedo-random values from it respectively. Unfortunately, most implementations of these functions are based on very simplistic math and do a really poor job of appearing random. For almost every application, you shouldn't use them.
POSIX provides two very similar functions, srandom()
and random()
(also defined in <stdlib.h>
, that are used in the same way and perform a little better. We'll use them for this assignment. Here's an example:
For real applications where randomness is actually important, even the POSIX standard library functions shouldn't be used. Your code in these cases will need to rely on external libraries that provide better solutions.
Completing all tasks in this lab should result in 3 separate C files within a single folder named like LastnameFirstname_lab2. Compress the folder into a ZIP archive and upload it to the D2L assignment.
The marks for this lab are heavily weighted towards good coding practice and style. Pay attention to your code formatting, use comments to clarify your code, and make sure you are using defines, variables, and array notation effectively.
NOTE: This assignment is to be done individually. You can help one another with problems and questions, but in the end everyone must write and submit their own code.
Criteria | Marks |
---|---|
Programs compile and run without error | 3 |
Good coding style | 3 |
Task requirements met or exceeded | 3 |
Learning Outcomes achieved | 3 |
Total | 12 |