CS1101C Lab 4 (Even Week)

Mini Graph Plotter

The deadline for this lab question is Wednesday 02 November 2005, 23:59:59 hours.

The name of your C program file must be called graph.c, files with any other name will not be marked.

Background

On one fine day, out of a whim, you ask your good friend Doromen: "Hey, do you know what is a quadratic function?". Doromen replies: "Aqua Dramatic? A movie about undersea? How queer!". Immediately, you know you are in deep trouble. "Erm, Doromen, it is actually mathematical function of the form Y = AX2 + BX + C" "Oh?! Whats so interesting about it?" "Erm..., Doromen, they trace beautiful line if you draw them." "You must be kidding me!"

Well, now you have to write a simple graph plotter to convince Doromen....

Details

A graph can be represented by a two-dimension char array (this is only a suggestion; you may use other arrays if you wish). The size of the graph can be represented by the number of rows and columns:

By default, X-Axis is at row 10 (i.e. the 11th rows from top), Y-Axis is at col 30 (i.e. the 31st columns from left). Each "square" in the graph can be represented by a row number and column number. To allow graphs of all sizes to be plotted, the scale for each "square" can be adjusted. The default scale is 0.25, i.e. each row/col represents a 0.25 difference.

Using the default values (X-Axis at row 10, Y-Axis at column 30, scale = 0.25), a graph with several numbered points should appear on the screen as follows. Take some time to count the number of columns and rows to make sure you understand.


                              |
                              |
                              |
                              |
                              |
                              |
                    4         |
                              |
                              |
                              |  1
------------------------------+------------------------------
             3                |
                              |
                              |
                              |
                              |2
                              |
                              |
                              |
                              |
                              |

Since each row/column represents 0.25, the coordinates for the points shown are:

Of course, if the scale changes, the coordinates for the points will change also.

Write a C program to process the following commands:

  1. Plot a function of the form ax2 + bx + c,
  2. Adjust the scale. All plotted formulas should be replotted according to the new scale.
  3. Remove all formula. Plot the empty graph.
  4. Exit the program.

Sample Output

User input is shown in bold font. Text that begins with // are comments; they are not in the actual sample output.

$ gcc -Wall -lm graph.c -o graph
$ ./graph            // Important: Don't leave out the ./
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
------------------------------+------------------------------
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Coefficients (a b c): 1 0 0          // Y = X^2
                              |
                        *     |     *
                              |
                              |
                         *    |    *
                              |
                          *   |   *
                              |
                           *  |  *
                            * | *
-----------------------------***-----------------------------
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Coefficients (a b c): -1 0 0         // Y = -X^2
                              |
                        *     |     *
                              |
                              |
                         *    |    *
                              |
                          *   |   *
                              |
                           *  |  *
                            * | *
-----------------------------***-----------------------------
                            * | *
                           *  |  *
                              |
                          *   |   *
                              |
                         *    |    *
                              |
                              |
                        *     |     *
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Coefficients (a b c): 0 0 2          // Y = 2
                              |
                        *     |     *
*************************************************************
                              |
                         *    |    *
                              |
                          *   |   *
                              |
                           *  |  *
                            * | *
-----------------------------***-----------------------------
                            * | *
                           *  |  *
                              |
                          *   |   *
                              |
                         *    |    *
                              |
                              |
                        *     |     *
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Coefficients (a b c): 0 0 -2         // Y = -2
                              |
                        *     |     *
*************************************************************
                              |
                         *    |    *
                              |
                          *   |   *
                              |
                           *  |  *
                            * | *
-----------------------------***-----------------------------
                            * | *
                           *  |  *
                              |
                          *   |   *
                              |
                         *    |    *
                              |
*************************************************************
                        *     |     *
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 2
Scale: 0.125          // New Scale of 0.125
                     *        |        *
                              |
                      *       |       *
                              |
                       *      |      *
                              |
                        *     |     *
                         *    |    *
                          *   |   *
                           *  |  *
----------------------------*****----------------------------
                           *  |  *
                          *   |   *
                         *    |    *
                        *     |     *
                              |
                       *      |      *
                              |
                      *       |       *
                              |
                     *        |        *
1-Plot 2-Rescale 3-Clear 0-Exit: 2
Scale: 0.5            // New Scale of 0.5
                              |
                              |
                          *   |   *
                              |
                              |
                              |
*************************************************************
                              |
                            * | *
                              |
-----------------------------***-----------------------------
                              |
                            * | *
                              |
*************************************************************
                              |
                              |
                              |
                          *   |   *
                              |
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 2
Scale: 0.25           // New Scale of 0.25
                              |
                        *     |     *
*************************************************************
                              |
                         *    |    *
                              |
                          *   |   *
                              |
                           *  |  *
                            * | *
-----------------------------***-----------------------------
                            * | *
                           *  |  *
                              |
                          *   |   *
                              |
                         *    |    *
                              |
*************************************************************
                        *     |     *
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Coefficients (a b c): 0 1 1          // Y = X + 1
                              |     *
                        *     |    **
*************************************************************
                              |  *
                         *    | *  *
                              |*
                          *   *   *
                             *|
                           ** |  *
                           ** | *
--------------------------*--***-----------------------------
                         *  * | *
                        *  *  |  *
                       *      |
                      *   *   |   *
                     *        |
                    *    *    |    *
                   *          |
*************************************************************
                 *      *     |     *
                *             |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Maximum formulas exceeded.          // Already plotted 5 formula.
1-Plot 2-Rescale 3-Clear 0-Exit: 3
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
------------------------------+------------------------------
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
                              |
1-Plot 2-Rescale 3-Clear 0-Exit: 1
Coefficients (a b c): 0 -1 0         // Y = -X
                    *         |
                     *        |
                      *       |
                       *      |
                        *     |
                         *    |
                          *   |
                           *  |
                            * |
                             *|
------------------------------*------------------------------
                              |*
                              | *
                              |  *
                              |   *
                              |    *
                              |     *
                              |      *
                              |       *
                              |        *
                              |         *
1-Plot 2-Rescale 3-Clear 0-Exit: 0

Assumptions

Tips


This document, index.html, has been accessed 19 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 14 different hosts have accessed this document in the last 445 days; your host, 216.73.216.39, has accessed it 1 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.