Showing posts with label Distance. Show all posts
Showing posts with label Distance. Show all posts

How to write a C Program Doom ACS distance in C Programming Language ?

How to write a C Program Doom ACS distance in C Programming Language ?


Solution:

  1. function int abs(int input)
  2.   {
  3.     if (input < 0) { return -input; }
  4.     return input;
  5.   }
  6.  
  7.   function int fdistance(int tid1, int tid2)
  8.   {
  9.     int len;
  10.     int y = getactory(tid1) - getactory(tid2);
  11.     int x = getactorx(tid1) - getactorx(tid2);
  12.     int z = getactorz(tid1) - getactorz(tid2);
  13.  
  14.     int ang = vectorangle(x,y);
  15.     if (((ang+0.125)%0.5) > 0.25) len = fixeddiv(y, sin(ang));
  16.     else len = fixeddiv(x, cos(ang));
  17.  
  18.     ang = vectorangle(len, z);
  19.     if (((ang+0.125)%0.5) > 0.25) len = fixeddiv(z, sin(ang));
  20.     else len = fixeddiv(len, cos(ang));
  21.  
  22.     return len;
  23.   }
  24.  
  25.  
  26. // distance between player and monster tid.
  27. int owner_dist = fdistance(0, mtid) >> 16;

C Program for distance vector algorithm to find suitable path for transmission

How to Write a C Program for distance vector algorithm to find suitable path for transmission in C Programming Language ?


Solution:

 
  1. #include<stdlib.h>
  2. #define nul 1000
  3. #define nodes 10
  4. int no;
  5. struct node
  6. {
  7. int a[nodes][4];
  8. }router[nodes];
  9. void init(int r)
  10. {
  11. int i;
  12. for(i=1;i<=no;i++)
  13. {
  14. router[r].a[i][1]=i;
  15. router[r].a[i][2]=999;
  16. router[r].a[i][3]=nul;
  17. }
  18. router[r].a[r][2]=0;
  19. router[r].a[r][3]=r;
  20. }
  21. void inp(int r)
  22. {
  23. int i;
  24. printf("\nEnter dist from the node %d to other nodes",r);
  25. printf("\nPls enter 999 if there is no direct route\n",r);
  26. for(i=1;i<=no;i++)
  27. {
  28. if(i!=r)
  29. {
  30. printf("\nEnter dist to the node %d:",i);
  31. scanf("%d",&router[r].a[i][2]);
  32. router[r].a[i][3]=i;
  33. }
  34. }
  35. }
  36. void display(int r)
  37. {
  38. int i,j;
  39. printf("\n\nThe routing table for node %d is as follows:",r);
  40. for(i=1;i<=no;i++)
  41. {
  42. if(router[r].a[i][2]>=999)
  43. printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);
  44. else
  45. printf("\n\t\t\t %d \t %d \t\t d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
  46. }
  47. }
  48. void dv_algo(int r)
  49. {
  50. int i,j,z;
  51. for(i=1;i<=no;i++)
  52. {
  53. if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)
  54. {
  55. for(j=1;j<=no;j++)
  56. {
  57. z=router[r].a[i][2]+router[i].a[j][2];
  58. if(router[r].a[j][2]>z)
  59. {
  60. router[r].a[j][2]=z;
  61. router[r].a[j][3]=i;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. int main()
  68. {
  69. int i,j,x,y;
  70. char choice;
  71. printf("Enter the no. of nodes required (less than 10 pls):");
  72. scanf("%d",&no);
  73. for(i=1;i<=no;i++)
  74. {
  75. init(i);
  76. inp(i);
  77. }
  78. printf("\nThe configuration of the nodes after initialization is as follows:");
  79. for(i=1;i<=no;i++)
  80. display(i);
  81. for(i=1;i<=no;i++)
  82. dv_algo(i);
  83. printf("\nThe configuration of the nodes after computation of paths is as follows:");
  84. for(i=1;i<=no;i++)
  85. display(i);
  86. while(1)
  87. {
  88. printf("\n\nWanna continue (y/n):");
  89. scanf("%c",&choice);
  90. if(choice=='n')
  91. break;
  92. printf("\nEnter the nodes btn which shortest path is to be found:\n");
  93. scanf("%d %d",&x,&y);
  94. printf("\nThe length of the shortest path is %d",router[x].a[y][2]);
  95. }
  96. }

Output C Program for distance vector algorithm to find suitable path for transmission



[root@localhost ~]# ./a.out
Enter the no. of nodes required (less than 10 pls):4
Enter dist from the node 1 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 2:5
Enter dist to the node 3:3
Enter dist to the node 4:7
Enter dist from the node 2 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:5
Enter dist to the node 3:999
Enter dist to the node 4:6
Enter dist from the node 3 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:3\
Enter dist to the node 2:
Enter dist to the node 4:
Enter dist from the node 4 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:
Enter dist to the node 2:
Enter dist to the node 3:
The configuration of the nodes after initialization is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 no link no hop
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 no link no hop
3 0 3
4 no link no hop
The routing table for node 4 is as follows:
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 8 1
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 8 1
3 0 3
4 10 1
The routing table for node 4 is as follows:
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
^C
[root@localhost ~]# clear
[root@localhost ~]# ./a.out
Enter the no. of nodes required (less than 10 pls):4
Enter dist from the node 1 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 2:5
Enter dist to the node 3:3
Enter dist to the node 4:7
Enter dist from the node 2 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:5
Enter dist to the node 3:999
Enter dist to the node 4:6
Enter dist from the node 3 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:3
Enter dist to the node 2:999
Enter dist to the node 4:2
Enter dist from the node 4 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:7
Enter dist to the node 2:6
Enter dist to the node 3:2
The configuration of the nodes after initialization is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 no link no hop
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 no link no hop
3 0 3
4 2 4
The routing table for node 4 is as follows:
1 7 1
2 6 2
3 2 3
4 0 4
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 5 3
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 8 1
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 8 1
3 0 3
4 2 4
The routing table for node 4 is as follows:
1 5 3
2 6 2
3 2 3
4 0 4
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
1 4
The length of the shortest path is 7
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found-

C Program to Distance Conversion Program

How to write a C Program to Distance Conversion Program in C Programming Language ?


Solution:
This Program converts the value entered in Km to inches, centimetres, feet and metres.

The distance between two cities (in km.) is input through the keyboard.

Write a program to convert and print this distance in meters, feet, inches and centimetres.


/*
Distance Conversion:
1Km = 39370 inches
1Km = 3281 Feet
1Km = 100000 Cm
1Km = 1000 metres
*/

  1. #include<stdio.h>
  2.  
  3. main()
  4. {
  5. float km,m,cm,inch,foot;
  6.  
  7. printf("Enter the distance between the two cities in Km: ");
  8. scanf("%f", &km);
  9.  
  10. inch = 39370*km;
  11. foot = 3281*km;
  12.  
  13. cm= 100000*km;
  14. = 1000*km;
  15.  
  16. printf ("\nDistance converted to inches: %f", inch);
  17. printf ("\nDistance converted to feet: %f", foot);
  18.  
  19. printf ("\nDistance converted to centimeters: %f", cm);
  20. printf ("\nDistance converted to meters: %f", m);
  21.  
  22. }