CS240 Lab07 Array Random Access

Overview

Write a program that can create, read and write non-negative integer arrays of different length. No array notation "[]" is allowed in this lab.

Lab Exercise

The user is allowed to input the following four commands (c,r,w,q):

1. "c n num1 num2 num3 ..."

    This command is used to create an array of length n, and there're exactly n integers following it. No output for this command.

    eg. "c 5 1 2 3 4 5" creates an array of length 5 which contains 1,2,3,4,5

2. "r m n"

    This command is used to read the nth element from the mth array that is entered previously. If the m or n is invalid, you should output "Out of Bound". The n and m start from 0.

    eg. "r 0 0" should output "1" after the input of the above command.

        "r 1 0" and "r 0 5" should output "Out of Bound".

3. "w m n p"

    This command is used to write integer p to the nth position of the mth array. No output for this command if m and n are valid. Else output "Out of Bound".

    eg. After entering "w 0 0 7", the program should output 7 if "r 0 0" command is entered.

4. "q"

    Quit the program.

You can assume the number of creation commands will be always less than 10. The length of an individual array can be an arbitrary positive number. All the input numbers are non-negative.

Sample Output

c 5 1 2 3 4 5
c 1 6
c 3 7 8 9
c 10 10 11 12 13 14 15 16 17 18 19
c 4 20 21 22 23
r 0 4
5
r 4 3
23
r 3 3
13
r 2 3
out of bound
r 5 0
out of bound
w 0 0 24
r 0 0
24
w 4 3 25
w 4 3 26
r 4 3
26
w 4 4 7
out of bound
w 5 0 2
out of bound
q

For convenience, you can download the input file here.

กก

Hint

As there're at most 10 arrays with different length, you can use a pointer array to store the addresses of those 10 integer arrays. The 10 integer arrays should be allocated dynamically. Again, remember, you cannot use the array notation [] for this pointer array. Also, don't forget to free the memory before your program terminates.

กก

Turnin

When you are satisfied that your program works correctly (or you run out of time), please do the following.

  1. Create a directory named lab07.
  2. Copy all program files (*.c and *.h) and your Makefile to the directory created in step 1.
  3. Goto the parent directory of lab07 and type the following command into the terminal:

turnin -c cs240=XXXX -p lab07 lab07

where XXXX represents your lab section number.

The turnin section is as follows:

Section

Time

TA

0201

Thursday 15:30-17:20

Dan Zhang

0301

Friday 09:30-11:20

Suli Xi

0401

Friday 13:30-15:20

Youhan Fang

0501

Thursday 09:30-11:20

J. C. Chin

Make sure turnin reports that your project was submitted for grading. You can check the files you have submitted by running the following command:

turnin -c cs240=XXXX -v -p lab07

  1. Submit often to make sure that you did not try to submit after turnin has been turned off.

 

Grading

Makefile

2

q command

2

c command

5

w command 5
r command 5
memory free 1


กก