Lab 3: Structs

The goal of this lab is to learn basic C structs.

1. Introduction

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.

2. Set up

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/* .

This will give you the following files:

3. Given code

The chatterUtil library provides the following functions:

4. Tasks

You need to complete the following tasks:

A. Design and implement the struct

The first thing is to design and implement a struct to to represent chatter messages. This is kind of like writing a class in Java, but simpler. I have already set up a dummy struct for a struct message type (it's in message.h snce the struct type needs to be seen by the library and the drivers.).

There is no real testing that can be done yet, but you can compile just the header file to make sure the syntax is right:

gcc message.h

If the compiler doesn't complain, then at least you don't have a syntax problem... whether you design the struct right or not will come out when you've written the other parts of the lab.

B. Make a new random message

Now we need to produce a new message, that is, a value of our new struct message type.

The header file 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 (message.c, which you'll need to start from scratch) 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.

You'll still need the next piece (displaying a message) to run the driver, as before you can test check that the syntax is right by compiling what you have. Now you can use the makefile to compile message.c:

make message.o

C. Display a message

The headerfile 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 will test this (as well as the other stuff you've written so far). The display of a message should look something like this:

-----------------------
Sent by Verna at 1315499047608
Q: How can you tell if an elephant is in the refrigerator?
A: The door won't shut.
#mischief
#concert
#admiration
#concert

If you want the date to appear as something nice looking instead of a number of milliseconds, instructions for that are found in the optional section at the end of this lab description..

Write the implementation of display() in your message.c. Then compile the whole thing with make (displayDriver is the default target) and test. Remember, this is testing everything you've done so far, so if it's not right, you may need to go back to an earlier step.

D. Sort messages by username

The headerfile 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.

The, to test this, finish the sortDriver program in sortDriver.c. Find the place in that file that indicates you should add some code for this part to dipslay the users names for all the messages in msgs. (Don't call display(); we don't want to display the whole message this time, just the user names.)

Also, edit your makefile to handle sortDriver.o and sortDriver. Compile and test. (When you run sortDriver you'll notice it also displays something for part E, Looking for (some hashcode) .... You can ignore that for now.)

E. Finding a hashcode

Finally, complete the last part of sortDriver.c. In the for loop over the messages, you want to check for each message whether it contains the hashcode found in randomHashCode. If it does, display that message (using your display() function). Remember that a message may have several hashcodes---makes sure you check each one.

Compile and test.

5. Pretty displaying of the time (optional)

If you want to make a readable formatting of the time stamp, do the following things to your code:

Then the display of a message would look something like

-----------------------
Sent by Willis at Thu Sep  8 12:32:37 2011

Q: Why do elephants stomp on people?
A: They like the squishy feeling between their toes.
#surplus
#moisture

Thomas VanDrunen
Last modified: Thu Sep 13 11:29:51 CDT 2012