1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#define PI 3.14159265358979323846
using namespace std;
// Inputs data from a given file into a Two-dimensional array
void read_data(ifstream& in, double point[][2], int table_rows)
{
int i,j;
for(i = 0; i < table_rows; i++)
{
for(j = 0; j < 2; j++)
{
in >> point[i][j];
}
}
}
// Finds minimal and maximal distance between 2 points
void min_max(const double point[][2], int table_rows)
{
int max=std::numeric_limits<int>::min();
int min=std::numeric_limits<int>::max();
//double smal = 10000;
//double large = 0;
int i,j;
double dist;
for(i = 0; i < table_rows-1; i++)
{
for(j = i+1; j < table_rows; j++)
{
dist = sqrt(pow((point[j][0] - point[i][0]), 2) + pow((point[j][1] - point[i][1]), 2));
if (dist < min) dist=min;
if (dist > max) dist=max;
/If I change it to min=dist, max=dist
gives and error-[warning] assignment
[warning] argument
}
}
cout << "The minimum distance between two points is: " << min << endl;
cout << "The maximum distance between two points is: " << max << endl;
}
// Finds the biggest triagle area
void T_area(const double point[][2], int table_rows)
{int max=std::numeric_limits<int>::min();
double largest = 0;
int i,j,k;
double area,distAB,distBC,distCA,s;
for(i = 0; i < table_rows-2; i++)
{
for(j = i+1; j < table_rows-1; j++)
{
for(k = i+2; k < table_rows; k++)
{
distAB = sqrt(pow((point[j][0] - point[i][0]), 2) + pow((point[j][1] - point[i][1]), 2));
distBC = sqrt(pow((point[k][0] - point[j][0]), 2) + pow((point[k][1] - point[j][1]), 2));
distCA = sqrt(pow((point[i][0] - point[k][0]), 2) + pow((point[i][1] - point[k][1]), 2));
//@param s is the semiparameter of the triangle
s = (distAB + distBC + distCA)/2;
area = sqrt(s * (s - distAB) * (s - distBC) * (s - distCA));
if(area > max) max=area;
}
}
}
cout << "The largest area of a triangle is: " << max << endl;
}
int main()
{ ifstream infile;
cout << "Enter the input file name: ";
char filename[1024];
cin.getline(filename, 1024, '\n');
if (infile.fail())
{
cout << "Error opening " << filename << endl;
return 1;
}
const int POINT_ROWS = 50;
const int POINT_COLS = 2;
double point[POINT_ROWS][POINT_COLS];
const int x = 50;
const int y = 2;
double h_point[x][y];
int count = 0;
read_data(infile, point, POINT_ROWS);
min_max(point, POINT_ROWS);
T_area(point, POINT_ROWS);
infile.close();
system ("pause");
return 0;
}
|