Hello all,
I need help with this assignment I am doing for my data structures class. I need to make a Depth-First Search Algorithm.
In order to do this, I have thought of a clever(well, maybe not that clever but hey I am still a student so gimme a break) way to take care of this. I have decided to use a 2 dimensional array and make a adjacency list. So, suppose we have the nodes, {0,1,2,3,4,5} and we have the following connections:
{(0,1)(0,2)(0,5)(1,3)(2,5)(3,4)} that would mean that we have an adjacency list like so:
012345
0 |011001|
1 |000100|
2 |000101|
3 |000010|
4 |000000|
5 |000000|
According to the adjacency list and assuming that our start is 0 and our goal is 4, one possible path might be [0, 1, 3, 4]
Now, here's the problem: I am having trouble populating this 2-dimensional array. Here's the code I have so far. The explanation of the code is after the code itself
Code:
#include <iostream.h>
void printArray( int a[5][5] );
int main(){
int num1, num2;
const int delim = -1;
int array_test[5][5];
while (num1 != delim){
cin >> num1;
cin >> num2;
for ( int i = 0; i <= 5; i++ ){
array_test[num1][num2] = 1;
}
printArray(array_test);
return 0;
}
}
void printArray( int a[ 5 ][ 5 ] )
{
for ( int i = 0; i <= 5; i++ ){
for ( int j = 0; j <= 5; j++ )
cout << a[ i ][ j ] << ' ';
cout << endl;
}
}
The input is supposed to the program is as follows:
//the first two numbers are the start and the goal
//i.e - start at node 0 and end at node 4
04
//the rest are connections from node - to - node
01
02
05
13
23
25
34
//when user wants to end edge input type in -1 for both nodes
-1-1
First of all, I have no idea how to give the user this kind of flexibility in input(isn't it easier to put into a regular text file and then read from the text file?). Second of all, if anyone would run my code, they would see that my array does not get printed up right. I will be eternally grateful to whomever can help me. Thank you.