Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help Me!! Read through the code, and tell me what the output of each stage or ex

ID: 3885942 • Letter: H

Question

Help Me!!

Read through the code, and tell me what the output of each stage or explain how the method works for each of part5,6,7?

It is confusing for me the method for part5,part6,part7!

This code is about to defuse the bomb by interning each password for each stage.

The code:

V

V

#include
#include
#include

void explode(const int code);

void readInput(char buffer[], const int bufferSize){
int i;
int c;
for(i=0; i<=bufferSize; ++i){
c = getchar();

/* this checks that you are at the end of the line */
/* Windows encodes an end-of-line as two characters: */
/* Linux just uses */
/* This will accept either version */
if(c == ' '){
c = getchar();
}

if(c == ' '){
break;
}
else if(i buffer[i] = (char)c;
}
}

}

void part1(){
const int bufferSize = 12;
char buffer[bufferSize];
char target[] = "Optimus";
int i;
const int n = strlen(target);

readInput(buffer, bufferSize);

for(i=0; i if(buffer[i] != target[i]){
explode(1);
}
}
}

int part2(){
int i,z;

const int bufferSize = 12;
char buffer[bufferSize];

readInput(buffer, bufferSize);

/* atoi takes a string representation of a number (e.g. "1234") */
/* and converts it to an int representation of the number (e.g. 1234) */
z = atoi(buffer);
if(!z)
explode(20);

z += 22;

if(z != 53)
explode(21);

return z / 2;
}

void part3(){
int i,z;

const int bufferSize = 12;
char buffer[bufferSize];


readInput(buffer, bufferSize);

i=0;
while(i < bufferSize && (buffer[i] == 'H' || buffer[i] == 'w')){
i++;
}

if(i != 4)
explode(70);
}

void part4(const int x){
int i,y,z;
const int bufferSize = 12;
char buffer[bufferSize];

readInput(buffer, bufferSize);

/* atoi takes a string representation of a number (e.g. "1234") */
/* and converts it to an int representation of the number (e.g. 1234) */
z = atoi(buffer);
if(!z)
explode(20);

y = z;
for(i=0; i<50; i++){
y += z * i * y;
}

if(z + 12 != x){
explode(21);
}
}


void part5(char buffer[], const int bufferSize){
const char allowed[] = "a3hUD6Cc8bTzv";
int i, j, x;

for(i=0; i char c;

x = getchar();
if(x == EOF)
explode(10);
else if(x == ' ')
explode(11);

c = (char)x;
for(j=2; j<10; ++j){
if(c == allowed[j]){
buffer[i] = c + 1;
break;
}
}
if(j == 10){
explode(12);
}

}

j = 0;
for(i=1; i if(buffer[i] != buffer[i-1]){
j = 1;
break;
}
}
if(j == 0)
explode(13);


/* Windows encodes an end-of-line as two characters: */
/* Linux just uses */
/* This will accept either version */
x = getchar();
if(x == ' ')
x = getchar();
if(x != ' ')
explode(14);
}


void part6(const char buf1[], const int bufSize){
char buf2[bufSize];
int i, c;

readInput(buf2, bufSize);

for(i=0; i if(buf1[i] != buf2[bufSize - 1 - i])
explode(-44);
}

}

void part7(int *x){
int y = 19;
int *z = &y;
int k;

const int bufSize = 12;
char buffer[bufSize];

*z -= 3;

readInput(buffer, bufSize);

/* atoi takes a string representation of a number (e.g. "1234") */
/* and converts it to an int representation of the number (e.g. 1234) */
k = atoi(buffer);

if(k + *x != y)
explode(70);

}

void theBomb(){
const int bufSize = 10;
char buffer[bufSize];
int result;

printf("Enter the stage 1 password: ");
part1();
printf("Stage 1 defused. ");

printf("Enter the stage 2 password: ");
result = part2();
printf("Stage 2 defused. ");

printf("Enter the stage 3 password: ");
part3();
printf("Stage 3 defused. ");

printf("Enter the stage 4 password: ");
part4(result);
printf("Stage 4 defused. ");

printf("Enter the stage 5 password: ");
part5(buffer, bufSize);
printf("Stage 5 defused. ");

printf("Enter the stage 6 password: ");
part6(buffer, bufSize);
printf("Stage 6 defused. ");


printf("Enter the stage 7 password: ");
part7(&result);
printf("Stage 7 defused. ");

printf("The entire bomb has been defused. ");
}

Explanation / Answer

Code with comments for understanding

/* Method Name: part5()
Description: This method takes buffer[] array and buffersize as input and checks for every character in the allowed character array
case 1: If it is EOF then it is going to explode 10
case 2: If it is newline character then it is going to explode the 11
case 3: If the j value is 10 then it is going to explode 12
case 4: If the j value is 0 then it is going to explode 13
Windows encodes an end-of-line as two characters:
*/
void part5(char buffer[], const int bufferSize){
// character array declaration
const char allowed[] = "a3hUD6Cc8bTzv";
int i, j, x;
//for loop
for(i=0; i char c;
//reads the character from keyboard
x = getchar();
if(x == EOF)
//explode the character
explode(10);
else if(x == ' ')
explode(11);
c = (char)x;
for(j=2; j<10; ++j){
if(c == allowed[j]){
buffer[i] = c + 1;
break;
}
}
//check for if condition
if(j == 10){
explode(12);
}
}
j = 0;
for(i=1; i if(buffer[i] != buffer[i-1]){
j = 1;
break;
}
}
if(j == 0)
explode(13);
//read character from keyboard
x = getchar();
if(x == ' ')
x = getchar();
if(x != ' ')
explode(14);
}

/* Method Name: part6()
Description: This method takes buff1[] array and buffersize as input and it calls readInput() method by sending the buff array and bufferSize
case 1: Based on the condition it explodes 44
*/
void part6(const char buf1[], const int bufSize){
char buf2[bufSize];
int i, c;
//method call
readInput(buf2, bufSize);
//for loop
for(i=0; i if(buf1[i] != buf2[bufSize - 1 - i])
//explode the value
explode(-44);
}
}
/* Method Name: part7()
Description: This method reads the buffer with particular size and then make a call for readInput() method by sending buffer value and its size
atoi takes a string representation of a number (e.g. "1234") and converts it to an int representation of the number (e.g. 1234)
After checking the condition it explodes 70
*/

void part7(int *x){
int y = 19;
//Assign the address of y to the location of z
int *z = &y;
int k;
const int bufSize = 12;
char buffer[bufSize];
//decrement the address value by 3
*z -= 3;
//read input method call
readInput(buffer, bufSize);
k = atoi(buffer);
if(k + *x != y)
explode(70);
}