Bonus lab.pdf

12
for Week 13, Wednesday, 10th November 2010 Back to index page This is a bonus lab. This lab is outside of examinable curriculum for CS1010e. While it is an achievement to be able to complete this lab, do not worry if you are unable to solve the questions. If you have any questions, you may post your queries in the IVLE forum of your respective lecture group. Instructions There is only one task. 1. Submit all tasks to CodeCrunch . You are given 20 submissions for each task. 2. You may need to refer to the C online reference here . 3. There is no deadline. http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/ 1 of 12 11/9/2010 7:20 AM

description

Bonus lab.pdf

Transcript of Bonus lab.pdf

Page 1: Bonus lab.pdf

for Week 13, Wednesday, 10th November 2010

Back to index page

This is a bonus lab. This lab is outside of examinable curriculum for CS1010e. While it is an achievement to be able to complete this lab, donot worry if you are unable to solve the questions.

If you have any questions, you may post your queries in the IVLE forum of your respective lecture group.

Instructions

There is only one task.1.Submit all tasks to CodeCrunch. You are given 20 submissions for each task.2.You may need to refer to the C online reference here.3.

There is no deadline.

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

1 of 12 11/9/2010 7:20 AM

Page 2: Bonus lab.pdf

In this task you are given an input containing n lines, each is a string of length n where 1 <= n <= 20. This input corresponds to a maze, with'#' being the walls and '.' being traversable spaces. Your task is to find the path, given a start coordinate and the end coordinate. The mazetraveler may only move 4 directions: up, down, left and right.

We use the "keep your left hand on the wall" method. The algorithm to complete solve the maze problem is given as follows:

while destination is not reached, if you can turn left, then turn left, otherwise if you can go forward, then stay put otherwise if you can turn right, then turn right otherwise turn back. move forward for one step

The first line of a input is a single number n, which indicates the size of the grid.

The next n lines contains a string of length n each. Each character in the string is either '.' or '#', where '#' represent the walls and '.'represent traversable spaces.

The next line contains 4 numbers. The first two numbers are the row and column numbers of the start position. The next two numbers are therow and column numbers of the end position.

Your output should the original grid with the '+' character indicating the path taken.

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

2 of 12 11/9/2010 7:20 AM

Page 3: Bonus lab.pdf

Sample run using interactive input (user's input shown in blue; output shown in bold purple. Note the space after everynumber including the last number.

10###########.####...##....#.#.#####.#.#.##......#.##.##.#.#.##....#.#.#######.#.#######....##########1 1 8 9##########↵#+####+++#↵#++++#+#+#↵####+#+#+#↵#...+++#+#↵#.##.#.#+#↵#....#.#+#↵######.#+#↵######..++↵##########↵

Sample run 2:

10###########.####...##....#.#.#####.#.#.##......#.##.##.#.#.#

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

3 of 12 11/9/2010 7:20 AM

Page 4: Bonus lab.pdf

#....#.#.##.####.#.##........###########1 1 7 1##########↵#+####+++#↵#++++#+#+#↵####+#+#+#↵#...+++#+#↵#.##.#.#+#↵#....#.#+#↵#+####.#+#↵#++++++++#↵##########↵

Sample run 3:

10###########.####...##....#.#.#####.#.#.##......#.##.##.#.#.##....#.#.#######.#.#######...###########8 8 7 6##########↵#.####...#↵#....#.#.#↵####.#.#.#↵#......#.#↵#.##.#.#.#↵#....#.#.#↵

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

4 of 12 11/9/2010 7:20 AM

Page 5: Bonus lab.pdf

######+#.#↵######+++#↵##########↵

Sample run 4:

10###########.####...##....#.#.#####.#.#.##......#.##.##.#.#.##....#.#.#######.#.#######...###########8 8 6 1##########↵#.####...#↵#....#.#.#↵####.#.#.#↵#...+++#.#↵#.##+#+#.#↵#++++#+#.#↵######+#.#↵######+++#↵##########↵

Sample run 5:

10###########.####...##....#.#.#####.#.#.##......#.#

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

5 of 12 11/9/2010 7:20 AM

Page 6: Bonus lab.pdf

#.##.#.#.##....#.#.#######.#.#######...###########8 8 1 1##########↵#+####...#↵#++++#.#.#↵####+#.#.#↵#++++++#.#↵#+##+#+#.#↵#++++#+#.#↵######+#.#↵######+++#↵##########↵

You should write your code in the file Maze.c

Submit your program through CodeCrunch.

Warning: This section introduces new concepts such as dynamic memory allocation and pointer to pointer to struct. The program requires adeep understanding of pointers and is a challenge to even A+ students. If you do not want your brain to explode in a spectacular manner,leave this page and go to here. In your lecturer's time (which is not too long ago), LinkedList was taught in the last lecture of the CS1010Eequivalent module.

In computer science, a linked list is a data structure that consists of a sequence of data records such that in each record there is a field thatcontains a reference (i.e., a link) to the next record in the sequence.

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

6 of 12 11/9/2010 7:20 AM

Page 7: Bonus lab.pdf

Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstractdata structures, including stacks, queues, associative arrays, and symbolic expressions.

The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the dataitems are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with aconstant number of operations. This is not true in an array, where we have to perform a shift on all items to the right if we were to insert anitem at any point in the array.

For this task, you will be implementing a simple Linked List data structure to store a sequence of numbers. You will be implementing only asingle function which is listed below:

insert (num, i) : Insert an the number num at position i.

(Optional) You may wish to also implement this function:

delete (i) : deletes the item at position i.

8 of the test cases will test a series of insert operations. 2 of the test cases will test a series of insert AND delete operations. If you did notimplement the optional part of this question, you will fail 2 test cases, so don't be surprised.

A working sample code is given to you. It will require extensive modifications before you can use it to to complete your task. You should codemodularly (implement the functions insert and delete), otherwise it would defeat the purpose of attempting this exercise.

We will begin to explain the details of this problem. The section below is a summary of what you need to know.

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

7 of 12 11/9/2010 7:20 AM

Page 8: Bonus lab.pdf

List Nodes

Before we start, we need to understand the concept of a Linked List. A Linked List is a chain of Nodes. Each node is connected to a singlenode (as seen in the diagram above) in a chain. In this task, a Node need only store two information: its payload (which is the value it isstoring) and a pointer to the next node in the list. For example in the diagram above, the leftmost node is storing the value 12, and have apointer pointing to the node containing the value 99.

Naturally we would use a struct to represent a node. We do this with the struct definition below:

struct ListNode{ struct ListNode *next; int value;};

For example to construct the list in the diagram above, we can simply call the following statements:

struct ListNode a, b, c;c.value = 37;c.next = NULL; //This means that c does not have a next element

b.value = 99;b.next = &c; //This means that the next element of b is a pointer to c

a.value = 12;a.next = &b; //This means that the next element of a is a pointer to b

The head of the list is at the ListNode a. We can see that a has a pointer to b, b has a pointer to c and c does not point to anything.

Printing out the Nodes

When printing out a String, we can simply iterate through all the characters until we reach the null character and halt.

This is similar when we try to print out the contents of a List. We iterate until we see that the next pointer is not pointing to anything.

void printList(struct ListNode *curr){ while(curr != NULL)

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

8 of 12 11/9/2010 7:20 AM

Page 9: Bonus lab.pdf

{ printf(?d ? curr->value); curr = curr->next; } printf(�\n?;}

Dynamic Memory Allocation

Unlike an array where the memory space is allocated during compilation and not runtime, the memory used by a Linked List increasesdynamically during runtime when items are added into the list. As such we need to perform dynamic memory allocation by calling thefunction malloc whenever we need to add a new item to the list. The function malloc returns a pointer to the memory allocated.

The function malloc requires a parameter - which is the size in bytes to allocate the memory. We can easily obtain the size required by ourListNode struct by using the function sizeof as follows:

sizeof(struct ListNode) //Try doing the following: printf("%d %d", sizeof(int), sizeof(double));

Therefore to request the OS to allocate a memory location to our ListNode, we do the following:

malloc(sizeof(struct ListNode));

Malloc returns a void pointer to the memory allocated. To avoid compilation warnings, we should cast it to the appropriate type. Naturally weshould assign this memory space to a pointer so that we can access it later on.

struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));

Now that we have introduced the basic concepts, we can start working on our program!

Adding an item

To help you out, we demonstrate how to add an item to the beginning of the list. The idea is that we shift the current head node to theRIGHT. To do this, we set the new item that we are going to add as the new head node, and set the next element of this new head node tobe pointing to the old head element.

int main(){ struct ListNode *head = NULL, *temp;

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

9 of 12 11/9/2010 7:20 AM

Page 10: Bonus lab.pdf

int n;

do { scanf(?d? &n); if(n > 0) { temp = malloc(sizeof(struct ListNode); temp->value = n; if(head == NULL) head = temp; else { temp->next = head; head = temp; } } } while(n > 0); printList(head);}

For clarity, it is recommended that you look at the slides to understand how this piece of code works. Naturally, we want to have modularcode. We rewrite the code above to obtain the following:

int main(){ struct ListNode *head = NULL, *temp; int n;

do { scanf(?d? &n); if(n > 0) { addHead(&head); } } while(n > 0); printList(head);

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

10 of 12 11/9/2010 7:20 AM

Page 11: Bonus lab.pdf

} void addHead(struct ListNode **head){ struct ListNode *curr = malloc(sizeof(struct ListNode)); curr->value = num;

if(*head == NULL) *head = curr; else { curr->next = *head; *head = curr; }} You may notice that the fuction addHead takes a pointer to a pointer as an argument. Why do we need to do that? This is because, withevery insertion, the head node will change. If we call the function addHead with just the pointer to the struct as an argument, any changes tothe value of the pointer itself will not apply to the head variable in the main function! Input and OutputThe input contains n lines. Each line consists of a String command, followed by the parameters required by the command. There are threepossible commands

insertdeletehalt

If the command is insert, there will be two integers num and i following the command. This means to insert the number num at position i. Youmay assume that the position is always valid (if there are only two items in the list, valid positions would be 0, 1 and 2). If the command is delete, there will be one integer i following the command. This means to delete the number at position i. Again you mayassume that the position specified is always valid. If the command is halt, you should stop asking for user input and print the list.

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

11 of 12 11/9/2010 7:20 AM

Page 12: Bonus lab.pdf

Sample run using interactive input (user's input shown in blue; output shown in bold purple).

insert 1 0insert 2 0insert 3 0halt3 2 1 ↵

Sample run 2:

insert 1 0insert 2 1insert 3 2halt1 2 3 ↵

Sample run 3:

insert 1 0insert 2 0insert 3 0insert 4 3halt3 2 1 4 ↵

Download the skeleton file LinkedList.c here. You will need to make significant modifications to this code to solve this task.

Submit your program through CodeCrunch.

http://www.comp.nus.edu.sg/~cs1010e/labs/Lab6/

12 of 12 11/9/2010 7:20 AM