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

Hey guys, I missed class last week and I need help doing an assignment. I would

ID: 646574 • Letter: H

Question

Hey guys, I missed class last week and I need help doing an assignment. I would appreciate if you could show me a sample program related to this in C.

Graph Matrix:

Create a matrix based graph representation.

It will need to support the following operations.

Ask the user how many points there are.

Ask the user to label those points, ie "A", "B", "C"...

Define the matrix as a square matrix based on the number of points, also keep an array of the labels.

Repeatedly ask the user to define edges between two points. Add these edges to the matrix.

Have a list method that will list out all of the edges in the graph.

Explanation / Answer

#include<iostream>
#include<stdio.h>
using namespace std;
void list(int am[][50], int n, char ver[]);
struct node
{
int from,to;
}a[5], t;
void addEdge(int am[][50],int i,int j)
{
am[i][j] = am[j][i] = 1;
}
int main()
{
int n, c = 0, x, y, ch, i, j;
char ver[50]="",a,b;
cout<<"Enter the no of vertices ";
cin>>n;
int am[50][50];
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
am[i][j] = 0;
}
}
for(i=0;i<n;i++)
{
cout<<"enter label for vertex"<<i+1<<endl;
cin>>ver[i];
}
while (ch != -1)
{
cout<<"Enter the nodes between which you want to introduce edge ";
cin>>a>>b;
for(i=0;i<n;i++)
{
if(ver[i]==a)
x=i;
if(ver[i]==b)
y=i;
}
addEdge(am,x,y);
c++;
cout<<"Press 1 to add another edge and -1 to exit ";
cin>>ch;
}
list(am,n,ver);
}
void list(int am[][50], int n, char ver[])
{
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if(am[i][j]==1)
cout<<"( "<<ver[i]<<" , "<<ver[j]<<" )"<<endl;
}
}
  
}