What exactly is a piece of art? Is this a piece of art?
//Exercise 9
#include<stdio.h>
#include<windows.h>
#include<gl/gl.h>
#include<gl/glut.h>
float index[100]={0}, MA[100]={0}, bar_width, bar_height, height_scale, index_max;
int N, M;
int main(int argc, char**argv)
{
void userinput(int*, int*); //asking for user input
void calculateMA(int, int, float[], float[]); //calculate moving average
void printMA(int, int, float[], float[]); //
float compare_index(float[], int); // find the index with max value (to calculate height scale)
void graph(void); //draw axis, title, legend and bar chart
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowPosition(10, 50);
glutInitWindowSize(700, 350);
glutCreateWindow("Moving Average for a 15-Day Series");
userinput(&N, &M);
calculateMA(N, M, index, MA);
printMA(N, M, index, MA);
index_max = compare_index(index, N);
bar_width = 0.8/N;
height_scale = 1.6/index_max;
glutDisplayFunc(graph);
glutMainLoop();
return 0;
}
//---------------------------------graph function----------------------------------------------------------
void graph(void)
{
void MA_lines(int, int, float, float, float[]); //draw moving average lines and dots
int i, j;
float red, green, blue, x;
char title[40] = {"Moving Averages for a N days series"};
char MAvg[3][30]= {{"3-day moving average"},{"4-day moving average"},{"5-day moving average"}}; //Take note
glClearColor(1.0, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//write title
glColor3f(0.0, 0.0, 0.0);
glLineWidth(3.0);
glRasterPos2f(-0.8, 0.85);
for (i=0; i<40; i++)
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, title[i]);
//draw axis
glBegin(GL_LINE_STRIP);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.8, -0.8);
glVertex2f(0.8, -0.8);
glEnd();
//draw legend lines
glLineWidth(1.0);
glPointSize(3.0);
for (i=0, x=-0.9; i<3; i++,x=x+0.6)
{
if (i == 0)
{ red=1.0; green=0.0; blue=0.0; }
else if (i == 1)
{ red=0.0; green=1.0; blue=0.0; }
else if (i == 2)
{ red=0.0; green=0.0; blue=1.0; }
glColor3f(red, green, blue);
glBegin(GL_LINES);
glVertex2f(x, -0.9);
glVertex2f(x+0.05, -0.9);
glEnd();
glBegin(GL_POINTS);
glVertex2f(x+0.025, -0.9);
glEnd();
//write legend words
glColor3f(0.0, 0.0, 0.0);
glRasterPos2f(x+0.1 , -0.9);
for (j=0; j<30; j++)
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, MAvg[i][j]);
}
//draw barchart
glColor3f(1.0, 1.0, 0.5);
glLineWidth(3.0);
for(i=1, x=-0.8+bar_width; i<=N; i++,x=x+2*bar_width)
{
glPolygonMode(GL_FRONT, GL_FILL);
glRectf(x,-0.79, x+bar_width, -0.8+height_scale*index[i]);
}
//draw moving average lines
for (i=1; i<=3; i++)
{
if (i == 1)
{ red=1.0; green=0.0; blue=0.0; }
else if (i == 2)
{ red=0.0; green=1.0; blue=0.0; }
else if (i == 3)
{ red=0.0; green=0.0; blue=1.0; }
MA_lines(N, i, bar_width, height_scale, MA);
}
glFlush();
}
//-------------------------------MA_lines function-----------------------------------
void MA_lines(int N, int M, float bar_width, float height_scale, float MA[] )
{
int i;
float x;
//draw MA lines
glColor3f(1.0, 0.0, 0.0);
glLineWidth(1.0);
glBegin(GL_LINE_STRIP);
for(i=M, x=-0.8+(2*M-0.5)*bar_width; i<=N; i++,x=x+2*bar_width)
{
glVertex2f(x,-0.8+height_scale*MA[i]);
}
glEnd();
//draw MA points
glPointSize(3.0);
glBegin(GL_POINTS);
for(i=M, x=-0.8+(2*M-0.5)*bar_width; i<=N; i++,x=x+2*bar_width)
{
glVertex2f(x,-0.8+height_scale*MA[i]);
}
glEnd();
glFlush();
}
//--------------------------user input function---------------------------------------
void userinput(int *N, int*M)
{
do
{
printf("Enter number of days of index supplied: ");
scanf("%d", N);
printf("Enter the number of days of moving average required: ");
scanf("%d", M);
printf("\n");
} while (*N<*M);
}
//-------------------------calculate MA function----------------------------------------
void calculateMA(int N, int M, float a[], float b[])
{
int i, j;
for (i = 1; i<=N; i++)
{
printf("Enter index of day %d: ", i);
scanf("%f", &a[i]);
}
for (i = M; i<=N; i++)
{
for (j = 0; j < M; j ++)
{
b[i] = b[i] + a[i-j];
}
b[i] = b[i]/M;
}
}
//------------------------------printMA function---------------------------------------
void printMA(int N, int M, float a[], float b[])
{
int i;
printf("\n Day Index %d-day MA\n", M);
printf("------------------------------\n");
for (i = 1; i<=N; i++)
{
printf(" %d %8.1f %6.1f\n", i, a[i], b[i]);
}
printf("\n");
}
//---------------------------compare_index function--------------------------------------------
float compare_index(float index[], int N)
{
int i, max_value = index[1];
for (i=2; i<=N; i++)
{
if (max_value < index[i])
max_value = index[i];
}
return max_value;
}
I spent days trying to work out how to solve this problem. I write every single word of this code myself. And in the end, I get the computer to draw a graph for me.
To me, this a piece of art.
PS. This piece of art got 缺陷美, because it is incomplete. There's room for improvement.
PPS. Programming is simple. To be good in programming, there is a secret: You just become one with the computer! You think like a computer, act like a computer, be a computer. I think this secret comes too late to some pple, but it applies to everything else - drving, cooking, maths whatever. Just use your imagination. =)
PPPS. If you think what I've written is jus crap, remember, you are in a forbbiden section of my world. WHAT ARE YOU DOING HERE?!