Saturday, June 27, 2009

55. Plot Graph On a Dumb Terminal

Plotting graphs on a dumb (vt100) terminal

You have a client who is a mathematician and wants you to write a program to plot graphs. Unfortunately, all he has is a vt100 (i.e. ASCII, non-graphic) terminal that connects to the department server. This terminal can display 41 lines of text at a time, and each line has 61 characters. Your graph plotting program must work on this setup.

Your current task is to write a program that plots only linear functions -- you need to see how graphs look on such a terminal, before proceeding further, Remembering that the equation for a linear function is y=ax + b, you see that you need two inputs a,b. You can assume that the range of x and y for which the function needs to be plotted are 0 <= x <= 6.0, and 0 <= y <= 40. Each point on the graph is marked with a "*" and the axes are marked with the characters "|" and "-" (see the sample output). The graph has 60 points, one for each non-zero value of x. Each point is rounded to the nearest character; e.g. y=4.51 for some x implies that a star be drawn corresponding to y=5. If a point is out of range (i.e. if y>40), the point can be ignored. For simplicity, all non-graph points are blanks (" ") in the output file. Any point that lies on an axis is not plotted.

Input

The input has two floating point numbers a,b (a,b > 0).

Output

The output will be the plot of the line on the 60x40 terminal. Note that each line, including the last one (the x-axis), has a newline at the end of it.

Sample Input

10.0 1.0

Sample Output

|                                      *                   
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
| *
|*
|
-------------------------------------------------------------


1 comments:

Anurag said...

/*
Solution for the problem "plotting graphs on a dumb terminal
*/

#include stdio.h
#define DEBUG 0
main()
{
float a,b,x;
char pixel[61][41];
int i,j,y;

/* initialize pixel values */
for(j=1;j<=41;j++)
{
pixel[0][j] = '|';
for(i=1;i<61;i++)
pixel[i][j] = ' ';
}
for(i=0;i<61;i++)
pixel[i][0] = '-';

/* read the inputs */

scanf("%f %f", &a, &b);

if(DEBUG)printf("a = %f, b= %f\n", a, b);

for(i = 1; i <=60; i++)
{
x = 0.1*i;
y = (int)(0.5 + a*x + b);
if((y<=40)&&(y>0)) pixel[i][y] = '*';
}

/* plot the outputs */
for(j = 40; j >= 0; j--)
{
for(i=0;i<61;i++)
printf("%c",pixel[i][j]);
printf("\n");
}

}

Post a Comment