Skip to content

Instantly share code, notes, and snippets.

@aXe1
Forked from Levik/gist:892154
Created March 30, 2011 23:15
Show Gist options
  • Save aXe1/895501 to your computer and use it in GitHub Desktop.
Save aXe1/895501 to your computer and use it in GitHub Desktop.
Levik's lab4
#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();
}
@aXe1
Copy link
Author

aXe1 commented Mar 31, 2011

проверял в Visual Studio 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment