While at first glance this labsheet may seem rather long, the code for
each of the basic functions below is relatively short and will develop
with successive questions.
Each question attempts to address an important concept that you will
need to understand.
Note that the concepts in this labsheet can be difficult for beginner
programmers to grasp.
It is therefore important beginners attempt and complete (seeking help
where required) all beginner questions in this labsheet.
Refer to Lectures 8 and 9 (and to your reference materials) for details
on how to complete these tasks.
| 1.
|
Write a function called money that accepts a single
integer parameter that represents a number of cents, and determines the
"normalised" dollar/cent equivalent of this value.
Your function should return two integer values: the number of dollars,
and the number of "left-over" cents.
For example, a value of 524 should be returned as
5 dollars and 24 cents. Your function should
have the prototype:
void money(int total, int *dollars, int *cents);
and be called from a function like:
money(524, &dollars, ¢s);
|
| 2.
|
Using the structure:
typedef struct
{
int dollars;
int cents;
} MONEY;
write a new version of your money function from
Question 1 that accepts a pointer to a MONEY
structure and sets the dollars and cents
fields appropriately.
Your function should have the prototype:
void money(int total, MONEY *money);
|
| 3.
|
Write another version of your money function
from Question 1 that returns a pointer to a MONEY
structure. Your function should have the prototype:
MONEY *money(int total);
Remember, as you cannot return the address of a local variable,
you will need to use the malloc function to
"allocate" (heap) memory for the returned structure. Remember to
"de-allocate" the memory (with the free function)
when you are done with it.
|
| 4.
|
Write a function called decomposeDate that
accepts an integer parameter of the form yyyymmdd
(e.g. 20081006) and determines the corresponding
month, day, and year of the parameter, returning the three values
to the calling function.
For example, if your function is called using the statement:
decomposeDate(20081006, &year, &month, &day)
year should be set to
2008, month should be set to
10, and day
should be set to 06.
|
| 5.
|
Recast your decomposeDate function from Question
4 to return a pointer to a DATE structure defined
as follows:
typedef struct
{
int day;
int month;
int year;
} DATE;
Your function should have the prototype:
DATE *decomposeDate(int date);
|
| 6.
|
Using the DATE structure from Question 5,
write a function called dates that accepts an
integer array of dates to "decompose" and returns an array of
DATEs, one for each integer date in the array.
Your function will also need to be supplied the length of the
integer array and hence should have the prototype:
DATE *dates(int *intDates, int nDates);
|
| 7.
|
Extend your dates function from Question 6 to return
the DATEs in chronological order. To do this:
| i. |
Write a function called dateCompare that will compare two DATEs.
As we will shortly use this with the qsort function, your dateCompare function should have the prototype:
int dateCompare(const void *p1, const void *p2);
Your function should return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second argument.
Remember to cast the argument void pointers to DATE pointers before doing the comparisons.
Your function should start:
int dateCompare(const void *p1, const void *p2)
{
DATE *d1 = (DATE *)p1;
DATE *d2 = (DATE *)p2;
...
}
|
| ii. |
Use your dateCompare function with the qsort function to perform the actual sorting.
|
|
| 8.
|
Write a function called stats that accepts a name
of a file as parameter and reports some basic statistics about
the contents of the file.
Assume the file contains only integers (one per line), and
use the fscanf function to read each integer in.
Report back the minimum, maximum, sum, and average of the numbers.
|
| 9.
|
Write a function called readDates that accepts a name
of a file as parameter and reads integer decomposable dates from
the file. Assume the file contains only integers (one per line),
and use the fscanf function to read each integer in.
Store the integers in an array and once all integers have been
read, call the dates function from Question 6 to
decompose them. Make sure you continually grow the size of the
array using realloc as you read in more numbers
from the file.
|
10.
|
Extend your stats function from Question 8 such that
it also determines the median and mode of the integers read from
the file.
To do this, you will need to store the integers in an array and
then sort them using the qsort function.
|
11.
|
Using the DATE structure from Question 5, write
a function called datePtrs that accepts an
integer array of dates to "decompose" and returns an array
of DATE pointers, one for each integer date in
the array.
Note that each element of the returned array is now not simply
a DATE, but a pointer to a DATE.
Use your decomposeDate function from Question 5 to
fill the contents of the array.
Your function will also need to be supplied the length of the
integer array and hence should have the prototype:
DATE **datePtrs(int *intDates, int nDates);
|
12.
|
Extend your datePtrs function from Question
11 by using the qsort function to return the
DATEs in chronological order.
|
13.
|
Write a program called calc that reads in the
contents of a file and performs some basic calculations as
dictated by the file.
Assume that each series of three lines in the file follows the
same format: the first two lines contain two numerical values
on separate lines, and the third line contains a character
that denotes a mathematical operation (e.g. 'a'
for addition and 's' for subtraction).
Refrain from using the characters '+' and
'-' to represent operators - they are valid
characters that can denote the start of numerical values and
may cause problems when "scanning-in".
For example, if the file contained the lines:
5
8
a
42
2.1
m
4
2
d
your program should produce the following output:
5.0 + 8.0 = 13.0
42.0 * 2.1 = 88.2
4.0 / 2.0 = 2.0
|
14.
|
Extend your calc program from Question 13 such that
the file can contain any number of numerical operands before
an operator.
For example, if the file contained the lines:
5
8
12
15
a
42
2.1
m
your program should produce the following output:
5.0 + 8.0 + 12.0 + 15.0 = 40.0
42.0 * 2.1 = 88.2
|
15.

|
Using the built-in tm structure and the
mktime function (see man mktime), write
a function to produce a well-formatted textual calendar given a
supplied month and year. Use the cal program as a
guide to how your output should appear. For example,
cal 10 2008 will produce a textual calendar
for October 2008.
|
|
Top of Page
|
|
CRICOS Provider Code: 00126G
|
|