Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define _CRT_SECURE_NO_WARNINGS
- /* FUNCTIA RETURNEAZA NUMARUL DE PASI ADICA DE COMPARATII, SE CAUTA x */
- int cautare_binara(float x, float* y, int n, int epsilon)
- {
- int contor = 0, s, d, m;
- s = 0;
- d = n;
- while (s < d)
- {
- contor++;
- m = (s + d) / 2;
- if (fabsf(x - y[m]) <= epsilon)
- break;
- if (x < y[m])
- d = m - 1;
- else s = m - 1;
- }
- return contor;
- }
- /* FUNCTIA RETURNEAZA NUMARUL DE PASI ADICA DE COMPARATII, SE CAUTA x */
- int cautare_liniara(float x, float* y, int n, int epsilon)
- {
- int contor = 0;
- for (int i = 0; i < n; i++)
- {
- contor++;
- if (y[i]==x)
- return contor;
- }
- }
- int sortat(float* y, int n)
- {
- for (int i = 0; i < n - 2; i++)
- {
- for (int j = i + 1; j < n - 1; j++)
- {
- if (y[i] > y[j])
- return 0;
- }
- }return 1;
- }
- int main(void) {
- FILE* f, * g;
- unsigned n;
- float *y, epsilon;
- int i = 0, x;
- f = fopen("Prom.txt", "rt");
- g = fopen("stud.txt.txt", "wt");
- fscanf(f, "%d", &n);//memoram in n numarul numerelor din fisier
- y = (float*)malloc(n * sizeof(float*));
- while (!feof(f) && i < n)
- {
- fscanf(f, "%f", &y[i]);
- i++;
- }
- fscanf(f, "%d", &x);
- fscanf(f, "%f", &epsilon);
- if (sortat(y,n))
- fprintf(g, "%d", cautare_binara(x, y, n, epsilon));
- else
- fprintf(g, "%d", cautare_liniara(x, y,n,epsilon));
- }
Advertisement
Add Comment
Please, Sign In to add comment