USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -0 Department of Electronics and Communication INTERNAL ASSESSMENT TEST 1 Date : 30/8/2017 Marks: 0 Subject & Code : Data Structures using C++ (EC761) Sec : 7 th Sem A/B/C Name of faculty : Ms. Sai Prasanna M S Time : 11:30am -1:00 pm Note: Answer FIVE full questions. Marks Q.No 1 a. What are Data Structures? Justify their need with an illustrative example. b. Explain the need of Template Function and Template Class. 2 Explain the operators used for Dynamic Memory Allocation. Illustrate with a program - expanding the dynamic array. 3 What are the two types of representations of a linear list? Explain and distinguish them. Mention their relative Advantages and disadvantages. 4 What is linear list? Write the abstract class specification of a linear list. Using Formula based representation illustrate with a C++ program insertion of the element as the index-i th element using the template class for linear list. 6 a. In the following code, explain why the swap method fails to swap actual parameters. Change this code so that it swaps actual parameters, 4 void swap (int x, int y) int t = x ; x =y; y = t; b. Write a C++ function template for Bubble Sort. 7 Explain linked representation of singly linked list. Write a program using function templatei) to return the index of the first occurrence of the element ii) to insert an element in the index position. 8 Explain testing and debugging of a C++ code. Explain the different methods of testing. 6
SOLUTION MANUAL FOR INTERNAL ASSESSMENT TEST 1 Date : 30/8/2017 Marks: 0 Subject & Code : Data Structures using C++ (EC761) Sec : 7 th Sem A/B/C Name of faculty : Ms. Sai Prasanna M S Time : 11:30am -1:00 pm Note: Answer FIVE full questions. Marks Q.No 1 a. What are Data Structures? Justify their need with an illustrative example. b. Explain the need of Template Function and Template Class. Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. Class templates class can have members that use template parameters as types. For example: template <class T> class mypair T values [2]; public: mypair (T first, T second) values[0]=first; values[1]=second; ; 2 Explain the operators used for Dynamic Memory Allocation. Illustrate with a program - expanding the dynamic array. Dynamic memory In the programs seen in previous chapters, all memory needs were determined before program execution by defining the variables needed. But there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input. On these cases, programs need to dynamically allocate memory, for which the C++ language integrates the operators new and delete. Operators new and new[] Dynamic memory is allocated using operator new. new is followed by a data type specifier and, if a sequence of more than one element is required, the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated. Its syntax is:
pointer = new type pointer = new type [number_of_elements] The first expression is used to allocate memory to contain one single element of type type. The second one is used to allocate a block (an array) of elements of type type, where number_of_elements is an integer value representing the amount of these. For example: i nt * foo; foo=new int []; once it is no longer needed, it can be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of operator delete, whose syntax is: 1 delete pointer; 2 delete[] pointer; The first statement releases the memory of a single element allocated using new, and the second one releases the memory allocated for arrays of elements using new and a size in brackets ([]). The value passed as argument to delete shall be either a pointer to a memory block previously allocated with new, or a null pointer (in the case of a null pointer, delete produces no effect). 3 What are the two types of representations of a linear list? Explain and distinguish them. Mention their relative Advantages and disadvantages. A formula-based representation uses an array to represent the instances of an object. Each position of the array, called a cell or a node, holds one element that makes up an instance of that object. Individual elements of an instance are located in the array, based on a mathematical formula, e.g., a simple and often used formula is Location(i) = i 1,which says the ith element of the list is in position i 1. We also need two more variables, length and MaxSize, to completely characterize the list type. One way to overcome the inefficiency problem of the previous approach is to assign space on a need-only base. No space will be assigned if there is no need; and whenever there is a need, another piece of space will be assigned to an element. Since, we can t guarantee all the pieces of spaces assigned at different times will be physically adjacent, besides the space assigned for the elements, we also have to keep track of the location information of previously assigned pieces.
Hence, in a linked representation, each element of an instance is presented in a cell or node, which also contains a pointer that keeps information about the location of another node. 4 What is linear list? Write the abstract class specification of a linear list. L = (e0, e1, e2, e3,, en-1) Relationships: e0 is the zero th (or front) element en-1 is the last element ei immediately precedes ei+1 class linearlist public: virtual ~linearlist() ; virtual bool empty() const = 0; virtual int size() const = 0; virtual T& get(int theindex) const = 0; virtual int indexof(const T& theelement)const = 0; virtual void erase(int theindex) = 0; virtual void insert(int theindex, const T& theelement) = 0; virtual void output(ostream& out) const = 0; ; Using Formula based representation illustrate with a C++ program insertion of the element as the index-i th element using the template class for linear list. void arraylist<t>::insert(int theindex, const T& theelement) // Insert theelement. checkindex(theindex) ; // valid index, make sure we have space if (listsize == arraylength) // no space, double capacity changelength1d(element, arraylength, 2 * arraylength); arraylength *= 2; // shift elements right one position copy_backward(element + theindex, element + listsize, element + listsize + 1);
element[theindex] = theelement; listsize++; 6 a. In the following code, explain why the swap method fails to swap actual parameters. Change this code so that it swaps actual parameters, 4 void swap (int x, int y) int t = x ; x =y; y = t; Concept of actual parameter and formal parameter. Problem can be overcome using pointers. void swap (int *x, int *y) int t = x ; *x =*y; *y = *t; b. Write a C++ function template for Bubble Sort. 6 template< typename T > void sort( T a[], int n ) for( int i=0 ; i<n ; ++i ) for( int j=i+1 ; j<n ; ++j ) if( a[j] < a[i] ) int temp = a[i] ; a[i] = a[j] ; a[j] = temp ; 7 Explain linked representation of singly linked list. Write a program using function templatei) to return the index of the first occurrence of the element
ii) to insert an element in the index position. list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next int chain<t>::indexof(const T& theelement) const // search the chain for theelement chainnode<t>* currentnode = firstnode; int index = 0; // index of currentnode while (currentnode!= NULL && currentnode->element!= theelement) // move to next node currentnode = currentnode->next; index++; if (currentnode == NULL) return -1; else return index; void chain<t>::insert(int theindex, const T& theelement) if (theindex < 0 theindex > listsize) // Throw illegalindex exception if (theindex == 0) // insert at front firstnode = new chainnode<t>
(theelement, firstnode); else // find predecessor of new element chainnode<t>* p = firstnode; for (int i = 0; i < theindex - 1; i++) p = p->next; // insert after p p->next = new chainnode<t> (theelement, p->next); listsize++; 8 Explain testing and debugging of a C++ code. Explain the different methods of testing. Black box testing: when the tester has no idea of the internal working of the system which he is testing, that approach is called black box testing. In this case, the system under test is viewed as a black box. Requirements Document or Functional Specification Document forms the basis of this testing, which requires the user to understand the processes within the software. White box testing: The tester has the knowledge of the internals of a system and knows how the system is implemented. The tester uses this knowledge to develop test cases that will examine the control flow, information flow, data flow, exception and error handling as well as coding practices of the system. Debugging Techniques: Incremental testing Sanity checks Boolean constants for turning debugging code on or off Error variables for controlling program behavior after errors