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 4

2022

General Submission Instructions

Assignments must be submitted using Canvas.

• Programs must be written in C conforming to the C11 standard. Everything we teach you is compli- ant with the C11 standard. Things we haven’t taught you might not be, so use only the features of C that we have taught. If you try things you nd on other websites (where you should definitely not be looking for solutions) they may not be C11 compliant. Everything you need to solve programming problems can be found in the course materials, or in the assignment itself.

• Include the following identification in a comment at the top of all your code les: your name, NSID, student ID, instructor’s name, course name, and course section number.

• Late assignments are accepted with a penalty depending on how late it is received. Assignments are not accepted more than 2 days late (the assignment submission will close 48 hours after the due date passes and you will not be able to submit). See the course syllabus for the full late assignment and assignment extension policies for this class.

VERY IMPORTANT: Canvas is very fragile when it comes to submitting multiple les.  We insist that you package all of the les for all questions for the entire assignment into a single ZIP archive file.  This can be done with a feature built into the Windows explorer (Windows), or with the zip terminal command (LINUX and Mac), or by selecting les in the Mac nder, right-clicking, and choosing "Compress ...".  We cannot accept any other archive formats other than ZIP les.  This means no tar, no gzip, no 7zip ( .7zip), and no WinRAR ( .rar) files. Non-ZIP archives will not be graded.

• Instructions on "how to create zip archives" can be found here: https://canvas.usask.ca/courses/62306/pages/how-to-zip-slash-compress-your-files

Background

Command Line Arguments

This assignment requires the use of command line arguments to C programs.  To learn about command line arguments, read Appendix C in the textbook before proceeding.  It’s not long, only about 3 pages!. You will use command line arguments in questions 1 and 2.

Dynamically-allocated Pointer Arrays

From textbook chapter 11, we know that we can declare a pointer array of a xed size like this:

< pointer type > varname [ < size >];

where  is a pointer type, such as int* or char*, and  is a positive integer value. Used within in a function, such a declaration would use automatic allocation on the stack for the storage space for the  pointers. For example, an array of 10 pointers to integers would be declared thusly:

and those 10 pointers would be in automatically allocated memory on the stack.  But what if we didn’t know how many integer pointers we needed until runtime? We would have to dynamically allocate such a pointer array with the desired number of pointers.  Let’s suppose we need to allocate an array of n pointers to integers at runtime. It would be done like this:

int ** d y _ i n t _ p t r _ a r r ay =   ( int **) malloc (sizeof ( int *)   * n );

The type of dy_int_ptr_array must be int** because it is a pointer to the rst element of an array whose elements are of type int*. A pointer to something of type int* must therefore have type int** (a pointer to a pointer to an integer). Each element of both arrays int_ptr_array and dy_int_ptr_array is of type int*, but the type of dy_int_ptr_array cannot be an array type because it is dynamically allocated, so we have to use the type that such an array type would decay into, which is int**.

Now, suppose we have a typedef for a structure:

~

A automatically-allocated array of pointers to such structures would be declared thusly (similar to the declaration of int_ptr_array, above):

In Question 1 of this assignment, you’ll need to create a dynamically allocated array of pointers to a structure type.   Given the examples in this section it should not be a big leap to gure out how to dynamically allocate an array of pointers to a structure type, but exactly how to do that is left as a problem for Question 1. Use the example of allocating dy_int_ptr_array, above, as a guide the pattern is the same, you just need to substitute in the right types.

Reminder:  declaring and allocating a pointer array as shown above only creates space to store the pointers in the pointer array.  It does not allocate memory for any data items that the pointers in the pointer array are expected to point to, nor does it initialize the pointers in the pointer array to point to anything valid. These things need to be done after dynamically allocating the pointer array.

Question 1 (18 points):

Purpose: To practice with structures and pointer arrays.

For this question you will read in the contents of a datafile, storing the entire contents of the le at once in a array of pointers to structures (a pointer array where each element is a pointer to a structure type).

Data File Format

Data les will contain information about loot items in a hypothetical game, formatted thusly:

• The rst line of the data le contains a positive integer N that is the number of loot items that the file contains data for.

• The remainder of the le consists of N lines. Each line contains the data items for one loot item in the following order:

the name of the item consisting of not more than 30 characters that contains no spaces;

the item level of the item (positive integer);

the value of the item (positive integer);

the rarity of the item (a string of not more than 9 characters that contains no spaces).  This can be one of four possible strings: Common, Uncommon, Rare, or Legendary;

the durability of the item (positive integer); and

the probability of the item being awarded from a loot chest (a real number not less than 0.0 and not more than 1.0).

The data items on one line of the le may be separated by any amount of non-newline whitespace.

• The total length of any line in the le, including all whitespace and newline characters, is not more than 80 characters.

A sample input le, loot .txt, that conforms to the data le format above has been provided.

Program Requirements

Write a program that accepts exactly one command line argument: a lename.  Your program must read in the data from the datafile (in the format described above), ask the user to enter a rarity level, and then display a list of the loot items and their properties that have the selected rarity.

Your program must work with any input data le that is in the format described above. Use exclusively the file I/O paradigm for reading tabular les as described in Appendix B of the textbook (even for the first line of the le!).

Item names in the datafile may contain underscores as placeholders for spaces within the item name, as seen in previous assignments. After reading the item names and storing them, replace the underscores with spaces using the underscores_to_spaces() function provided with Assignment 3, Question 3.

Your program must be readable.   We will be assessing the readability and aesthetic presentation of this program.   Your program must use a consistent whitespacing style.   This includes spacing around and between operators and operands within lines, and blank-line spacing between lines of code and comment blocks. Use previous instructor solutions to assignments as a guide. Usually one space around/between operators and operands is preferred.  Indentation style should be consistent throughout the code. One of the curly brace styles described in Section 1.3 of the textbook should be used consistently throughout the program code.We have provided you with starter code in asn4q1-starter .c that contains comments that describe in greater detail how to complete the program. Wherever you see a comment that starts with “TODO“, you need to do something!

When using C standard library functions, employ error checks as established in the textbook even where not explicitly asked for in the TODO comments in the starter code.

Note: As provided, asn4q1-starter .c will not compile without error. You will need to complete it to a certain extent before it will compile.

Sample Output

Here we show the expected output of several runs under different conditions. In all cases, lines shown in green are not program output but are the commands executed to run the program. Red text shows text entered by the user during the program’s execution.

~


~


$ ./asn4q1 loot .txt

Successfully read 15 loot items from loot . txt .

List loot of what rarity ?   ( Common , Uncommon , Rare , Legendary ): Legendary Pervon s Finger Bone 20   500000 Legendary 27   0 . 000010

Spatula of Justice 19   340123 Legendary 38   0 . 000098

Censer of Tarrasque Summoning 17   42000 Legendary 29   0 . 000240


$ ./asn4q1 loot .txt

Successfully read 15 loot items from loot . txt .

List loot of what rarity ?   ( Common , Uncommon , Rare , Legendary ): Uncommon

Dust of Deliciousness 7   600 Uncommon 1   0 . 150000

Scroll of Conjure Milk 3   125 Uncommon 1   0 . 100000

Hideous Halberd 8   2424 Uncommon 97   0 . 023000

The Shortest Bow 6   1989 Uncommon 18   0 . 007600

Scroll of Fog of Enlightenment 8   250 Uncommon 1   0 . 075000