At this stage we have developed a function funarrays which creates float coordinate arrays from a float function of one float variable, as well as a function minmax to determine the minimum and maximum values of an array. In this exercise we will develop a new class Plot which will allow us to do an interactive character plot of coordinate arrays, with optional automatic scaling between the minimum and maximum values. This new class provides a powerful means of interactively viewing any function of one variable within defined limits.
As an example consider the following program output of a cosine plot:
plot width is 61 characters
enter the low and high values of angle (degrees)
0 360
plot cosine(angle) between [0, 360] degrees
enter the number of points to plot [2,100]
19
number of points entered is: 19
0 I *
20 I *
40 I *
60 I *
80 I *
100 * I
120 * I
140 * I
160 * I
180 * I
200 * I
220 * I
240 * I
260 * I
280 I *
300 I *
320 I *
340 I *
360 I *
minimum value: -1, maximum value: 1
|
This is the function that actually plots the n points of the corresponding (x,y) coordinate arrays. It automatically scales the function in the y-direction (across the screen), and plots the graph in the x-direction down the page, line by line, as shown above. It has the following prototype:
void plotarrays(float x[], float y[], int n, float ymin, float ymax);
// character plot of (x,y) coordinate arrays. x-axis is scrolled,
// y-axis is automatically scaled within ymin and ymax over (ny1-1)
// character positions. (the ny1 position is used for '\0')
|
In the output plot above we used function funarrays to create the n values of the coordinate x and y arrays of the cosine function. In this specific case the minimum and maximum values are known to be -1.0 and 1.0 respectively, hence we did not need to invoke the function minmax.
Consider the following annotated plot of the cosine function.

The central aspect of the plot is the character line array. For each coordinate point as we go down the page the line array is first filled with blank characters; subsequently the axis 'I' character, and finally the asterisk '*' are judiciously assigned to their respective positions in the character line before displaying it on the screen.
The various declarations of the function follow. Note that the number of characters in the line array ny1 is defined by the Constructor of the class Plot and has a default value of 62.
const char blank = ' ';
const char star = '*';
const char ichar = 'I';
const char tab = '\t';
char line[ny1]; // ny characters plus terminating character '\0'
int ny = ny1 - 1;
line[ny] = '\0';
|
Thus we have declared the three relevant plotting characters as well as the line character array which will contain 61 characters and is terminated by the special string terminator '\0'.
In order to do the scaling of the plot we use a function iyfun which has the following prototype:
int iyfun(float y, int ny, float ymin, float ymax); // purpose: find scaled index position iy between [0,(ny - 1)] |
The purpose of this function is to automatically scale the y-value and return the corresponding position iy. This is done in terms of similar triangles, as in the diagram below, followed by the function definition iyfun.
![]() |
int Plot::iyfun(float y, int ny, float ymin, float ymax)
// purpose: find scaled index position iy between [0,(ny - 1)]
{
int iy = int((ny - 1) * (y - ymin) / (ymax - ymin) + 0.5);
return (iy);
}
|
Notice the value 0.5 which is added to the ratio in order to 'round' it to the nearest whole integer. Be sure that you understand the difference between 'rounding' and 'truncation'.
Notice also that if y is greater than ymax, or less than ymin, the value of the index iy returned will be out of range of the line array - either negative or greater than ny. Be sure to augment the method iyfun with an if statement so that the index value returned always remains within the array range.
We now continue with and complete the function plotarrays. Notice how for each coordinate point we first prepare the line array by first blanking it out, inserting the 'I' character, and finally the asterisk '*'. The line is then displayed and this process is repeated for all the coordinate points until the plot is completed.
int iy0 = iyfun(0, ny, ymin, ymax);
for (int i = 0; i < n; i++)
{
for (int iy = 0; iy < ny; iy++)
line[iy] = blank; // blank the entire line
line[iy0] = ichar; // add the axis marker 'I'
int iy = iyfun(y[i], ny, ymin, ymax);
line[iy] = star; // and finally the '*'
cout << setw(12) << x[i] << tab << line << endl;
}
cout << "minimum value: " << ymin
<< ", maximum value: " << ymax << endl;
|
The complete class includes four functions. The function prototypes are shown below:
// a tutorial set of character plotting routines
// FILENAME: plot.h
using namespace std;
class Plot { // character plot of (x,y) coordinate arrays
private:
int ny1; // number of y-axis plot positions + 1
public:
Plot(int numy1 = 62) // Constructor
{
ny1 = numy1;
cout << "plot width is " << ny1-1 << " characters\n";
}
//===========================================================
void minmax(float y[], int n, float& ymin, float& ymax);
// PRE: y is an array containing at least n elements.
// POST: the minimum (ymin) and maximum (ymax) values of the
// n elements of array y are returned.
//===========================================================
int iyfun(float y, int ny, float ymin, float ymax);
// purpose: find scaled index position iy between [0,(ny - 1)]
//===========================================================
void plotarrays(float x[], float y[], int n, float ymin, float ymax);
// character plot of (x,y) coordinate arrays. x-axis is scrolled,
// y-axis is automatically scaled within ymin and ymax over (ny1-1)
// character positions. (the ny1 position is used for '\0')
//===========================================================
void plotarrays(float x[], float y[], int n);
// character plot of (x,y) coordinate arrays. x-axis is scrolled,
// y-axis is automatically scaled over (ny1-1)
// character positions. (the ny1 position is used for '\0')
//===========================================================
};
|
Notice the polymorphic nature of function plotarrays. When it is called with only three arguments then it will use the function minmax to determine the minimum and maximum values of of the y-array. Recall the function minmax that you developed in the previous lab - it can be copied and used here without change. In order to use class Plot you will need to complete the four function definitions of the above prototypes. Two of the function definitions will be required for the lab (functions iyfun and the first plotarrays) and the other two will be required for your final programming exercise.