donmike25 icon

tuto 7 q1

donmike25 | PRO | 01/08/14 07:13:56 AM UTC | 0 ⭐ | 204 👁️ | Never ⏰ | []
C |

1.17 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#define NUMPLACE 3
#define NOP -1.0
 
 
//1-D array to keep track of the place names
char places[NUMPLACE] = { 'A','B','C'};
//2-D array for the adjacency matrix representation
float roads [NUMPLACE][NUMPLACE] = {
 
    {0.0,3.5,NOP},
    {3.2,0.0,2.8},
    {3.1,NOP,0.0}
};
 
 
int main(void){
    int from;
    int to;
    char origin, dest;
    printf("From>>");
    scanf("%c",&origin);
    for(from = 0;from < NUMPLACE ; from++)
    {
        if(places[from]== origin)
        break;
    }
 
 
    //ape function getchar
    //try run without it
 
 
 
 
    //Obtain the origin’s place name from the user and find index
    //associated with the place name
 
    printf("To>>");
    getchar();
    scanf("%c",&dest);
    for(to = 0;to < NUMPLACE ; to++)
    {
        if(places[to]== dest)
        break;
    }
    //Obtain the destination’s place name from the user and find index
    //associated with the place name
 
    printf("From %c to %c ", places[from], places[to]);
 
    if (roads [from][to] == NOP )
        printf("No direct road.\n");
    else 
        printf("%2.1f km \n",roads [from][to]);
    //Obtain the distance from the adjacency matrix.
    //If there’s no path, then display “No direct road.”
 
    return(0);
}

Comments