Sunteți pe pagina 1din 3

Making Text Based Graphs In C

This tutorial will show you how to make a text based graph in the C programming
language. Although text based graphs have many limitations and are of no actual
use, they are fun to make when learning a programming language.

First of all, let us start with the headers. The math.h header is used so that w
e can use mathematical functions.
//WinterNurf Productions
#include <stdio.h>
#include <conio.h>
#include <math.h>
We now start the main() function and declare two variables, i and j, to control
the loop, as well as generate mathematical functions.
int main()
{
int i = 0, j = 0;
We also declare a character and assign it the ASCII value 219. This character is
a block, and we will use it as a data point for ot graph. Any other character o
f your choice may also be used.
char a = 219;
We will now create a nested loop. Our outer loop will comprise of the y - axis (
note that usually the outer loop is used for rows, and so at first it may seem t
hat rows must be used for x - axis. However, give it a thought and it becomes cl
ear that they must be used for y - axis). We use the variable j for this. Also,
we create the loop starting from 80 and ending at 1 because our console applicat
ion starts printing from top left, but we want the origin of our graph to be bot
tom left.
for (j = 80; j >= 1; j--)
The inner loop will comprise of the x - axis, and is pretty straighforward.
for (i = 1; i <= 80; i++)
Now comes the part in which we generate our function (and we do it inside an if
condition). Here, as an example, we have used the logarithm function (A lot of m
athematical functions may be used for this, and I have also included the output
of some of the other functions later on. However, some functions like sin and co
s do give weird results.). The function is given in y = f(x) format in the if co
ndition. It needs to be converted to an integer because all our data points need
to be integers (for the if condition to be true). Also, note that the log funct
ion has been multiplied by 10. This has been done so that when converting to int
eger, the values less than zero do not vanish and become zero.
If this condition is true, we print our data point, which is our character.
if (j == (int)(10 * log (i)))
printf ("%c", a);
You would be wondering why we chose 80 points. This is because the width of the
console application is 80, and so we must use only 80 characters for the x - axi
s. By doing this, we eliminate the need to print a newline character after every
line, and also extend the domain of our graph to the maximum possible.
The y - axis can be of variable length, but for that too we have chosen 80 data

points. However, when no data point matches, a sace needs to be printed, and we
do just that.
else
printf (" ");
That is all we need to print our graph! We now finish off the program.
_getch();
return 0;
}
The output for the logarithm function is as follows. You might need to scroll a
bit.

For the identity function, replace the if condition with


if (j == (int)(i))
The output will be as follows:

For the function y = x^2, replace the if condition with:


if (j == (int)(i * i))
The output will be as follows:

For the function y = tan(x), replace the if condition with:


if (j == (int)(10 * tan(i)))
The output will be as follows:

Here is the full code.


//WinterNurf Productions
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int i = 0, j = 0;
char a = 219;
for (j = 80; j >= 1; j--)
for (i = 1; i <= 80; i++)
if (j == (int)(10 * log(i)))
printf ("%c", a);
else
printf (" ");
_getch();
return 0;
}
This program has been written in Microsoft Visual Studio for Desktop. It may not

work in older compilers. However, simple editing will make the code compatible.

S-ar putea să vă placă și