Friday, February 19, 2010

Me Left Leg Wont Stop Twitching

Project 1: # 2 Find a good route

Objective: Find a good route from one place to another on a map.

couple of project: Alan http://twowolvescorp.blogspot.com/

This is the pseudocode:


# include \u0026lt;stdio.h>


main ()
{

# define if 1, not 0;

/ * Declare some variables, i, j and k are indices to assess repetitions .* /

double ndir , i, j, k;

/ * These are matrices that contain words .* /

addresses char [] [] paths [];

/ * And here are some precision numbers .* /

long float distances [] [], short, minor menoralt [];

printf ("This program calculates the distance over short of one or more paths \\ nthat can travel on a map, from an initial direction to a final. ")

printf ("How many addresses are?")
scanf ("% ld", & ndir);

/ * is defined the first nxn matrix .* /
directions [ndir] [ndir]

/ * Now we are filling the spaces in the front row of the array .* /

while (i \u0026lt;= ndir)
{
printf ("Address% d?", I);
scanf ("% s", & addresses [i] [1]);
i + +;
}

printf ("Assign the connection or link between the addresses:")

/ * We continue to fill the matrix but completely now, every road connections are written immediately below the row in each column .* /

for (i = 1; i \u0026lt;= ndir; i + +)
{
printf ("Address% s, connected with," addresses [ i] [1]);

for (j = 2 j \u0026lt;= ndir; j + +)
{

/ * This while creating a list of connections you can choose for each direction .* /

while (i \u0026lt;= ndir)
{
printf(“\n%ld. %s”, i, direcciones[i][1]);
i++;
}
i-=i+1;

printf(“la dirección(numero): ”);
scanf(“%ld”, &opcion);

if (opcion=1)
{

/ * It would go the same direction, no sense .* /

printf ("Invalid option, choose another ...");
}

/ * The switch matrix is \u200b\u200bfilled according to the number selected in its corresponding position .* /

switch (option)
{
case 2:
direcciones[i][j]= direcciones[i][1] ;
break;

case 3:
direcciones[i][j+1]= direcciones[i][1] ;
break;

continue case ndir:
direcciones[i][j+2]=direcciones[i][1];
break;
}

printf ("It is also connected to another address?");
scanf ("% s", & choice);

if (choice == 1)
{
j + +

/ * Return to the line 53 to continue filling the matrix in the next column .* /

return line 53;
}
/ * This ensures not to repeat questions from unions directions .* /
j + +;
}

}

printf ("Assign the distances between addresses:");

/ * nxn matrix is \u200b\u200bdefined for other distances (float number) .* /

distances [ndir +1] [ndir +1];

for ((i = 1; i \u0026lt;= ndir; i ++)&&( j = 2 j \u0026lt;= ndir; j + +))
{

/ * says that if there is something definite in position [i] [j] of matrix "addresses" assigned the number 1 in the matrix of "distances" in the equivalent position .* /

if (exists (address [i] [j]) == 1)
{
distances [i] [j] = 1;
}
}

for (i = 1; i \u0026lt;= ndir; i + +)
{

for (j = 2 j \u0026lt;= ndir; j + +)
{

/ * And then he says that if there is a 1 in the positions of the array "distance", I asked to enter the distance .* /

if (exists (addresses [i] [j]) == 1)
{
printf ("Which is the distance% s-% s?", addresses [i] [1], addresses [i] [j])
scanf ("% lf", distance [i] [j]);
}
/ * This (j + +) is to not repeat the questions of equal distances but in different directions. Ie distance = AB = BA * /

j + +
}

}

printf ("What is the starting address?")
scanf ("% s" , & start);

printf ("And what is the final direction or destination?")
scanf ("% s", & fin);

/ * This is the part that is a bit confusing because we do not know how to make tree diagrams .* /

generate diagram (paths [])

/ * Here we just say we generate k-paths possible from the array of addresses and to be adding the distances .* /

for (k = 1, k \u0026lt;= distances [i] [j], k + +)
{
for (j = 2 j \u0026lt;= tam (addresses [i] [j]))
{

/ * Find addresses adjacent or unions .* /

search adjacent (Address [])
sum = distances [i] [ j] + distances [i] [j + +]
}
paths [k] = sum;
sum = 0;
}

/ * We take the first number of paths generated and compared with all others will be assigned to the variable "child" to the lower number of each comparison made. This is done because there are times that are 500 different ways .* /

k = 1;
minor = path [k];

for
(k = 2, k \u0026lt;= size (paths []) k + +)
{
if (minor> paths [k])
{
minor = path [k];
}
else if (minor = path [k])
{

/ * Menoralt means less alternative, and sometimes you can find 2 or more roads with the same distance .* /

menoralt [i] = paths [k];
}

}

printf ("The shortest path that can take % S% s:%-s with a distance of:% lf ", start, end, paths [k], child);

/ * Compare the results and offered alternative screen only if they are equal to the shortest path (lowest) .* /

j = size (menoralt []);
if (menoralt [j] = lower)
{
for (i \u0026lt;= j, i = 1, i--)
{
while (child = menoralt [i])
{
printf ("Another alternative is:%-s with the same distance, road [i]);
}
}
}

getch ();
}
An example:


This is an example of the algorithm with a simple map:

  1. Take a map either, in this case is the state of Arizona, USA.


  1. Suppose you want to go to Holbrook to Blythe, just on the roads highlighted in blue and underlined in red cities.

  1. Program start asking the number of cities or addresses that are: 11

Blythe, Needles, Kingman, Williams, Flagstaff, Sedona, Prescott , Wickenburg, Phoenix , Globe and Holbrook.

  1. Then define a 11x11 matrix.

  1. Fill in the blanks in the first row as van entering the data.

  1. asks you what are the connections or joints the first direction (Blythe), the map can see that are Needles, Wickenburg and Phoenix. And the program the inserted just below the first city

i



i = 1
i = 2
= 3
i = 4
i = 5
j = 1
Blythe
Needles
Kingman
Williams
j=2
Needles
Blythe
j=3
Wickenburg
j=4
Phoenix
...
...
...

  1. And does the same with other cities without repeat the questions.

  1. matrix is \u200b\u200bdefined for the distance but it is a number larger than the total number of addresses. Ie 12x12.

  1. If any value is numeric or alphabetic address in the matrix, is assigned a 1 to its counterpart in the distance matrix. Ie distances [i +1] [j +1].




Blythe
Needles
Kingman
Williams
bythe




Needles
1



Kingman



Phoenix
1
...
...


  1. Then, if there is a 1 in the matrix above, prompt the distance [i] [1] [i] [j].

  1. start address are requested and the final address: Holbrook and Blythe.

  1. Start making a tree diagram from both parent and assigning the results in an array called paths [k].



Here you can appreciate the complexity of the map with all possible ways.

    Later
  1. have generated all paths [k], compare the distances obtained, ie each of the branches of the tree diagram that end in Blythe, our goal.

  1. Almost finally determined which was the lowest of all distances and is offered as a final result. For practical purposes we assume that the shortest path is the one with fewer addresses involved. You can see from the diagram that these roads are:

Holbrook> Globe > Phoenix> Blythe.

  1. I finally are some alternatives:
Another option would be:
Holbrook> Flagstaff > Williams> Prescott > Wickenburg> bythe.
The complexity of the program is about NP since the tree diagram increases exponentially as the roads or connections between cities increases.








Friday, February 5, 2010

Cheaters Korean Movie Online Watch

Exercise 2 - integer to binary

Here is my algorithm in C to convert an integer to a binary number:

# include \u0026lt;stdio.h>
# include \u0026lt;math.h>
# include \u0026lt;stdlib.h>

main () {

int num1, i = 0, choice;
double base = pow (2, i);

printf ("Enter an integer to convert Binary: ");
scanf ("% d ", & num1);

while (base<=num1)
    {
          printf(" %lf", base);
          i++;
          base= pow(2,i);
    }
    i--;
    printf("\n i=%d\n\n", i);
    base= pow(2,i);
   
    while (num1>0)
    {
    if (base
    {
                  num1-=base;
                  i--;
                  base= pow(2,i);
                  printf("1");
    }
    else if (base>num1)
    {
        i--;
        base= pow(2,i);
        printf("0");
    }
    else if (base=num1)
    {
    num1-=base;
    printf("1");
    while (i>0)
    {
          printf("0");
          i--;
    }
    }
    }

    printf("\n\n.");
    system("pause");
    return 0;}