0

This is the beginning portion of an assignment I'm working on. I'm not familiar with structures yet so I just want to know if I've created the beginning portion correctly. The corresponding portions in bold are the statements that I'm not sure if I've followed the guidelines correctly.

The beginning description:

Start by defining two structure types. All structure types must be documented by saying (1) what a value of this structure represents and (2) what each field represents.

A structure of type Edge represents one edge in a graph. It has three fields: two vertex numbers and a weight. Include a parameterless constructor that sets all three fields to 0.

A structure of type Graph represents a weighted graph. It has four fields:

the number of vertices in the graph, the number of edges in the graph, an array of Edges that holds the edges, the physical size of that array of edges. Include a constructor Graph(nv) that yields a graph with nv vertices and no edges. The constructor must create the array of edges. You can assume that there are no more than 100 edges, but that number must be easy to change. To increase this to 200, for example, should only require changing one line of your program. To achieve that, create a named constant that is the maximum number of edges. For example,

const int maxEdges = 100;

defines constant maxEdges with a value of 100. Any time you need to refer to the maximum number of edges, use maxEdges, not 100.

My development plan for the said description:

struct Edge { int vertex1; int vertex2; int weight; Edge() { vertex1 = 0; vertex2 = 0; weight = 0; } }; struct Graph { int numOfVert; int numOfEdge; int arrayOfEdge[]; const int maxEdges = 100; Graph(int nv) { numOfVert = nv; numOfEdge = 0; arrayOfEdge[maxEdges]; } }; 

I just want to know if I've completely understood what the description is telling me to do. Appreciate any help.

1
  • Apperently you didn't see this: "All structure types must be documented by saying (1) what a value of this structure represents and (2) what each field represents." Commented Mar 2, 2017 at 4:10

1 Answer 1

0

What you have for Edge is correct. You can improve your code a little by initializing the members using initializer lists in the constructor.

Edge() : vertex1(0), vertex2(0), weight(0) { } 

Graph needs a little bit work.

  1. Make maxEdges a non-member constant.
  2. Make arrayOfEdge an array of known size.

Here's my suggestion:

const int maxEdges = 100; struct Graph { int numOfVert; int numOfEdge; int arrayOfEdge[maxEdges]; Graph(int nv) : numOfVert(nv), numOfEdge(0), arrayOfEdge() { } }; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion. A question I have is if I followed your suggestion of making arrayOfEdge an array of known size before the constructor, does that mean I am invalidating the guideline that states The constructor must create the array of edges ?
I don't think you would be violating that guideline.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.