Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, I have two one-dimensional arrays to store the locations of 8 coordinate

ID: 3854844 • Letter: I

Question

In C++, I have two one-dimensional arrays to store the locations of 8 coordinate points - one array for the x-coordinates and another for the y-coordinates. These are uniformly distributed coordinates generated using the following code:

const int DATA_SIZE = 8;
int xpos[DATA_SIZE];
int ypos[DATA_SIZE];
for (int i = 0; i < DATA_SIZE; i++)
     xpos[i] = random() % 100;
     ypos[i] = random() % 100;

My next task is to calculate the Euclidean distance between all pairs of x,y coordinates and store the results in a two-dimensional 8x8 array, distance[pointA][pointB]. I know the Euclidean distance formula

     distance = sqrt((xB-xA)2 + (yB - yA)2),

but I don't know how to get all 64 distances and have it displayed in the 2D array of the pictured form.

Any help would be immensely appreciated!

Points 0 0 0 point 0 to 1 point O to 2 point O to 3 point O to 4 point 0 to 5 point 0 to 6 point O to 7 1point 1 to O point 1 to 2 point 1 to 3 point 1 to 4 point 1 to 5 point 1 to 6 point 1 to 7 2 point 2 to O point 2 to 1point 2 to 3 point 2 to 4 point 2 to 5 point 2 to 6 point 2 to 7 point 3 to 4 point 3 to 5 point 3 to 6 point 3 to 7 point 4 to 0 point 4 to 1 point 4 to 2 point 4 to 30 point 4 to 5 point 4 to 6 point 4 to 7 5 point 5 to O point 5 to 1 point 5 to 2 point 5 to 3 point 5 to 40point 5 to 6 point 5 to 7 point 6 to 7 3 point 3 to O point 3 to 1 point 3 to 20 to 4point S to 6 point 5 to 0 point 6 to 0 point 6 to 1 point 6 to 2 point 6 to 3 point 6 to 4 point 6 to 5 point 7 to O point 7 to 1 point 7 to 2 point 7 to 3 point 7 to 4 point 7 to 5 point 7 to 60

Explanation / Answer

Code in C++:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int main() {
const int DATA_SIZE = 8;
int xpos[DATA_SIZE];
int ypos[DATA_SIZE];
int dis[DATA_SIZE][DATA_SIZE];
for(int i=0; i< DATA_SIZE; i++)
{
xpos[i] = rand() % 100;   //random integer values will be generated and assigned to xpos
ypos[i] = rand() % 100;   //random integer values will be generated and assigned to ypos
}
for(int j=0;j<DATA_SIZE;j++)
{
for(int k =0; k<DATA_SIZE;k++)
{ if(j == k)
   dis[j][k] = 0;   //Insert 0 in array when j=k
   else
   dis[j][k] = sqrt(pow((xpos[k]-xpos[j]),2) + pow((ypos[k] - ypos[j]),2));   //Calculate distance using Euclidean distance formula
}
}
cout<<"Coordinates are:";
for(int x=0;x<DATA_SIZE;x++)
cout<<"("<<xpos[x]<<","<<ypos[x]<<") ";   //Display all coordinates
cout<<" Result is: ";
for(int p=0;p<DATA_SIZE;p++)
{
for(int q=0;q<DATA_SIZE;q++)
{
cout<<dis[p][q]<<" ";   //Result Displayed
}
cout<<" ";
}
return 0;
}

Output is: