This program will draw rather rough graphs of functions The
This program will draw (rather rough) graphs of functions. The function being graphed and the region of the graph to be displayed will be hard-coded into the program, but in such a way that the program can easily be modified and recompiled to graph different functions and display different sized regions of the graph. The function f(x) = sin(3*x) for x and y in [-2, 2] will be graphed as:
and the function f(x) = x2 for x in [-3, 3] and y in [-2, 2] will be graphed as:
Constructing the graph
You will define constants using #define for the region to be displayed. The origin will always be at the center. You will choose a constant X_SEMIRANGE indicating that the horizontal range covered is [-X_SEMIRANGE, X_SEMIRANGE] and a constant Y_SEMIRANGE indicating that the vertical range covered is [-Y_SEMIRANGE, Y_SEMIRANGE]. These are defined, for example, by:
near the top of your program where they can be easily modified. Throughout the program the ranges will always be referenced using these constants. Each unit in the x or y direction will occupy 10 spaces (xdirection) or 10 lines (y direction).
You will also write a function double f(double) which holds the function to be graphed. This can be easily rewritten (and the program recompiled) to graph different functions.
The graph will be stored in a 2-dimensional character array:
(The extra space -- + 1 -- in each dimension is necessary to include 0). Your program will first initialize the entire array to spaces (\' \') and then draw the x and y axes with the characters \'-\' and \'|\'. It will then plot the points on the graph by calling f() in a loop and writing the character \'o\' in the corresponding position in the array. Doing the plot correctly requires some careful thought.
Notes on plotting the graph
You will have to round off the double values of f() to ints before storing points in the array. Do not just type cast the double value to an int as this truncates rather than rounds off the converted values. Rounding a double x can be accomplished by:
Do not call a function you write here round() because that will conflict with the round() function in the math library.
Sometimes the point to be graphed for f() is outside of the vertical range of the graph. In this case the point will not be plotted. You must check this for each point you plot. (See the graph of f(x) = x2above).
The first index in graph[][] corresponds to the y coordinate of points on the graph and the second index corresponds to the x coordinate. Points on the graph display move upwards as values increase butdownwards as the first index of graph[][] increases.
Many mathematical functions are available in the standard library. To use these you must #include <math.h>. When you use functions from the math library you have to link your program with the math library when you compile. Use the \"-lm\" switch to do this:
gcc -o project2 project2.c -lm
Organization
You will make appropriate use of C functions and each function (including main()) will be short. Your program will be well commented.
Testing your program
Ensure that your program compiles and runs without errors on zeus.
Solution
#include<stdio.h>
#include<math.h>
#define X_SEMIRANGE 3
#define Y_SEMIRANGE 2
char graph[Y_SEMIRANGE*20+ 1][X_SEMIRANGE*20 + 1];
int round_d(double x)
{
int i;
if (x >= 0)
i = (int)(x + 0.5);
else /* x < 0 */
i = (int)(x - 0.5);
return i;
}
void set_point(double y,double x){
int x1,y1;
y=-y;
x = x + X_SEMIRANGE ;
y = y + Y_SEMIRANGE;
x1 = round_d(x*10);
y1 = round_d(y*10);
if(x1>=0 && x1<=20*X_SEMIRANGE)
if(y1>=0 &&y1<=20*Y_SEMIRANGE)
{
graph[y1][x1] = \'1\';
}
}
double f1(double x){
double y;
y = sin(3*x);
return y;
}
double f2(double x){
double y;
y = x*x;
return y;
}
int main(){
double z;double x,y;
int i,j;
for(x=-X_SEMIRANGE;x<=X_SEMIRANGE;x++)
{
z=x;
for(i=0;i<10;i++)
{
set_point(f1(z),z);
z +=1.0/10;
}
}
for(j=0;j<Y_SEMIRANGE*20 +1;j++)
{
printf(\"\ \");
for(i=0;i<X_SEMIRANGE*20 +1;i++)
{
if(j == 10*Y_SEMIRANGE)
printf(\"-\");
else if(i == 10*X_SEMIRANGE)
printf(\"|\");
else
if(graph[j][i]==\'1\')
printf(\"0\");
else
printf(\" \");
}
}
memset(graph,0,sizeof(graph));
for(x=-X_SEMIRANGE;x<=X_SEMIRANGE;x++)
{
z=x;
for(i=0;i<10;i++)
{
set_point(f2(z),z);
z +=1.0/10;
}
}
for(j=0;j<Y_SEMIRANGE*20 +1;j++)
{
printf(\"\ \");
for(i=0;i<X_SEMIRANGE*20 +1;i++)
{
if(j == 10*Y_SEMIRANGE)
printf(\"-\");
else if(i == 10*X_SEMIRANGE)
printf(\"|\");
else
if(graph[j][i]==\'1\')
printf(\"0\");
else
printf(\" \");
}
}
system(\"pause\");
}
OUTPUT:


