Starting from:

$25

CSE031-Lab 3 Memory in C Solved

In this exercise, we will explore how structures are stored in memory.

 

TPS activity 2: Discuss questions 1 – 3 with your TPS partners in your assigned group (20 minutes) and record your answers in tpsAnswers.txt under a section labelled “TPS 2”:

1.       Open NodeStruct.c and discuss what this program does.

2.       Insert extra code to print out the value of head, addresses of head, iValue, fValue, and next pointed by head.

3.       Based on the addresses of the members of Node structure, what do you observe about how structures are stored in memory? What is the relationship between the pointer (head) and its destination (the Node structure)?

 

Individual Assignment 1: Arrays and pointers
As we have discussed in lecture, we can use array names as if they are pointers. Open array.c and complete the following tasks:

1.       This program will store integers entered by a user into an array. It then calls bubbleSort to sort the array. Study the code in bubbleSort to refresh your memory on Bubble Sort algorithm and answer the following questions:

a.       Why do we need to pass the size of array to the function?

b.      Is the original array (the one being passed into the function) changed at the end of this function?

c.       Why do you think a new array (s_array) is needed to store the result of the sorted values (why not update the array as we sort)? Hint: look at what the main function does.

2.       Once you understand how Bubble Sort works, re-write the code so that you are accessing the array’s content using pointer notations (*s_arr), i.e., you cannot use s_arr[j] anymore. Comment out the original code so the algorithm will not be run twice.  

3.       After the array is sorted, the program will ask user to enter a key to search for in the sorted array. It will then call bSearch to perform a Binary Search on the array. Complete the bSearch function so that it implements Binary Search recursively (no loop!).  You must use pointer notations here as well. Pay attention to what is written in main so your bSearch will return an appropriate value.  

 

Individual Assignment 2: Cyclic Linked List
In cyclic_ll.c, complete the function has_cycle() to implement the following algorithm for checking if a singly-linked list has a cycle.

 

Recall that if p is a pointer to a struct, p->member refers to a member variable in the struct, and is equivalent to (*p).member.

1.       Start with two pointers at the head of the list

2.       On each iteration, increment the first pointer by one node and the second pointer by two nodes. If it is not possible to do one or both because of a null pointer, then we know there is an end to the list and there is therefore no cycle.

3.       We know there is a cycle if

a.       The second pointer is the same as the first pointer

b.      The next node of the second pointer is pointed to by the first pointer

The reason we know there is a cycle with the two conditions in 3) is that second pointer has wrapped around to the first one in the circle and we have detected it. After you have correctly implemented has_cycle, the program you get when you compile cyclic_ll.c will tell you that has_cycle agrees with what the program expected it to output.

More products