Help with this code

I am writing a code that will ask a user to input no. of corners and the coordinates,the distance between two corners and perimeter of the shape and the area. I am stuck on how to connect the 1st corner and last corner. Also I am not sure on how to include the parameter.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
void coordinates_of_vertices(int vertices,double x[],double y[]);
void distance_between_two_vertices (int vertices,const double x[], const double y[], float totaldistance[]);
int main()
{
int vertices;
double x[10], y[10], depth;
float totaldistance[10];
cout<<"Enter the number of vertices of the swimming pool"<<endl; // Asked the user to input number of vertices.
cin>>vertices;
if (vertices<3)
{cout<<"The vertices needs to be greater or equal to 3."<<endl<<" Please Try again."<<endl;}
else // if the if statment is true then program will execute.
{
cout<<"Enter the depth of the swimming pool"<<endl;// Asked the user to enter the depth.
cin>>depth;
if (depth<=0)
{cout<<"Depth must be larger than 0."<<endl<<"Please Try Again."<<endl;}
else{
coordinates_of_vertices (vertices, x, y);//function to ask coordinates.
distance_between_two_vertices (vertices, x, y, totaldistance);//function to ask the distanc ebetween two vertices.
}
}
system ("pause");
return 0;

}


void coordinates_of_vertices(int vertices,double x [10],double y[10])
{
for (int a=0;a<vertices;a++)
{cout<<"Enter the coordinates"<<endl;
cin>>x[a]>>y[a];
cout<<"The x is "<<x[a]<<" and the y is "<<y[a]<<" for vertices "<<a+1<<endl;}
system ("pause");
}
void distance_between_two_vertices(int vertices,const double x[10], const double y[10], float totaldistance[10])
{
double distancex,distancey;
for (int j=0;j<vertices-1;j++)
{distancex=pow(x[j+1]-x[j],2);
distancey=pow(y[j+1]-y[j],2);
totaldistance[j]=sqrt(distancex+distancey);
cout<<"the distance between vertices "<<j+1<<"("<<x[j+1]<<","<<y[j+1]<<")"<< "and vertices "<<j+2<< "("<<x[j]<<","<<y[j]<<") is "<<totaldistance[j]<<endl;}
system ("pause");
}
Your first and last vertices are of course, x[0],y[0] and x[vertices-1],y[vertices-1].

You have perverted distance_between_two_vertices to display all the distances instead of simply doing the calculation between any two vertices as the name of the function would imply.

Consider rewriting distance_between_two_vertices as follows, where you pass it any two vertices and it returns the distance.
 
double distance_between_two_vertices (double x1, double y1, double x2, double y2)


Then rewrite your existing function as a new function, e.g. display_all_distances, which calls distance_between_two_vertices for each pair, and the pair x[vertices-1],y[vertices-1] and x[0], y[0].

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.