From 7f409708c3ca3e0af213efa4d6f8390b4f46f78e Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 1 Jan 2018 20:11:15 +0100 Subject: update --- src/c/read.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/c/read.c (limited to 'src/c/read.c') diff --git a/src/c/read.c b/src/c/read.c new file mode 100644 index 0000000..109c603 --- /dev/null +++ b/src/c/read.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +// Define some constants +int range = 100; + +// Prototype pythagoras function +float pythagoras(int x, int y, int z); + +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 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 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])); + + } + + // 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; +} + +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; +} -- cgit 1.4.1 From eed019682e3d54334fbcb3b9a612086cfb9d4295 Mon Sep 17 00:00:00 2001 From: hanemile Date: Mon, 1 Jan 2018 20:48:48 +0100 Subject: merged the two files langfassung.c and read.c --- src/c/lookup | Bin 13200 -> 0 bytes src/c/lookup.c | 74 ---------------------------------------------------- src/c/read | Bin 12880 -> 13456 bytes src/c/read.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 74 deletions(-) delete mode 100755 src/c/lookup delete mode 100644 src/c/lookup.c (limited to 'src/c/read.c') diff --git a/src/c/lookup b/src/c/lookup deleted file mode 100755 index 6a6368d..0000000 Binary files a/src/c/lookup and /dev/null differ diff --git a/src/c/lookup.c b/src/c/lookup.c deleted file mode 100644 index 01b5508..0000000 --- a/src/c/lookup.c +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include - -// 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; - -// Define the directory where the data shall be stored - -// Prototype -float rho(float r); -float phi(float x); - -int main(int argc, char *argv[] ){ - if( argc == 2 ) { - printf("Generating %s Values...\n", argv[1]); - - FILE * fp = fopen("testFile.csv", "w+"); - if(fp == NULL){ - printf("Error opening the file!\n"); - exit(1); - } - - int x = atoi(argv[1]); - - for(int i = 0; i < x; i++){ - fprintf(fp, "%d, %f\n", i, phi(i)); - } - - // for(int i = 0; i < 1e7; i = i + 1e4){ - // printf("%s, %f\n", i, phi(i)); - // } - - fclose(fp); - - } - else if( argc > 2 ) { - printf("Too many arguments supplied.\n"); - return 0; - } - else { - printf("One argument expected.\n"); - return 0; - } - - return 0; -} - -// 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/read b/src/c/read index 97751d7..9724dee 100755 Binary files a/src/c/read and b/src/c/read differ diff --git a/src/c/read.c b/src/c/read.c index 109c603..48ece4b 100644 --- a/src/c/read.c +++ b/src/c/read.c @@ -6,8 +6,21 @@ // 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 @@ -20,6 +33,10 @@ int main(int argc, char *argv[] ){ // 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; @@ -27,6 +44,10 @@ int main(int argc, char *argv[] ){ 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]); @@ -34,6 +55,45 @@ int main(int argc, char *argv[] ){ } + /* + 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 @@ -42,6 +102,7 @@ int main(int argc, char *argv[] ){ return 0; } +// Define the Pythagorean theorem float pythagoras(int x, int y, int z){ float a = pow(x, 2); float b = pow(y, 2); @@ -50,3 +111,23 @@ float pythagoras(int x, int y, int z){ 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); + } +} -- cgit 1.4.1