Created
April 28, 2018 13:14
-
-
Save dyigitpolat/0228a2e45eca50fafa9c6a30eb304f8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdio.h" | |
| #include "stdlib.h" | |
| #include "math.h" | |
| #define N 5000000L | |
| float rand_1() | |
| { | |
| float x = (float)rand()/(float)(RAND_MAX/1); | |
| } | |
| float line_avg() | |
| { | |
| float total_dist = 0; | |
| for(long i = 0; i < N; i++) | |
| { | |
| float loc1 = rand_1(); | |
| float loc2 = rand_1(); | |
| float dist = loc1-loc2; | |
| dist = (dist<0 ? -dist : dist); | |
| total_dist += dist; | |
| } | |
| total_dist /= N; | |
| } | |
| float sq_avg() | |
| { | |
| float total_dist = 0; | |
| for(long i = 0; i < N; i++) | |
| { | |
| float loc1x = rand_1(); | |
| float loc2x = rand_1(); | |
| float loc1y = rand_1(); | |
| float loc2y = rand_1(); | |
| float dist = (loc1x-loc2x)*(loc1x-loc2x)+ | |
| (loc1y-loc2y)*(loc1y-loc2y); | |
| dist = (dist<0 ? -dist : dist); | |
| total_dist += sqrt(dist); | |
| } | |
| total_dist /= N; | |
| } | |
| int main() | |
| { | |
| printf("%f\n", line_avg()); | |
| printf("%f\n", sq_avg()); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment