``````#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..

2
Contributors
1
2
Views
9 Years
Discussion Span
Last Post by VernonDozier

Well this code can certainly be simplified. You don't need all of the cases. A nice formula based on i will do the trick:

``````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;
}
}``````

It's not clear what all of the data represents and what the goal is and what the map is and what computers are hooked up to what, etc., at least not to me. Presumably every terminal has an (x,y) coordinate, they all need to be connected somehow, but the rules aren't clear and thus the map isn't clear. The prompt needs to be better:

``````cout<<"\nSelect "<<count<<" locations for computer terminals."<<endl;

for (i=0; i<count; i++)
{
cout<<"\nTerminal "<<(i+1)<<":	";
cin>>terminals[i];

}``````

The location is represented by a single integer? Presumably there's some algorithm somewhere, but with no comments and vague prompts and variable names, as well as a vague problem specification, it's hard to proceed.

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.

The "chain" has not been defined here, so this hookup is hard to visualize. Everything flows from that. Something needs to be connected. All 20 computers? Just the computers the person is using? There needs to be some algorithm somewhere.

i dont understand some part of it since my groupmate just forwarded it to me.

Ask him. Presumably he knows. Though it happens all the time, unfortunately, people should not expect to simply forward undocumented/uncommented code to other people on the team and expect them to simply figure it out.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.