ziobrowskyy

szukanie w for flaoty

Nov 24th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <math.h>
  4.  
  5. #define size 10
  6. #define tolerance 0.1
  7.  
  8. float getRandFloat(int min, int max) {
  9. //  float f = (float)(rand() % (max - min + 1) + min);
  10. //  return (float)rand()/(float)RAND_MAX/(float)max;
  11.     float f = ((float)rand() / ((float)RAND_MAX/(float)(max-min)))+min;
  12.     return f;
  13. }
  14.  
  15. void main() {
  16.     srand(time(0)); //tym sie nie przejmuj
  17.    
  18.     float tab[size];
  19.     int min = 0, max = 10;
  20.     for(int i = 0; i<size; i++) {
  21.         tab[i] = getRandFloat(min, max);
  22.         printf("%10.15f\n", tab[i]);
  23.     }
  24.     //tu sie konczy tworzenie tablicy po prostu i zaczyna wlasniwa czesc kodu
  25.    
  26.     float x;
  27.     printf("Poodaj liuczbe z zakresu %d do %d\n", min, max);
  28.     scanf("%f", &x);
  29.     int index = -1, i = 0;
  30.     do {
  31.        
  32.         if(fabs(x - tab[i]) < tolerance) {
  33.             index = i;
  34.         }
  35.         i++;
  36.        
  37.     } while(i < size);
  38.    
  39.     if(index == -1) {
  40.         printf("nie znaleziono");
  41.     } else {
  42.         printf("znalezniono na pozyhcj %d", index);
  43.     }
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment