-
-
Save aXe1/895501 to your computer and use it in GitHub Desktop.
Levik's lab4
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 <iostream> | |
#include <math.h> | |
#include <conio.h> | |
#include <time.h> | |
// settings | |
#define N 100 | |
#define PRECISION 3 | |
using namespace std; | |
void main() | |
{ | |
float a[N], m, best_diff, nearest, diff, distance; | |
unsigned int i, j, element1, element2, precision_factor; | |
srand(time(0)); | |
precision_factor = pow(float(10), PRECISION); | |
for (i = 0; i < N; i++) | |
a[i] = float(rand() % 10) + float(rand() % precision_factor) / precision_factor; | |
cout << "Array:\n"; | |
for (i = 0; i < N; i++) | |
cout << "a[" << i << "]=" << a[i] << " "; | |
cout << "\n\nEnter M: "; | |
cin >> m; | |
nearest = fabs(m); | |
for (i = 0; i < N; i++) { | |
for (j = 0; j < N; j++) { | |
if (i == j) | |
continue; | |
diff = a[i] - a[j]; | |
distance = fabs(diff - m); | |
if (distance < nearest) { | |
best_diff = diff; | |
nearest = distance; | |
element1 = i; | |
element2 = j; | |
} | |
} | |
} | |
cout << "Nearest is a[" << element1 << "] - a[" << element2 << "] = " << best_diff; | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
проверял в Visual Studio 2010