The goal of this lab is to learn basic C structs.
You are writing software for a Twitter knock-off called Chatter. In the Chatter system, users send messages at most 140 characters long. Additionally, each message has the user id of the user who sent the message (at most 16 characters long), a time stamp indicating the time the message was sent (a long int indicating the number of milliseconds since midnight, Jan 1, 1970), and up to 5 hash codes indicating categories or topics the message falls under (each hash code is at most 10 characters long).
I have provided some code for producing "random" text components and "random" user names. Your tasks will include designing and implementing a struct to represent messages, writing functions for displaying and sorting messages, and completing drivers to test these messages.
Set up a new directory for this lab and move into it.
mkdir lab3 cd lab3
Copy starter code from the course directory.
cp /homes/tvandrun/Public/cs245/lab3/* .
The chatterUtil
library provides the following
functions:
randomInt()
: produce a random int
between 0 (inclusive) and a given int (exclusive).
So randomInt(4)
will return 0, 1, 2, or 3,
with equal probability.
getTimeMillis()
: return the current time
fillRandomText()
: fill the given char
array with a string containing a randomly chosen text
for a chatter message
fillRandomUserName()
: fill the given
char array with a string containing a randomly chosen
text for a user name
fillRandomHashCode()
fill the given
char array with a string containing a randomly chosen
hash code
You need to complete the following tasks:
struct message
type.
You need to complete that struct according to
the description of a message given above.
Since the struct type needs to be seen by
the library and the drivers, it is defined in
message.h
message.h
gives the prototype for the function newRandomMessage()
which is used by the two driver programs.
Write a definition for this function in an
implementation file for the message library.
Use the functions from chatterUtil
to
produce the text, user name, and hash codes.
Use randomInt()
to determine the number of
hash codes.
message.h
gives the prototype for the function display()
which displays a message to the screen, nicely formatted,
showing all the information stored about that
message.
The driver displayDriver
tests this
(as well as your newRandomMessage
).
message.h
gives the prototype for the function sortByUserName
,
and the driver sortDriver.c
uses it.
This function must be completed so that it
sorts an array of messages by user name, alphabetically.
You may use any sorting algorithm for this.
What will be different is that you are sorting strings,
not numbers.
To do this alphabetically, first look at the first character of each
string---remember that characters are represented by numbers,
and the numeric code for characters is consistent with
their alphabetical ordering.
If the first character is a tie, then look to the second
character, etc.
Note that sortByUserName()
also needs
the size of the array as a parameter.
sortDriver
.
The sortDriver
program
is intended to display all the usernames in the
array of messages after sorting,
to search the array for a message containing
a given hashcode, and then display those messages.
You may choose the hashcode to search for.
Finish the driver so that it exercises these
things, adding appropriate functions to
the library as necessary.
Make a typescript showing all the files you modified and showing the program run.