Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CMPT 214– Programming Principles and Practice

Assignment 1

2022

Question 1 (5 points):

Purpose: To practice console I/O and use of variables.

Write a short story that uses three pieces of numeric data.  The story should be entirely your own. There is no reason why two students should hand in the same story.

Now pretend that the pieces of numeric data in your story are "blanks" to be lled in by someone else. Write a program that prompts the user to enter the three pieces of numeric data from the console. Print the story to the console with the numeric data read from the console substituted into the appropriate

"blanks" in your story.

Sample input and output:

                                                                                                                                                                                                                                                                                                                                                                                            

Enter   an   integer :  3

Enter   a   real   number :  2.5

Enter   an   integer :  9

A   young   hobbit   was   walking   in   the   woods   about   3  miles   from   home .

After   collecting   2 . 5   baskets   of   tasty   mushrooms   they   were   beset       upon   by   9   restless   spirits .     The   hobbit   took   a  bite   of   a  mushroom  and   offered   some   to   the   spirits .     The   spirits ,   their   hunger   sated , left   the   hobbit   in   peace .

Note: the green text in the sample output is the input entered by the user.

Testing

Run your program once, with valid input values. Copy the output and paste it into to a text le called q1-output .txt which you will hand in.

Implementation Notes

While you should always strive for good code commenting, commenting will not be assessed for this question.

When reading in numbers using scanf(), your program must verify that it actually read a value successfully. Your program must terminate with an appropriate exit code using errx() if it does not successfully read a value when one is expected. However, if a value is successfully read, you do not have to check whether the value read is valid (e.g. if the program asked for a positive value, you don’t need to verify that it is positive).

If your program runs to completion without encountering any errors, it must return an exit code indicating this.

Question 2 (8 points):

Purpose: To practice use of variables and operators.

In video games, a common trope is that you defeat monsters for experience points.  Let’s suppose that in some imaginary video game, the player is the hero and they have to defeat monsters to gain experience points to gain levels. In this hypothetical game, the amount of experience that a hero gains for defeating a monster depends on:

 the monsters level; and

 the heros level.

Let m be the monster’s level, and let h be the hero’s level.  The base amount of experience, xpbase, awarded for defeating the monster is:

xpbase = 100 2.5 × m

The base amount of experience is then adjusted depending on the level difference between the hero and the monster. So the adjusted experience, xpadjusted that is actually awarded to the hero when they defeat the monster is:

xpadjusted  = xpbase  × 1.2(m h)

Write a C program that does the following:

1. Print a prompt message to the console asking the user to enter the monster’s level, and then read in an integer from the console, storing it in a variable. This is the monster’s level.

2. Print a prompt message to the console asking the user to enter the hero’s level, and then read in an integer from the console, storing it in a variable. This is the hero’s level.

3. Compute xpbase for a monster of the level entered by the user.

4.  Compute xpadjusted  for the hero and monster levels entered by the user.

5. Print to the console a series of messages that report back to the user the following items:

 The monsters level that was entered in step 1.

 The heros level that was entered in step 2.

 The calculated xpbase from step 3.

 The calculated xpadjusted from step 4.

Testing

Run your program at least twice with different valid values input by the user each time that are different from the sample output, below. Copy the output from all runs and paste it into to a text le called q2-output .txt. You’ll hand this le in with your code.

Implementation Notes

Your program must terminate with an appropriate error code if a call to scanf() fails to read a value. Use the errx() library function.

If scanf() successfully reads a value, your program may assume that the hero and monster levels entered by the user are between 1 and 50. Your program doesn’t have to verify this, and does not have to work correctly for values outside of that range. We can do better than this dangerous assumption, but we are saving that for question 3!

Experience point values should be stored and reported as integers. If at any time you need to convert from a oating-point value to an integer, truncation is acceptable.  It is not required to round to the

nearest integer, e.g. both 42.8 and 42.2 may be truncated to the integer 42 (this is actually much easier than rounding!).

Remember that to perform the exponentiation operations in the formula, you need the pow() function from the math library. In case you forgot, we remind you that Section 5.2.3 of the textbook explains how to use math library functions, and includes a specific example of how to use the pow() function. Don’t forget to include the -lm option when compiling a program that uses the math library (see textbook, bottom of page 59!).

If your program completes successfully, it must terminate with an appropriate exit code.

Sample Output

Here is an example of how your program’s output should look. It doesn’t need to be identical, but all the same information should be present. While the output of one run should resemble the sample output, you should perform more rigorous testing than what is shown here.

                                                                                                                                                            

What   is   the  monster s   level ?     12

What   is   the  hero s   level ?     7

The   monster   is   level   12 .

The   hero   is   level   7 .

The  monster s  base   XP   value   is   130 .

The  monster s   adjusted   XP   is   323 .


Note: the green text in the sample output is the input entered by the user.

Question 3 (14 points):

Purpose: Practice with variables, operators, conditionals, loops, and functions.

This question follows directly from Question 2. Complete question 2 before attempting this question.

For this question we are going to modify our solution to question 2 so that it is more robust to incorrect user input, and supports some additional special monster types. We are going to improve our program so that it can calculate experience point value of not just normal monsters, but also elite monsters and boss monsters.

We will represent monster type as an integer between 1 and 3. The different types of monsters have different formulae for calculating their base experience value.  The formula for normal monsters is as before in question 2, while the formulae for elite and boss monsters is different.  The following table summarizes the integer value that represents each monster type, and the corresponding base experience point formulae (where m represents the monster’s level between 1 and 50 as before).

Monster Type

Integer Representation

xpbase Formula

Normal

1

100 2.5 × m

Elite

2

100 m1.2

Boss

3

100 m1.9

Starting with your solution to Question 2, modify it to do the following:

1. Write a function that accepts two parameters:  an integer representing the monster’s level, and an integer representing the monster’s type. The function should use the parameters to calculate and return xpbase for the given monster type and level. This function may assume that the values passed to the parameters are valid. Us the formulae for xpbase in the table above.

2. Write a function that accepts three parameters: the base experience value of a monster, the level of that monster, and the level of the hero. The function should use the parameters to calculate and return xpadjusted, the formula for which is the same as in question 2. This function may assume that the values passed to the parameters are valid.

3. Modify the main program so that when it successfully reads the monster level, if the user entered an invalid value that is not between 1 and 50 (inclusive), it displays an error message, and asks to enter the value again. This process should repeat until the user enters a valid value.

4. Do the same thing for the input of the hero level.

5. Add code that will prompt the user to enter an integer between 1 and 3 indicating the monster’s type. If the user does not enter a valid value of 1, 2, or 3 print an error and ask again, repeating this process until the user enters a valid value.

6. Modify the main program so that it uses the functions you wrote in steps 1 and 2 to calculate xpbase and xpadjusted instead of computing them directly.

7. Thoroughly comment your code.  Use inline comments where appropriate to describe the pur- pose of small groups of lines that perform specific tasks in both the main program and the two functions from steps 1 and 2. Place a block comment before each of these functions to describe them. Each such comment block should include:

  a one-sentence description of the functions main purpose;

  a brief description of the purpose and expected range of values of each parameter; and

  a description of the functions return value.

Testing

Run your program three times with different valid values input by the user and different from the sample output, below. Each run should input a different monster type, that is, one run should be for a normal monster, one for an elite monster, and one for a boss monster.

Run your program a fourth time, but include invalid integer inputs to demonstrate that your code recovers from such user errors.  Copy the output from all runs and paste it into to a text le called q3-output .txt. You’ll hand this le in with your code.

You do not have to test your program with non-numeric inputs or oating point numbers, but if you wish you may include additional test runs which show your program terminating abnormally under such circumstances.

Implementation Notes

Your program must terminate with an appropriate error code if a call to scanf() fails to read a value. Use the errx() library function.

Again youll need the pow() function from the math library. Dont forget the -lm when compiling!

The program must report the same results as in question 2, that is, the hero and monster levels, and the base and adjusted experience point values for the monster described by the inputs.

Sample Output

Here is an example of how your program’s output should look. It doesn’t need to be identical, but all the same information should be present. While the output of one run should resemble the sample output, you should perform more rigorous testing than what is shown here.

                                                                                                                                                              What   is   the  monster s   level ?     90

Error :  monster   level   must   be   between   1   and   50 .

What   is   the  monster s   level ?     0

Error :  monster   level   must   be   between   1   and   50 .

What   is   the  monster s   level ?     5

What   is   the  monster s  type   (1= normal ,   2= elite ,   3= boss )?     4       Error :  monster   type  must   be   1   ( normal )  2   ( elite )   or   3   ( boss ) . What   is   the  monster s  type   (1= normal ,   2= elite ,   3= boss )?     2

What   is   the  hero s   level ?     100

Error :  hero   level   must   be   between   1   and   50 .

What   is   the  hero s   level ?     7

The   monster   is   level   5 .

The   hero   is   level   7 .

The  monster s  base   XP   value   is :   106 .

The  monster s   adjusted   XP   is :   73 .

Note: the green text in the sample output is the input entered by the user.

General Hints

You can expect your programs to go completely wrong when you enter non-numeric inputs. This is not an issue and your programs do not have to recover from such errors (yet). Recovering from non-numeric input to scanf() when numeric input is expected is surprisingly difficult.  This is because C is very old and low-level, thus, the console I/O functions are not particularly robust or clever.  Console I/O with strings in C is notoriously cumbersome, but we’ll get there.

What to Hand In

Hand in a .zip le archive which contains the following les:

asn1q1.c: The code for your solution for question 1.

q1-output.txt The text le containing the output of your tests of question 1.

asn1q2.c: The code for your solution for question 2.

2022-11-03