Reputation: 31
I am writing the following program in C.
This program is a adjacency matrix, which asks the user to set a connection between nodes and than check to see if there is a connection between node A and node B.
# include <stdio.h>
# include <stdlib.h>
#define N 11
#define FALSE 0
#define TRUE 1
typedef int adj_mat[N][N]; /*defining adj_mat */
int path (adj_mat A, int u, int v);
The main function asks user to make directed graph and then asks for the user to enter two nodes to check if they exists a connection between node A and node B.
int main()
{
adj_mat Matrix; /*intializing a new graph adjacency matrix.
on this moment nodes are disconnected every cell contains zero */
int dadnode, sonnode; /*intializing dad node and son node*/
printf("Hello. Enter now the pairs of connected nodes.\n");
printf("enter EOF after finishing of connecting all the nodes\n");
do { /*here user enter the nodes to connect */
printf("Enter the number of first node\n");
scanf("%d", &dadnode);
printf("Enter the number of second node\n");
scanf("%d", &sonnode);
if ((dadnode < sonnode) && (sonnode <= N) && (dadnode > 0)) /*checking if nodes are legal*/
Matrix[dadnode][sonnode] = 1; /*if legal - connect*/
} while ( (dadnode != EOF ) && (sonnode != EOF)); /*until user enter EOF */
printf("Now enter u and v nodes to check if exists way from u node to we node\n");
/*here user enter the nodes to check */
printf("Enter the number of u node\n");
scanf("%d", &dadnode);
printf("Enter the number of v node\n");
scanf("%d", &sonnode);
if ((dadnode < sonnode) && (sonnode <= N) && (dadnode > 0)) /*checking if nodes are legal*/ {
if( path(Matrix,dadnode,sonnode) == TRUE ) /*if exisits way from u to v*/
printf ("Exists way from node u to node v ");
}
else printf ("Not exists way from node u to node v ");
}
The following function returns TRUE if exists way from u(dad node) to v(son node) otherwise returned FALSE
int path (adj_mat A, int u, int v) {
if (v >= u) /*no sense to check if dad node yonger than son node or dad of himself */
return FALSE;
int nodenum; /*number of node*/
/* "nodenum = v - 1" because node v cannot be son of node >= v */
for(nodenum = v - 1; nodenum > 0; nodenum-- ) {
if (A[nodenum][v] == TRUE) /*dad detected*/
{
if (nodenum == u) {
return TRUE; //complete
} else if (path (A, u, nodenum)) {
return TRUE; //maybe dad is a node that we are looking for (recursion)
}
}
}
return FALSE; /*all parents of v node were cheked and noone of them isnt u node*/
}
Finally, I run this in gdb (ubuntu).
do { /*here user enter the nodes to connect */
printf("Enter the number of first node\n");
scanf("%d", &dadnode);
printf("Enter the number of second node\n");
scanf("%d", &sonnode);
if ((dadnode < sonnode) && (sonnode <= N) && (dadnode > 0)) {/*checking if nodes are legal*/
Matrix[dadnode][sonnode] = 1; /*if legal - connect*/
}
} while ( (dadnode != EOF ) && (sonnode != EOF)); /*until user enter EOF */
Why when I trying to stop this loop (from main function) by pressing Ctrl+d, the loop will continue and stop only after a pair of numbers are found where one of the numbers is -1 ?
Ok, enter "-1" and then the main function should calls the path() function to check if node a and node b are connected. If they are, then it should output a message according the result of path(Matrix,dadnode,sonnode).
However, instead of this behaviour I get the message "Program exited normally." Why do I get this message?
Does main function even call the path() function? I'm not sure what the error in my code is...
Upvotes: 0
Views: 296
Reputation: 3225
EOF
is defined as (-1
) in stdio.h
, however when you use Ctrl + D to send an EOF
message, you're sending a different character value (4
). The EOF
define of (-1
) is meant to be the return value of functions that fail due to an end-of-file or other error. So instead of comparing your input value (dadnode
or sonnode
) to EOF
, you should compare the return value of scanf()
to EOF
.
The return value of scanf()
is the number of items read (in your case, it should only be 1
), or EOF
if the user sends Ctrl + D (Windows users will have to send Ctrl + Z).
Example:
int dadnode, sonnode;
int result;
while (true)
{
result = scanf("%d", &dadnode);
if (result < 1) break;
result = scanf("%d", &sonnode);
if (result < 1) break;
}
Upvotes: 2