aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov@twistea.su>2024-09-18 16:56:29 +0300
committerjustanothercatgirl <sotov@twistea.su>2024-09-18 16:56:29 +0300
commit2161762fdebf0dfeb6714b658e853550aa3524c4 (patch)
treed28f4604609794ea4ae049071e41a41a0f0916f2 /main.c
parent8e4446ce02f37a0f9ef145a49a82aae1bf7223fe (diff)
initial commit
Diffstat (limited to 'main.c')
-rw-r--r--main.c79
1 files changed, 41 insertions, 38 deletions
diff --git a/main.c b/main.c
index b9bebbb..607d1b2 100644
--- a/main.c
+++ b/main.c
@@ -1,56 +1,59 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
-
-#define _USE_MATH_DEFINES
#include <math.h>
-struct point {
- float x, y;
-};
-
-struct circle {
- // center not needed for this task
- /*struct point center;*/
- float rad;
-};
+#define GEN_MAX 1000000
-char in_circle(const struct circle *c, const struct point p) {
- /*p.x -= c->x;*/
- /*p.y -= c->y;*/
- return p.x * p.x + p.y * p.y < c->rad*c->rad;
+static int tenpow(int pow) {
+ int base = 10;
+ int ret = 1;
+ while (pow) {
+ if (pow & 0b1)
+ ret *= base;
+ base *= base;
+ pow >>= 1;
+ }
+ return ret;
}
-float calculate_pi(int iterations) {
- int in = 0, out = 0;
- int *arr[2] = {&out, &in};
- const struct circle c = {.rad = 1.0f};
- for (int i = 0; i < iterations; ++i) {
- struct point p = {
- .x = rand() % 2000000u / 1e6f - 1,
- .y = rand() % 2000000u / 1e6f - 1
- };
- ++*arr[in_circle(&c, p)];
- }
- return (float)in/iterations * 4;
+static char get_at_pos(unsigned int n, int pos) {
+ n /= tenpow(pos);
+ return n % 10;
}
int main(int argc, char *argv[]) {
srand(time(NULL));
- int iters;
- if (argc == 2) {
+ int arr[10] = {0};
+ unsigned int gen;
+
+ int iters, pos;
+ if (argc == 3) {
iters = atoi(argv[1]);
+ pos = atoi(argv[2]);
+ if (!iters || !pos) goto fail;
} else {
fputs("Iterations: ", stdout);
- if (!scanf("%i", &iters)) {
- fputs("Input a valid number\n", stderr);
- return EXIT_FAILURE;
- }
+ if (!scanf("%i", &iters)) goto fail;
+ fputs("Position (10^N): ", stdout);
+ if (!scanf("%i", &pos)) goto fail;
}
- float pi = calculate_pi(iters);
- float err = fabs(M_PI-pi)/M_PI;
- printf( "calculated pi: %.4f, actual pi: %.4f\n"
- "error: %.2f%%\n",
- pi, M_PI, err * 100);
+
+ for (int _ = 0; _ < iters; ++_) {
+ gen = rand() % GEN_MAX;
+#ifdef PRINT_GENERATED
+ printf("%i\n", gen);
+#endif
+ ++arr[get_at_pos(gen, pos)];
+ }
+
+ printf("count of numbers at position %i:\n", pos);
+ for (int i = 0; i < 10; ++i)
+ printf("\t%i: %i\n", i, arr[i]);
+
return EXIT_SUCCESS;
+fail:
+ fputs("Input a valid number\n", stderr);
+ return EXIT_FAILURE;
}
+