diff options
author | Emile <HanEmile@users.noreply.github.com> | 2018-01-01 20:53:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-01 20:53:43 +0100 |
commit | ca73466265f66598289d905c7321b637dc7e4d89 (patch) | |
tree | 4df16102d71a607e6b31871938238a0166b64717 /src/c | |
parent | 216207aa5a1b3ca4cddfcb0f4e9c111b0bfac9a2 (diff) | |
parent | 19bae8d7db56d4b87ca997b19e1ac1daa68b8555 (diff) |
Merge pull request #4 from HanEmile/c-code
C code
Diffstat (limited to 'src/c')
-rwxr-xr-x | src/c/read | bin | 0 -> 13456 bytes | |||
-rw-r--r-- | src/c/read.c | 133 | ||||
-rw-r--r-- | src/c/testFile.csv | 100 |
3 files changed, 233 insertions, 0 deletions
diff --git a/src/c/read b/src/c/read new file mode 100755 index 0000000..9724dee --- /dev/null +++ b/src/c/read Binary files differdiff --git a/src/c/read.c b/src/c/read.c new file mode 100644 index 0000000..48ece4b --- /dev/null +++ b/src/c/read.c @@ -0,0 +1,133 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <math.h> + +// Define some constants +int range = 100; + +// Define some variables +int sigma = 200; +float f_0 = 0.1; +float R_s = 1e4; + +// Define some constants +float pi = M_PI; +float e = M_E; +float G = 4.302e-3; + +// Prototype pythagoras function +float pythagoras(int x, int y, int z); +// More prototypes +float rho(float r); +float phi(float x); + +int main(int argc, char *argv[] ){ + // Seed random + srand(time(NULL)); + + // Define the number of stars that should be generated using command line + // arguments + long num_of_stars = atoi(argv[1]); + + // define an array to store the coordinates of the stars in + int star_arr[num_of_stars][3]; + + /* + Generate random coordinates + */ + + // generate the random coordinates + for(int i = 0; i < num_of_stars; i++){ + star_arr[i][0] = rand() % range + 1; + star_arr[i][1] = rand() % range + 1; + star_arr[i][2] = rand() % range + 1; + } + + /* + Print out the coordinates + */ + + // print the content of the array star_arr + for(int i = 0; i < num_of_stars; i++){ + printf("%d, %d, %d\n", star_arr[i][0], star_arr[i][1], star_arr[i][2]); + printf("%f\n\n", pythagoras(star_arr[i][0], star_arr[i][1], star_arr[i][2])); + + } + + /* + Generate a lookuptable + */ + + // If the correct amount (2) of command line arguments are given, continue + if( argc == 2 ) { + + // Print out how many stars are being generated + printf("Generating %s Values...\n", num_of_stars); + + // Open a file into which the lookuptable wil be written + FILE * fp = fopen("testFile.csv", "w+"); + + // Abourt if no file is specified + if(fp == NULL){ + printf("Error opening the file!\n"); + exit(1); + } + + // generate the lookuptable + for(int i = 0; i < num_of_stars; i++){ + fprintf(fp, "%d, %f\n", i, phi(i)); + } + + // close the file now containing the lookuptable + fclose(fp); + + } + // Exception: to many arguments + else if( argc > 2 ) { + printf("Too many arguments supplied.\n"); + return 0; + } + // Exception: no argument specified + else { + printf("One argument expected.\n"); + return 0; + } + + // Test if the Star should be generated or not + + // If the star should be generated, write it's coordinates to a file + // Else do nothing + + return 0; +} + +// Define the Pythagorean theorem +float pythagoras(int x, int y, int z){ + float a = pow(x, 2); + float b = pow(y, 2); + float c = pow(z, 2); + float d = a + b + c; + float e = sqrt(d); + return e; +} + +// Define rho function +float rho(float r){ + float a = (1) / ( sqrt(2 * pi) * sigma); + float b = exp( - (phi(r) / pow(sigma, 2) )); + return a; +} + +// Define phi function +float phi(float x){ + if(x == 0) { + float a = -4 * pi * f_0 * G * pow(R_s, 2); + return(a); + } + else { + float a = - (4 * pi * G * f_0 * pow(R_s, 3) / x); + float b = log(1 + (x / R_s) ); + return(a * b); + } +} diff --git a/src/c/testFile.csv b/src/c/testFile.csv new file mode 100644 index 0000000..3568a06 --- /dev/null +++ b/src/c/testFile.csv @@ -0,0 +1,100 @@ +0, -540605.250000 +1, -540668.000000 +2, -540640.937500 +3, -540613.937500 +4, -540425.875000 +5, -540431.062500 +6, -540425.500000 +7, -540413.812500 +8, -540398.375000 +9, -540380.312500 +10, -540360.375000 +11, -540280.750000 +12, -540263.562500 +13, -540244.812500 +14, -540224.875000 +15, -540204.062500 +16, -540182.437500 +17, -540160.187500 +18, -540101.687500 +19, -540080.375000 +20, -540058.500000 +21, -540036.125000 +22, -540013.312500 +23, -539990.187500 +24, -539966.812500 +25, -539943.062500 +26, -539894.375000 +27, -539871.062500 +28, -539847.500000 +29, -539823.750000 +30, -539799.812500 +31, -539775.687500 +32, -539751.312500 +33, -539707.375000 +34, -539683.375000 +35, -539659.125000 +36, -539634.750000 +37, -539610.312500 +38, -539585.687500 +39, -539560.937500 +40, -539520.000000 +41, -539495.562500 +42, -539470.875000 +43, -539446.125000 +44, -539421.312500 +45, -539396.375000 +46, -539371.375000 +47, -539332.625000 +48, -539307.750000 +49, -539282.812500 +50, -539257.812500 +51, -539232.687500 +52, -539207.562500 +53, -539182.312500 +54, -539145.187500 +55, -539120.062500 +56, -539094.937500 +57, -539069.687500 +58, -539044.437500 +59, -539019.125000 +60, -538993.750000 +61, -538968.312500 +62, -538932.562500 +63, -538907.187500 +64, -538881.812500 +65, -538856.375000 +66, -538830.937500 +67, -538805.437500 +68, -538779.937500 +69, -538745.125000 +70, -538719.687500 +71, -538694.125000 +72, -538668.625000 +73, -538643.125000 +74, -538617.500000 +75, -538591.875000 +76, -538557.812500 +77, -538532.312500 +78, -538506.750000 +79, -538481.062500 +80, -538455.437500 +81, -538429.812500 +82, -538404.187500 +83, -538370.750000 +84, -538345.125000 +85, -538319.437500 +86, -538293.750000 +87, -538268.062500 +88, -538242.375000 +89, -538216.625000 +90, -538183.750000 +91, -538158.062500 +92, -538132.312500 +93, -538106.625000 +94, -538080.875000 +95, -538055.125000 +96, -538029.312500 +97, -538003.500000 +98, -537971.187500 +99, -537945.437500 |