0% found this document useful (0 votes)
127 views5 pages

Briland Hitaj Triangle Problem

This document contains the source code for a C++ program that finds the triangle with the minimum perimeter from a set of coordinate points. It includes functions for calculating distances between points, reading point coordinates from a file, generating combinations of points to form triangles, and calculating each triangle's perimeter to find the minimum. The program outputs the minimum perimeter found, the points defining that triangle, and the execution time. It also writes these results to an output file.

Uploaded by

Briland Hitaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views5 pages

Briland Hitaj Triangle Problem

This document contains the source code for a C++ program that finds the triangle with the minimum perimeter from a set of coordinate points. It includes functions for calculating distances between points, reading point coordinates from a file, generating combinations of points to form triangles, and calculating each triangle's perimeter to find the minimum. The program outputs the minimum perimeter found, the points defining that triangle, and the execution time. It also writes these results to an output file.

Uploaded by

Briland Hitaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FACULTY OF ARCHITECTURE AND ENGINEERING

DEPARTMENT OF COMPUTER ENGINEERING





Advanced Mathematics for Computer Science
Assignment NO: 1
CEN 535
EPOKA UNIVERSITY











Prepared by: Accepted by:
Briland Hitaj Dr. Arban Uka
B. Hitaj
Triangle Problem
1

/**
* TRIANGLE PROBLEM
* Prepared by: Briland Hitaj
* Accepted by: Dr. Arban Uka
* Course: Advanced Mathematics for Computer Science
* EPOKA UNIVERSITY, 2013
*/
#include <iostream>
#include <fstream>
#include <ctime>
#include <math.h>
#define NR_POINTS 50
using namespace std;

float pointCoordinates[NR_POINTS][2];
int storeResult[3] = {0};
float currentPerimeter = 0.0;
float minimumPerimeter = 10000;
int* ourRange; //array to set the range from which we are going to choose values
void head(); //print the head of the program
double euclidean_distance(double, double, double, double); //euclidean_distance function
void getData(); //function to read the input values from the file
void performCalculations(int*, int, int); //function which finds the combinations and performs the neccessary
calculations
// the triangle with the
minimum perimeter

int main()
{
head(); // call to the head function
getData(); // call to the getData function to read information
ourRange = new int[NR_POINTS]; // creating the range array from which we are going to perform
combinations of three elements
for (int i = 0;i<NR_POINTS;i++)
{
ourRange[i]=i;
}
int tempArray[3] = {0, 1, 2};

clock_t startTime = clock(); // B.H.Note: saves the start time of algorithm execution
performCalculations(tempArray, 3, 0);
clock_t endTime = clock(); // B.H.Note: saves the end time of algorithm termination
B. Hitaj
Triangle Problem
2


double timeInSec = double(endTime - startTime) / CLOCKS_PER_SEC; // B.H.Note: finds the time difference
and stores the result in seconds
// B.H.Note: Printing the results
cout<<"Minimum Perimeter found: "<<minimumPerimeter<<endl;
cout<<"First Point with coordinates: "<<pointCoordinates[storeResult[0]][0]<<",
"<<pointCoordinates[storeResult[0]][1]<<endl;
cout<<"Second Point with coordinates: "<<pointCoordinates[storeResult[1]][0]<<",
"<<pointCoordinates[storeResult[1]][1]<<endl;
cout<<"Third Point with coordinates: "<<pointCoordinates[storeResult[2]][0]<<",
"<<pointCoordinates[storeResult[2]][1]<<endl;
cout<<"Algorithm brought the result in: "<<timeInSec<<" sec."<<endl;
// B.H.Note: Printing the results in a file named output.txt
fstream fout("output.txt");
fout<<"Minimum Perimeter found: "<<minimumPerimeter<<endl;
fout<<"Algorithm brought the result in: "<<timeInSec<<" sec."<<endl;
return 0;
}

/**
* B.H.Note: euclidean_distance function is prepared to calculate the distance between two points
*/
double euclidean_distance(double x1, double y1, double x2, double y2){ //
return (double) sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}
/*** END OF FUNCTION ***/

/**
* B.H.Note: getData function reads the points coordinates from a file
* named inputset.txt and stores their values in pointCoordinates array
* for later usage in the code
*/
void getData()
{
ifstream fin("inputset.txt");
float xCoordinate, yCoordinate;
for(int i = 0; i<NR_POINTS; i++)
{
fin>>xCoordinate>>yCoordinate;
pointCoordinates[i][0] = xCoordinate;
pointCoordinates[i][1] = yCoordinate;
B. Hitaj
Triangle Problem
3

//cout<<i<<".\t"<<xCoordinate<<"\t"<<yCoordinate<<endl; //B.H.Note: Uncomment this if you want to see
the input set on the screen
}
}
/*** END OF FUNCTION ***/

/**
* B.H.Note: function performCalculations is the function which
* recursively generates combinations of three from the given input,
* meaning generating different triangles from the given points.
* At the same time, in order to increase the performance, at the moment that
* the new triangle is created, I calculate its perimeter and then compare the
* perimeter with the value stored at the minimumPerimeter variable, in order to
* reset the minimumPerimeter value if necessary and bring out as a result
* the minimum perimeter in the set.
*/
void performCalculations(int *P, int chosen, int init)
{
int resArray[NR_POINTS][3] = {0};

if (chosen == 0)
{
for (int j = 3;j>0;j--)
{
resArray[init-1][j] = P[j];
}
currentPerimeter = euclidean_distance(pointCoordinates[resArray[init-1][0]][0],
pointCoordinates[resArray[init-1][0]][1], pointCoordinates[resArray[init-1][1]][0],
pointCoordinates[resArray[init-1][1]][1]) + euclidean_distance(pointCoordinates[resArray[init-1][0]][0],
pointCoordinates[resArray[init-1][0]][1], pointCoordinates[resArray[init-1][2]][0],
pointCoordinates[resArray[init-1][2]][1]) + euclidean_distance(pointCoordinates[resArray[init-1][1]][0],
pointCoordinates[resArray[init-1][1]][1], pointCoordinates[resArray[init-1][2]][0],
pointCoordinates[resArray[init-1][2]][1]);
if(minimumPerimeter > currentPerimeter)
{
minimumPerimeter = currentPerimeter;
storeResult[0] = resArray[init-1][0];
storeResult[1] = resArray[init-1][1];
storeResult[2] = resArray[init-1][2];
}
}
else{
B. Hitaj
Triangle Problem
4

for (int i = init; i < NR_POINTS; i++)
{
P[chosen] = ourRange[i];
performCalculations(P, chosen-1, i+1);
}
}
}
/*** END OF FUNCTION ***/

/**
* B.H.Note: head function declared only for preparing a header containig information for the program
*/
void head(){
cout<<"*****************************************************************"<<endl;
cout<<"*\tTRIANGLE PROBLEM\t\t\t\t\t*"<<endl;
cout<<"*\tPrepared by: Briland Hitaj\t\t\t\t*"<<endl;
cout<<"*\tAccepted by: Dr. Arban Uka\t\t\t\t*"<<endl;
cout<<"*\tCourse: Adv. Mathematics for Computer Science\t\t*"<<endl;
cout<<"*\tEPOKA UNIVERSITY, 2013\t\t\t\t\t*"<<endl;
cout<<"*****************************************************************"<<endl<<endl;
}
/*** END OF FUNCTION ***/

output.txt
Minimum Perimeter found: 0.381383
Algorithm brought the result in: 0.036 sec.

Figure 1 - Algorithm Output

You might also like