donmike25

tuto 7 q1

Jan 8th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #define NUMPLACE 3
  3. #define NOP -1.0
  4.  
  5.  
  6. //1-D array to keep track of the place names
  7. char places[NUMPLACE] = { 'A','B','C'};
  8. //2-D array for the adjacency matrix representation
  9. float roads [NUMPLACE][NUMPLACE] = {
  10.  
  11.     {0.0,3.5,NOP},
  12.     {3.2,0.0,2.8},
  13.     {3.1,NOP,0.0}
  14. };
  15.  
  16.  
  17. int main(void){
  18.     int from;
  19.     int to;
  20.     char origin, dest;
  21.     printf("From>>");
  22.     scanf("%c",&origin);
  23.     for(from = 0;from < NUMPLACE ; from++)
  24.     {
  25.         if(places[from]== origin)
  26.         break;
  27.     }
  28.  
  29.  
  30.     //ape function getchar
  31.     //try run without it
  32.  
  33.  
  34.  
  35.  
  36.     //Obtain the origin’s place name from the user and find index
  37.     //associated with the place name
  38.  
  39.     printf("To>>");
  40.     getchar();
  41.     scanf("%c",&dest);
  42.     for(to = 0;to < NUMPLACE ; to++)
  43.     {
  44.         if(places[to]== dest)
  45.         break;
  46.     }
  47.     //Obtain the destination’s place name from the user and find index
  48.     //associated with the place name
  49.  
  50.     printf("From %c to %c ", places[from], places[to]);
  51.  
  52.     if (roads [from][to] == NOP )
  53.         printf("No direct road.\n");
  54.     else
  55.         printf("%2.1f km \n",roads [from][to]);
  56.     //Obtain the distance from the adjacency matrix.
  57.     //If there’s no path, then display “No direct road.”
  58.  
  59.     return(0);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment