Main

Assignment 2

Due: midnight on Wednesday of week 3

In this assignment, you will be working with multidimensional C arrays, memory management, pointers, loops, and functions. You will also create some header files and use a Makefile to compile your code.

Style and logistics:

Your implementation should follow a consistent style, include reasonable documentation (comments). Make sure to initialize all variables before using them.

In your uoregon-cis330 Bitbucket repository create a new directory named assignment2 and place all source files (*.h and *.c) of your solution in it (cd assignment2). See the required file listing at the end of the mandatory tasks.

Overview

The world suddenly needs an ASCII-art clock, but none is to be found! (Ok, there are tons of them out there, but do not become an ASCII clock collector until after you are done with this assignment!). You decide to use your mad C skills to save the world from this terrible shortage.

Because you are more organized than most superheroes who just fly around and throw stuff (albeit amazingly powerfully and accurately without aiming at all), you break down your approach into the following four tasks and follow the naming conventions for the files in each part.

Task 1 (45pts)

In this task, you will define a simple ASCII-art clock program that just prints the current time and exits. If you are not familiar with ASCII art, check out its fascinating history.

  • Create a header file called clock.h with the following contents. This will be the interface you will implement as part of this and subsequent tasks. Fill in the definition of the clock data structure that will be used to store your ASCII clock -- the design is entirely up to you.
#ifndef CLOCK_H_
#define CLOCK_H_

typedef struct {
  // TODO: Define your ASCII clock data structure here. 
} ClockType;

// Initialize the clock data structure 
void initClock( ClockType *clock );

// Print an ASCII clock showing cur_time as the time 
void printClock( const time_t cur_time, const ClockType *clock );

// Free up any dynamically allocated memory in the clock
void cleanClock( ClockType *clock);

#endif /* CLOCK_H_ */
  • In a source file called clock.c, implement the functions in clock.h: initClock, printClock, cleanClock. Do not just print the time on a single line, make each digit at least 8 lines tall for the mandatory tasks in this assignment (as shown in the example output below). You may find the C ctime function helpful. Learn how to use it and related functions from manual pages, cppreference.com or any other reference of your choice. You have to define all three functions in this API, even if they don't do anything.
  • In a separate file called testClock.c, implement the main function, which should include the clock.h header file and call the initClock, printClock, and cleanClock functions (you can have more functions if you wish, but do not change the signatures of these three functions).
  • Compile and test your code with
gcc -std=c11 -g -o clock.exe clock.c testClock.c
./clock.exe

Below is an example clock produced by a solution to this problem; note that you don't have to match this format exactly, as long as the clock is readable. An example executable is on ix, /home/users/norris/public/cis330/assignment2/clock.exe.

norris@ix-dev:~$ ./clock.exe 
  @    @@@        @@@@@ @@@@@          @   @@@  
 @@   @   @       @     @             @@  @   @ 
  @   @       @   @     @       @    @ @      @ 
  @   @@@@        @@@@  @@@@        @@@@@     @ 
  @   @   @           @     @          @     @  
  @   @   @   @       @     @   @      @    @   
  @   @   @       @   @ @   @          @   @    
 @@@   @@@         @@@   @@@           @  @@@@@ 

Task 2 (35 pts)

In this task, you will use the ASCII clock you developed in Task 1 and implement an ASCII-clock timer. You should prompt the user to input the number of minutes and seconds, then have a MM:SS-formatted clock counting down to 00:00, then exit.

Here is an example prompt and user input:

How long should the timer run (MM:SS)? 01:20 
  • Create a header file timer.h containing the following functions (you can have more functions, but the below ones are required. Do not modify the given function signatures.
// Initialize the timer with the user-provided input
void initTimer(ClockType *clock, int minutes, int seconds);

// Run the timer -- print out the time each second
void runTimer();

// Clean up memory (as needed)
void cleanTimer(ClockType  *clock);
  • Create a file timer.c and implement (at least) these three functions and any other functions you find helpful. Also, create testTimer.c with your main function implementation for this task.
  • Test your implementation by including timer.h in your testTimer.c file and adding calls to the above timer functions in your main program or another function that is called from main. Feel free to define any other functions you need.
  • Compile and run
gcc -std=c11 -o timer.exe timer.c testTimer.c
./timer.exe

Task 3 (20 pts)

Using a Makefile, using valgrind.

  • Copy the following files into your assignment2 directory on ix-dev. Both are also in /home/users/norris/public/cis330/assignment2 on ix-dev.
  • Build your code with
make clean
make alien.exe
  • Test the resulting program:
./alien.exe
  • Your assignment2 directory in Bitbucket should have the following required contents:
Makefile
clock.h
clock.c
testClock.c  (has main)
timer.h
timer.c
testTimer.c (has main)

End of mandatory tasks!


4. Optional extra credit (up to 10pts)

Implement a new timer, but instead of printing the remaining time values sequentially in a soul-crushingly tedious and visually displeasing way as is done in task 2, your super programming senses guide you to use the NCURSES library which allows the timer value to change dynamically on the screen, so only the current value is visible. Place your implementation (including main) in a file named smartTimer.c (and commit and push to your repo).

5. (Very) optional extra-credit (up to 10pts) challenge:

Warning: this is a nontrivial problem, only attempt if you have absolutely nothing else to do! Warning++: the points for this problem do not reflect its difficulty proportionally compared to the mandatory problems.

Implement a customizable ASCII clock -- ask the user for the size (e.g., digit height and width), then draw the clock accordingly. Tricky bit -- can you do this without hard-coding a bunch of different sized digits? Place your implementation in a file named superClock.c.

Green Marinee theme adapted by David Gilbert, powered by PmWiki