#include <iostream>
#include <algorithm>
#include <conio.h>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int terminals[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int count,row,col,i;
int limitcomp[20];
int coordinates[2][20];
double smallest = 0.0;
double current = 0.0;
double x, y;
int network = 1;
double chunk, total;
do
{
cout<<"Enter number of computers: ";
cin>>count;
if (count>20)
cout<<"Number of computers exceeded the maximum limit. Maximum of 20 only."<<endl;
}while(count>20);
cout<<"Enter the interval of computers in row: ";
cin>> row;
cout<<"Enter the interval of computers in column: ";
cin>>col;
//----------------------------------------------------------------
//MAP//
cout<<"\n*(16) *(17) *(18) *(19) *(20)"<<endl;
cout<<"\n*(11) *(12) *(13) *(14) *(15)"<<endl;
cout<<"\n*(6) *(7) *(8) *(9) *(10)"<<endl;
cout<<"\n*(1) *(2) *(3) *(4) *(5)"<<endl;
cout<<"-------------------------------------"<<endl;
//----------------------------------------------------------------
cout<<"\nSelect "<<count<<" locations for computer terminals."<<endl;
for (i=0; i<count; i++)
{
cout<<"\nTerminal "<<(i+1)<<": ";
cin>>terminals[i];
}
while(count!= 0)
{
current = 0.0;
smallest = 0.0;
total = 0.0;
chunk = 0.0;
for (int i=0; i<count; i++)
{
switch (terminals[i])
{
case 1: coordinates[0][i]=(row); coordinates[1][i]=(col); break;
case 2: coordinates[0][i]=(2*row); coordinates[1][i]=(col); break;
case 3: coordinates[0][i]=(3*row); coordinates[1][i]=(col); break;
case 4: coordinates[0][i]=(4*row); coordinates[1][i]=(col); break;
case 5: coordinates[0][i]=(5*row); coordinates[1][i]=(col); break;
case 6: coordinates[0][i]=(row); coordinates[1][i]=(2*col); break;
case 7: coordinates[0][i]=(2*row); coordinates[1][i]=(2*col); break;
case 8: coordinates[0][i]=(3*row); coordinates[1][i]=(2*col); break;
case 9: coordinates[0][i]=(4*row); coordinates[1][i]=(2*col); break;
case 10: coordinates[0][i]=(5*row); coordinates[1][i]=(2*col); break;
case 11: coordinates[0][i]=(row); coordinates[1][i]=(3*col); break;
case 12: coordinates[0][i]=(2*row); coordinates[1][i]=(3*col); break;
case 13: coordinates[0][i]=(3*row); coordinates[1][i]=(3*col); break;
case 14: coordinates[0][i]=(4*row); coordinates[1][i]=(3*col); break;
case 15: coordinates[0][i]=(5*row); coordinates[1][i]=(3*col); break;
case 16: coordinates[0][i]=(row); coordinates[1][i]=(4*col); break;
case 17: coordinates[0][i]=(2*row); coordinates[1][i]=(4*col); break;
case 18: coordinates[0][i]=(3*row); coordinates[1][i]=(4*col); break;
case 19: coordinates[0][i]=(4*row); coordinates[1][i]=(4*col); break;
case 20: coordinates[0][i]=(5*row); coordinates[1][i]=(4*col); break;
}
}
sort(terminals, terminals+count);
do
{
current = 0.0;
for(int i = 0; i < count-1 ; i++)
{
x = (coordinates[0][(i)-1]) - (coordinates[0][(i+1)-1]);
y = coordinates[1][(i)-1] - coordinates[1][(i+1)-1];
current += sqrt(x*x + y*y);
}
if ((current < smallest) || (smallest == 0.0))
{
smallest = current;
for (int i = 0; i < count; i++)
{
limitcomp[i] = terminals[i];
}
}
}
while (next_permutation(terminals, terminals+count));
cout << "**********************************************************\n"
<< "Network #" <<count << "\n";
for (i = 0; i < count - 1; i++)
{
cout << "Cable requirement to connect "
<<terminals[i] << " to "
<< terminals[i+1] << " is ";
x = coordinates[0][limitcomp[i]-1] - coordinates[0][limitcomp[i+1]-1];
y = coordinates[1][limitcomp[i]-1] - coordinates[1][limitcomp[i+1]-1];
chunk = sqrt(x*x + y*y) ;
total += chunk;
cout<<setprecision (2)<<chunk<< " meters.\n";
}
cout << "Number of meters of cable required is "<< setprecision (2)<< total << endl;
network++;
cin >> count;
}
return 0;
}
our task is to determine how the computers should be connected into such a chain to minimize the total amount of cable needed. there are 20 terminals each has a corresponding coordinate(x,y). the user will just choose which terminal he will put his pc's.
1. you wil ask the user first if how many pc's he will use (should be less than or equal to 20)
2. enter the terminal number he will use (1-20)
3. calculate minimum path for cabling
this code is a disaster. it doesnt give the correct answer and i dont understand some part of it since my groupmate just forwarded it to me. please help me understand this code by providing comments and finding the reason why it doesnt give a correct answer..
add me on your ym. eaon21@yahoo.com