Advertisement
artbakulev

Eduard | lab 6 | individual task

Feb 24th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float *input(int);
  6.  
  7. int main() {
  8.     float R;
  9.     int N;
  10.  
  11.     printf("Введите число R: ");
  12.     scanf("%f", &R);
  13.     printf("Введите размер массива: ");
  14.     scanf("%d", &N);
  15.     float *array = input(N);
  16.  
  17.     float number = array[0];
  18.     int numberIndex = 0;
  19.     for (int i = 1; i < N; i++) {
  20.         if (fabsf(array[i] - R) < fabsf(number - R)) {
  21.             number = array[i];
  22.             numberIndex = i;
  23.         }
  24.     }
  25.     printf("Самое близкое число к %f - это %f с индексом %d", R, number, numberIndex);
  26.  
  27.     return 0;
  28. }
  29.  
  30.  
  31. float *input(const int N) {
  32.     printf("Введите %d чисел: ", N);
  33.     float *array = (float *) malloc(N * sizeof(float));
  34.     for (int i = 0; i < N; i++) {
  35.         scanf("%f", &array[i]);
  36.     }
  37.     return array;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement