Coordinates of graph

Hello, the input will be the number of vertices then output is to display the number of edges present and then showing the individual coordinates of each graph. The output is something like this:

Sample output:

Enter the number of vertexes: 5

There are 10 edges present in 5 vertices.

Here is the list of edges:
(1, 2) (1, 3) (1, 4) (1, 5)

(2, 3) (2, 4) (2, 5)

(3, 4) (3, 5)

(4, 5 )

My question is how do you display each coordinate of the edges? Thank you for the help.

Here is my code so far wherein I only got to display how many edges will it have according to the number of vertices.

#include <bits/stdc++.h>
using namespace std;

int totalNumberOfEdges(int n)
{
int result = 0;

result = (n * (n - 1)) / 2;

return result;
}


int main()
{

int n;

cout <<"Enter number of vertices:" << endl;
cin>>n;
cout << "There are " << totalNumberOfEdges(n) << " edges in " << n << " vertices.";

return 0;
}
Nested loops.
Outer loop index i runs from 1 to n-1 and gives the first vertex in the bracket.
Inner loop index j runs from i+1 to n and gives the second vertex in the bracket.
Hello,thank you for responding, will this code work? Sorry I'm still starting .

void printEdges(){
int n,i,j;

for(i=0;1<n-1;i++){
for(j=1;i+1<n;j++){
cout << "(" << i << "," << j << ")" << endl;
}
}
The simplest way of finding out if a code works ... is to try it.

No it won't work. Amongst other things, you are confusing 1 and i (so it will probably run indefinitely), every loop limit is wrong, and the line feed is in the wrong place.

The first value in a for-loop is set in the first of the three sections of that expression, not in the middle of it. You can (and should) read about it here:
http://www.cplusplus.com/doc/tutorial/control/#for

Please use code tags when providing code snippets.
Last edited on
Topic archived. No new replies allowed.