Classes

Hi, I'm new to C++ and in a course I'm taking, we wrote our first program in C and its objective was to use recursion and an adjacency matrix to find how many possible routes a person could take to get through a subway system without going over a track more than once. That was self explanatory for me but now I'm lost on program 2 which is to do the same problem from program 1 in C++ and using three classes and recursion. I don't even know how to start... how do you go about the transition from a simple adjacency matrix into three classes? It seems counterproductive since it seems more complicated. :X

We've only gone through basic stuff about classes: public, private, members, functions, etc.

Any hints on how to start? I can think of a subwaysystem class, a tracks class, and a stations class.

Thanks.

EDIT:
Oops forgot to include program 1:

#include <stdio.h>

void routesFinder(int row, int col);

//adjacency matrix from given map
int adMatrix[12][12] = {{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}};

char ST[13] = "ABCDEFGHIJKL"; //station letters
char order[25] = "A"; //set up for route order, up to 25 conservatively
int numRoutes = 0, i = 1;

main()
{

routesFinder(0, 0); //start with first row, first column

printf("\nThere are %d possible days before the traveler must repeat a route.\n", numRoutes);

return 0;
}

void routesFinder(int row, int col) {

while (col < 12) { //go through columns of a row
if (adMatrix[row][col] == 0) {
//end of row and no ones
if (row == 11) { //reached goal
numRoutes++; //increment route count
printf("Route %d: %s.\n", numRoutes, order);
return; //backtrack from goal
}
col++;
if (row != 11 && col == 12) { //backtracking from deadend
return;
}
}
if (adMatrix[row][col] == 1) {
order[i] = ST[col]; //add station to route
i++; //increment, prepare for next route
adMatrix[row][col] = 0; //can no longer take path forward
adMatrix[col][row] = 0; //can no longer take path backward
routesFinder(col, 0); //recursion, look for path in new row
order[i] = '\0'; //remove route from list
i--; //decrement, prepare for next route
adMatrix[row][col] = 1; //restore path
adMatrix[col][row] = 1; //restore path
col++; //returning from deadend, check for next open path
if (row != 11 && col == 12) { //backtracking from deadend
return;
}
}
}
}
Last edited on
Maybe they want you to make some tree type classes? I.e. node class, pointers to other nodes it is connected to?
I too, thought about tree since they want a recursive structure. I found a quite useful source about trees, maybe you want to have a look at it.
http://math.hws.edu/eck/cs225/s03/binary_trees/
Topic archived. No new replies allowed.