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

Lab #7

This lab exercise contributes to your lab mark (3.5%).

1. We reserve the right to ask you to explain and demo your lab solutions.

2. You need to declare that you wrote the code all by yourself at the submission webpage.

3. We use the lab machines in CSC 159 for all marking purposes; make sure that your C programs can be compiled using the default options  -Wall -std=c99 (no warning  messages, or any notes) and run on these machines.

4. Download and extract the supplied  lab07.tar file, which contains

lab07_case_generator.py and  check , needed for the lab.

5. Pack all of your completed files ( ex7q1.c and optionally  ex7q1.h  if you choose to make a

header file,  check  and other files if any) into  submit.tar by  tar . You should really execute  ./check submit.tar to validate (see testing section) that you can extract required files

successfully and that they work well (all files in the same directory), where  check is a

program we provide for this lab exercise. Please note that  check  does not necessarily check for correctness of your code.

6. Submit your  submit.tar to the designated submission webpage in eClass as early as

possible, and before the due date for your lab section  D01-07 due Oct 26 Oct 29, D08 due  Nov 2).

Objectives:

Practice program organization into functions

Practice basic dynamic storage allocation and clearing

Practice dealing with character-by-character input

Description

IKEA has recently unveiled several new aisles to display their new DJUNGELSKOG! However, IKEA needs help organizing how they stock their aisles in different stores. As a DJUNGELSKOG fan and top-notch C programmer, you are the perfect 201 student for the job!

Each newtype of DJUNGELSKOG (Grizzly, Polar, ...) being stocked has a unique serial code, along

with a one-character product name. New products are always stocked from left to right in the

aisles. DJUNGELSKOG are stocked into aisles based on the modulo of the serial code. So a given

product as line  123 G  at an IKEA with 4 aisles, the product name  G should go in aisle 3, since 123 % 4 = 3 . As is customary in IKEA, all aisles are infinitely long.

The input starts with a positive integer delimiting the number of new aisles for an IKEA. All

following lines will be one of the two types, a new product or a print line. All print lines start with

the number  0 followed by the index of the aisle you need to print. For example,  0 1  means

printing the second (index 1) aisle, with no spaces between product names. The only other line is a new product line. These have a serial code, which is a positive integer less than  2**30  (i.e., ),

followed by the product name represented by a single character. For example a Grizzly DJUNGELSKOG with serial code  123 is listed as the line  123 G .

Example input 1:

2

123 G

91312 B

9999 P

0 0

0 1

Example output 1:

B

GP

Example input 2:

20

0 0

795083255 s

990050615 5

718986123 1

39742171 O

126657912 _

468132353 q

844455765 c

701743244 M

243972890 ,

0 10

0 13

0 15

232040209 5

0 17

516936088 (

472120246 %

0 17

336483072 U

97516161 t

0 8

Example output 2:

,

q

s5

(

Implementation

You can implement your solution however you'd like, though we'll outline one approach below.

Your solution must run within a second with at most 25000 lines of input. This time limit shouldn't be difficult, so unless your code is unreasonably slow, you'll be fine. 50% marks will be docked if   your solution has memory leaks.

One possible solution to this lab uses an array of arrays. Each inner array represents one aisle,

while the outer array stores the aisles in order by their index. Here's an example of what example input 1 would look like:

0 -> ['B']

1 -> ['G', 'P']

Say we add a new line  10 A at the end of input 1. We first find the aisle this product should go in, which is  10 % 2 = 0 . We start by calling  realloc on aisle 0, expanding it to hold 2 characters.

Then we can insert A at the end of aisle 0, which makes it look like:

0 -> ['B', 'A']

1 -> ['G', 'P']

Your C code will only need one variable  char **aisles; to store all these data.

Testing

Provided in  lab07.tar , which can be found on eClass under Week 8, is  check and

lab07_case_generator.py . The  check program tests the example inputs in the description

above and you guarantee yourself a 50% on the lab if you pass the check script. You'll very likely not get a great mark if the  check script doesn't pass.

lab07_case_generator.py randomly generates test cases you can try your code on, however it   doesn't provide a solution for each test, only the input. The script takes 2 arguments, the number of aisles and the number of lines in the output. The command below generates a random input

for an IKEA with  3 aisles,  20 lines of input and stores it in a file called  input_2.txt .

python3 lab07_case_generator.py 3 20 > input_2.txt

Regardless of your implementation 50% of marks will be deducted for memory leaks. You can test your code with valgrind , using any input (try some of your own randomly generated

inputs!). Use the commands below:

gcc -Wall -std=c99 ex7q1.c

python3 lab07_case_generator.py 10 1000 > input.txt

valgrind ./a.out < input.txt

Something similar to the following output means you're leaking memory:

==488904== HEAP SUMMARY:

==488904== in use at exit: 254 bytes in 22 blocks

==488904== total heap usage: 38 allocs, 16 frees, 5,376 bytes allocated

Valgrind outputting something similar to this means you aren't leaking memory:

==489540== HEAP SUMMARY:

==489540== in use at exit: 0 bytes in 0 blocks

==489540== total heap usage: 38 allocs, 38 frees, 5,376 bytes allocated

Don't worry about the  LEAK SUMMARY section, we'll only look at the  HEAP SUMMARY 's in use at exit line.

Your program must compile with flags  gcc -Wall -std=c99 , which means you can't use  math.h ,  as we're not linking additional libraries (you don't need it either). We further strongly recommend   compiling with  gcc -Wall -Wextra -Werror -std=c99 , which will help catch more errors for you.

Submitting

1. Run the  check program (need to change its mode to be executable) and make sure it says you're good togo. Otherwise you won't be able to get full marks

#use `tar` to pack your files into `submit.tar`

#then

chmod 700 check

./check submit.tar

2. If you are not working on the lab machines, then scp  submit.tar  back to your local computer and submit it to eClass before the deadline

#run this on your own computer

#not needed if you are working on a lab machine

scp <ccid>@ug20.cs.ualberta.ca:~/<path_to_your_submit_file>/submit.tar .

//end of Lab #7 Copyright @Guohui Lin, 2023-2026