BOMB DIFFUSION PROGRAM IN C LANGUAGE
Introduction:
In this tutorial, we brief about the bomb diffusion program in C language.
Program 1:
In order to stop bomb diffusion, one needs to use various wires. The following colors are available for these cables: white, purple, green, black, red, and orange.
Let us consider in order to diffuse the bomb, one must follow the following rules. Otherwise, bomb will explode.
- One cannot cut another white or black cable if he cuts a white cable.
- You must cut a green cable after cutting a red cable.
- You are not allowed to cut a white, orange, or green cable after cutting a black cable.
- You need to cut a black or red cable after cutting an orange cable.
- You must cut a white or orange cable after cutting a green cable.
- You cannot cut a purple, orange, green, or white cable once he cuts a purple cable.
- The bomb explodes if you cut anything in the incorrect sequence.
Then, the program which satisfies all the above conditions is:
#include <stdio.h>
#include <string.h>
int main()
{
int num;
scanf("%d", &num);
char array[500][15];
for (int i = 0; i < num; i++)
{
scanf("%s", array[i]);
}
int j = 0;
for (int i = 0; i < num - 1; i++)
{
if (strcmp(array[i], "white") == 0 && (strcmp(array[i + 1], "white") == 0 || strcmp(array[i + 1], "black") == 0))
{
j = 1;
}
else if (strcmp(array[i], "red") == 0 && strcmp(array[i + 1], "green") != 0)
{
j = 1;
}
else if (strcmp(array[i], "black") == 0 && (strcmp(array[i + 1], "white") == 0 || strcmp(array[i + 1], "green") == 0 || strcmp(array[i + 1], "orange") == 0))
{
j = 1;
}
else if (strcmp(array[i], "orange") == 0 && (strcmp(array[i + 1], "red") != 0 && strcmp(array[i + 1], "black") != 0))
{
j = 1;
}
else if (strcmp(array[i], "green") == 0 && (strcmp(array[i + 1], "orange") != 0 && strcmp(array[i + 1], "white") != 0))
{
j = 1;
}
else if (strcmp(array[i], "purple") == 0 && (strcmp(array[i + 1], "black") != 0 && strcmp(array[i + 1], "red") != 0))
{
j = 1;
}
}
if (j == 0)
{
printf(" YES \n");
} else
{
printf(" NO \n");
}
return 0;
}
Output:
Program 2:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, value, i, j, k, count, sum, p;
scanf("%d", &num);
scanf("%d", &value);
int array[100];
for(i=0; i<num; i++)
{
scanf("%d", &array[i]);
}
if(value < 0)
{
p = abs(value);
for(i=0; i<num; i++)
{
count = 1; sum = 0;
for(j = i-1; count <= p; j--)
{
if(j < 0)
{
j = num-1;
count++;
k = array[j]+0;
sum = sum+k;
}
else
{
count++;
k = array[j]+0;
sum = sum+k;
}
}
printf("%d ", sum);
}
}
else
{
for(i=0; i<num; i++)
{
count = 1; sum = 0;
for(j = i+1; count <= value; j++)
{
if(j >= num-1)
{
j = 0;
count++;
k = array[j]+0;
sum = sum+k;
}
else
{
count++;
k = array[j]+0;
sum = sum+k;
}
}
printf("%d ", sum);
}
}
}
Output:
Explanation:
num: The total number of elements in the array.
Value: An integer denoting the "diffusion value," or the number of components the bomb diffuses away from.
k: Temporary variable to store array elements.
Count: Count to the diffusion distance.
Sum: An accumulator for the total diffusion paths.
P: When the value is negative absolute value is used.
array[100]: An array which can store up to 100 elements.
Diffusion calculation:
- The program determines the diffusion backwards if {value} is negative.
- It iterates over every single element of the array using a loop.
- Considering the array to be circular, it takes p steps backwards for every element in it.
- The total of the components in this backward diffusing path is computed.
- Outputs the total for every array point.
- The program computes the diffusion towards the forward direction if value is a positive.
- The total of the components in this forward diffusing path is computed.
- Outputs the total for every array point.
Conclusion:
In this article, we have seen bomb diffusion program in two different methods. Therefore, one can use the any of the method in order to diffuse the bomb.