Monday, March 30, 2009

ecp1026 tut 7

GRAPH

#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>>");

//Obtain the origin s place name from the user and find index
//associated with the place name

scanf("%c", &origin);

for(from = 0; from < NUMPLACE; from++)
if(places[from]==origin) break;

getchar();

printf("To>>");

//Obtain the destination s place name from the user and find index
//associated with the place name

scanf("%c", &dest);

for(to = 0; to < NUMPLACE; to++)
if(places[to] == dest) break;

printf("From %c to %c ", places[from],places[to]);

//Obtain the distance from the adjacency matrix.
//If there s no path, then display No direct road.

if((roads[from][to])!=NOP)
printf("it is %2.1f km\n", roads[from][to]);
else
printf(": no direct road. \n");


return(0);
}

SET

typedef unsigned int Set;

#define SETSIZE 8

void printSet(Set s)
{
int j;
printf("{ ");
for (j=0; j< SETSIZE; j++)
if ((1<< j)&s)
printf("%d ",j);
printf("}\n");
}

int main()
{
Set a, b;
a=(1<<1)|(1<<2)|(1<<3)|(1<<4);
b=(1<<4)|(1<<5)|(1<<6);

printf("a= ");
printSet(a);

printf("b= ");
printSet(b);

printf("a union b = ");
printSet(a|b);

printf("a intersection b = ");
printSet(a&b);

printf("inset 5 into a.\n");
a = (1<<5)|a;

printf("is 3 an element of b?\n");
if ((1<<3)&b)
printf("yes\n");
else
printf("no\n");

return 0;
}



3 comments:

Eric Ng said...

nice post ever ^

Hau said...

Hey bro jia yu la haha

Anonymous said...

c++? lol