Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

C Program Fraction To Decimal

How to write a C Program Algorithm:
  1. Divide numerator by denominator until remainder = 0
  2. or it makes a loop (a remainder appears twice).
  3. Use array to mark position of a remainder.

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 Bankers Algorithm Example

How to write a C Program to Banker Algorithm Example in C Programming Language ?

The Banker's algorithm, sometimes referred to as the avoidance algorithm, is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes an "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue.
The algorithm was developed in the design process for the THE operating system and originally described (in Dutch) in EWD108 When a new process enters a system, it must declare the maximum number of instances of each resource type that it may ever claim; clearly, that number may not exceed the total number of resources in the system. Also, when a process gets all its requested resources it must return them in a finite amount of time.

Solution:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. int n,m,i,j;
  5. int all[10][10],max [10][10],need[10][10];
  6. int avail[10],work[10],req[10];
  7. struct process
  8. {
  9.  char name[10];
  10.  int flag;
  11. }p[10];
  12.  
  13. void input()
  14. {
  15.  printf("enter the total number of process \n");
  16.  scanf("%d",&n);
  17.  for(i=0;i<n;i++)
  18.  {
  19.   printf("name:");
  20.   scanf("%s",p[i].name);
  21.   }
  22.   printf(" enter the number of resouces \n");
  23.   scanf("%d",&m);
  24.   printf(" enter the allocation matrix \n");
  25.   for(i=0;i<n;++i)
  26.   {
  27.    for(j=0;j<m;++j)
  28.    scanf("%d",&all[i][j]);
  29.   }
  30.   printf(" enter max matrix \n");
  31.   for(i=0;i<n;++i)
  32.   {
  33.    for(j=0;j<m;++j)
  34.    scanf("%d",&max[i][j]);
  35.   }
  36.   printf(" need matrix \n");
  37.   for(i=0;i<n;++i)
  38.    {
  39.     for(j=0;j<m;++j)
  40.     {
  41.      need[i][j]=max[i][j]-all[i][j];
  42.      printf(" %d ",need[i][j]);
  43.       printf("\n");
  44.      }
  45.    }
  46.      printf("\n");
  47.      printf("enter avaliable resources \n");
  48.      for(i=0;i<m;i++)
  49.       scanf("%d",&avail[i]);
  50. }
  51. void safeseq()
  52. {
  53.   int sseq[10],ss=0,chk=0,chki=0,count=0;
  54.   for(j=0;j<m;++j)
  55.    work[j]=avail[j];
  56.    for(i=0;i<n;++i)
  57.     p[i].flag=0;
  58.     while(count!=5)
  59.     {
  60.      for(i=0;i<n;++i)
  61.      {
  62.       chk=0;
  63.       for(j=0;j<m;++j)
  64.       {
  65.        if(p[i].flag==0)
  66.        {
  67.         if(need[i][j]<=work[j]);
  68.         chk++;
  69.         }
  70.         if(chk==m)
  71.         {
  72.          for(j=0;j<m;j++)
  73.          {
  74.           work[j]=work[j]+all[i][j];
  75.           printf("%d",work[j]);
  76.           p[i].flag=1;
  77.          }
  78.         sseq[ss]=1;
  79.         ss++;
  80.         count++;
  81.       }
  82.      }
  83.     }
  84.    }
  85.   for(i=0;i<n;++i)
  86.   {
  87.    if(p[i].flag==1)
  88.    chki++;
  89.    }
  90.    if(chki>=n)
  91.    {
  92.     printf("system is in safe state \n");
  93.     for(i=0;i<n;++i)
  94.      printf(" %d ",sseq[i]);
  95.     }
  96.     else
  97.     printf("not safe \n");
  98. }
  99. void request()
  100. {
  101.  int pro;
  102.  printf(" enter the process number of request \n");
  103.  scanf("%d",&pro);
  104.  printf(" enetr the request array \n");
  105.  for(i=0;i<m;++i);
  106.  scanf("%d",&req[i]);  
  107.  for(j=0;j<m;j++)
  108.  {
  109.   if(req[j]<=need[pro][j])
  110.   {
  111.    if(req[j]<=avail[j])
  112.    {
  113.     avail[j]=avail[j]-req[j];
  114.     all[pro][j]=all[pro][j]+req[j];
  115.     need[pro][j]=need[pro][j]-req[j];
  116.     printf(" avail: %d",avail[j]);
  117.    }
  118.   printf("\t need : %d",need[pro][j]);
  119.  }
  120.   else
  121.   {
  122.    printf("process has some kirik \n");
  123.    exit(0);
  124.   }
  125.  }
  126. }
  127. void print()
  128. {
  129.  printf("max matrix \n");
  130.   for(i=0;i<n;++i)
  131.   {
  132.    for(j=0;j<m;++j)
  133.    printf(" %d ",max[i][j]);
  134.     printf("\n");
  135.    }
  136.    printf("allocation matrix \n");
  137.   for(i=0;i<n;++i)
  138.   {
  139.    for(j=0;j<m;++j)
  140.    printf(" %d ",all[i][j]);
  141.     printf("\n");
  142.    }
  143.    printf("need matrix \n");
  144.   for(i=0;i<n;++i)
  145.   {
  146.    for(j=0;j<m;++j)
  147.    printf(" %d ",need[i][j]);
  148.     printf("\n");
  149.    }
  150.   printf(\n availiable ");
  151.   for(i=0;i<m;++i)
  152.   printf(" %d ",avail[i]);
  153. }
  154. int main()
  155. {
  156.  int ch;
  157.  do{
  158.  printf("\n menu");
  159.  printf(" 1.input ");
  160.  printf("\n 2.safe seq ");
  161.  printf(\n 3. request ");
  162.  printf("\n 4.print");
  163.  printf("\n 5.exit");
  164.  printf("\n enter choice ");
  165.  scanf("%d",&ch);
  166.  switch(ch)
  167.  {
  168.   case 1: input();
  169.    break;
  170.   case 2: safeseq();
  171.    break;
  172.    case 3: request();
  173.    break;
  174.    case 4: print();
  175.     break;
  176.     case 5:break;
  177.   }
  178.   }while(ch!=5);
  179. return 0;
  180. }