Question
Write the program in C. Write the body of the report_danger() function. (The rest of the program is correct.) The report_danger() function is passed two
Write the program in C. Write the body of the report_danger() function. (The rest of the program is correct.) The report_danger() function is passed two arguments: an integer with some bits switched on indicating which equipment the player has, and an integer with some bits switched on indicating which attacks the monster might use. The function should print out which attacks the user does not have a defense against. To do this, use bit masking and bit shifting to check the individual bits, and if you find a bit on in the monster that's not on in the equipment, then you print out what it is. For example, these lines: #define BITE 0x02 #define POISON 0x20 #define COBRA ( BITE | POISON )
mean that COBRA has the value 0x22 (in binary, 0010 0010). And these lines: #define ACID 0x08 #define POISON 0x20 #define AMULET ( POISON | ACID )
mean that a player armed only with the AMULET has protection of 0x28 (in binary, 0010 1000). The only bit turned on in the monster which is not turned on in the defenses is hex 0x02 (0000 0010). So in the case of a player with only an amulet threatened by a cobra, you'd print out to be wary of a bite. Here is the correct output: Testing report_danger() function. Test 1: Beware of: bite Test 2: Beware of: Test 3: Beware of: fire Test 4: Beware of: acid bite Test 5: Beware of: Test 6: Beware of: Test 7: Beware of: claw bite fire Test 8: Beware of: claw bite Test 9: Beware of: poison bite Test 10: Beware of: bite
Fire is 0x01, Bite is 0x02, Claw is 0x04, Acid is 0x08, Frost is 0x10, and Poison is 0x20. So 1, 2, 4, 8, 16, and 32 in decimal.
example: #define FIRE (0x02)
rest- #define WOLF (BITE) #define COUGAR (BITE | CLAW) #define TIGER ( BITE | CLAW) #define COBRA (BITE |POISON) #define DRAGON (FIRE | BITE | CLAW) #define YETI (BITE | CLAW | FROST) #define ALIEN (BITE | ACID)
#define NONE (0x00) #define INSULATION (FIRE | FROST) #define ARMOR (BITE | CLAW) #define AMULET (POISON | ACID)
void report_danger(int, int); int equipment, monster;
printf("Test 1: "): equipment = NONE; monster = WOLF; report_danger (equipment, monster);
Then it's the same format for tests 2-10. 2: ARMOR, WOLF. 3: ARMOR, DRAGON. 4: INSULATION, ALIEN. 5: ARMOR, TIGER. 6: ARMOR | AMULET | INSULATION, DRAGON. 7: NONE, DRAGON. 8: INSULATION, DRAGON. 9: INSULATION, COBRA. 10: AMULET, ALIEN.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started