#include <stdio.h>
int main (void) {
double O,H,L,C;
double typical ;
scanf("%lf %lf %lf %lf", &O, &H, &L, &C);
typical = (H+L+C)/3;
printf("The Day's Typical Price Was: %.2lf \n", typical);
return 0;
}
A little progress at a time. Instead of just taking inputs and creating re-arranged outputs as I did in My First C program, this version manipulates input data with the formula that calculates the day's typical price and then returns the value to the output. I've also graduated from terminal input and terminal output. The next step is to get data from a file that I previously typed, and then output data to a new file.
First, let's get the above code into a file, which we will call pivot4.c. From command line:
milktrader@ubuntu:~/Dropbox/desk.mySimpleC/trunk$ vim pivot4.c
This opens up a new file and calls it pivot4.c. Type in the above code and then, after getting back to Normal mode in vim, type:
:x
This will simultaneously save and close the file. And bring us back to our command line. For readability, I'm only showing the command line from $ sign for the rest of this blog. We want an input file called data4 so we type:
$ vim data4
And we type the following (OHLC SPX prices for July 27, 2010) into the file:
1117.36 1120.78 1109.78 1113.84
Again, we save and close the file, which sends us back to the command line where the fun begins. First, let's compile our program with the following:
$ gcc pivot4.c -o pivot4
Now we assign the input (data4) and output (just invent a name as the file will be automatically created) to our executable with the following:
$ ./pivot4 < data4 > answer4
To check our output, we type the following:
$ vim answer4
When we open this file, we find what we've been looking for. Our daily pivot for July 28, 2010. Now that's a trading application you can use.
Best practices require us to save our work so we first add the files we created to our svn repo with this:
$ svn add pivot4 pivot4.c data4 answer4
And then we commit them with the following:
$ svn commit -m "our fourth C program"
Now it would be really cool if we could avoid typing in our data4 file next time and just get it from Yahoo or something, thus avoiding the possibility of typing errors and reducing stress on our fingers, wrists and eyeballs.
Note: if you'd rather not do the above exercise and you really want to know the answer, the file outputs The Day's Typical Price was: 1114.80.
2 comments: