Advertisement
Guest User

Programming 102, test 2, Ahsan Zahid

a guest
Nov 24th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. // Ahsan Zahid
  2. // Math/CS 102
  3. // Test 2
  4.  
  5. // 1.
  6.  
  7. // a)
  8. // Creating a new pointer array of integers, fill with -1.
  9. int *p = new int[100];
  10. for(int x=0; x<100; x++){
  11.     p[x] = -1;
  12. }
  13.  
  14. // b)
  15. //Allocate numbers to A, spaces to B, then 9-x is reverse order in the loop.
  16. int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  17. int B[10];
  18. for(int x =0; x<10; x++){
  19.     B[x] = A[(9-x)];
  20. }
  21.  
  22. // c)
  23. // Some data already in p; so delete the contents of p, then reassign the pointer to &x;
  24. int *p = new int(5);
  25. int x = 10;
  26. cout<<p<<" "<<*p<<endl;
  27. cout<<&x<<" "<<x<<endl;
  28.  
  29. delete p;
  30. p = &x;
  31. cout<<p <<" "<<*p;
  32.  
  33. // d
  34. // Both p and q point to dynamically alocated int 5. Delete either, then set both to the null pointer.
  35. int *p = new int(5);
  36. int *q = p;
  37. cout <<*p<<" "<<*q<<" "<<p<<" "<<q<<endl;
  38. delete p;
  39. p = nullptr;
  40. q = nullptr;
  41. cout<<p<<" "<<q;
  42.  
  43. // e)
  44. // declare struct, then point both nodes at m.
  45. struct node{int n; node* p;};
  46. node n,m;
  47. // ...
  48. n.p = &m;
  49. m.p = &m;
  50. cout<<n.p<<" "<<m.p<<endl;
  51.  
  52. // 2.
  53. // a)
  54.     // i) {2, 3, 4}
  55.     // ii) {2, 3, 4}
  56.     // iii) {4.6, 5.0, 8.2}
  57.     // iv) {"Dog", "cat", "dog"}
  58. // b
  59. template <class T> void fcnB(T& a, T&b, T&c, bool compare(T& a, T&b)){
  60.     if(a.compare(b)) swap(a,b);
  61.     if(b.compare(c)) swap(b,c);
  62.     if(a.compare(b)) swap(a,b);
  63. }
  64.  
  65. // 3.
  66. // a)
  67. // Need a method to pop. To pop, take the last element, get rid of it, and shrink the vector.
  68. void pop() {
  69.         //Only remove if there is at least one element actually in the array.
  70.         if ((avail-1) != begin()){
  71.             //Temp contains  reference to the last pointer with something there.
  72.             T* temp = data[(avail-1)];
  73.             //Get rid of whatever is at temp.
  74.             alloc.deallocate(temp);
  75.             //Set avail one back, so it now points to the empty space we just created.
  76.             --avail;
  77.  
  78.         }
  79.     }
  80.    
  81. // b) To act as just a stack class, the [] operator is not needed, so long as the beginning and ending iterators are known and can be accessed. Any methods that access the interior of the vector (which aren't needed by other methods) can be taken out, as a stack only deals with the ending and sometimes starting elements.
  82.  
  83. // c) If our goal is just to return the top element... We just get it from data. If we start at data, go up to avail, and then go back one, we'll have arrived at the last/top element in the vector. We want the actual element, so we deference it.
  84. T getTopElement(){
  85.     return *data[(avail-1)];
  86. }
  87.  
  88. // 4.
  89.  
  90. // a) If n is odd, the parent is (n-1)/2. If n is even, parent is (n-2)/2
  91.  
  92. // b) In the given implementation, there's no definition as to what to do if the value to be searched does not exist in the tree. Will it return null? Will it create the node? Nothing is defined. Also it uses a single pointer for the tree, so it seems it can only really hold one node.
  93.  
  94. // c)
  95.     // i)
  96.     // If pt has the tree above, pt.ptr[3] would refer to 6. pt.prt[4] would need to be null, or some other sentinel defining the lack of a node at that space.
  97.    
  98.     // ii)
  99.     // First off, this uses an array of pointers, so it can hold more than one node. Also, the search function (as it claims, at least...) will point to where a number SHOULD be, if it does not actually exist in the tree.
  100.    
  101.     // iii)
  102. size_t search(T){
  103.     n=0;
  104.     while(true){
  105.         if(T>*pTr[n]) n = 2*n+2;
  106.         else if(T<*pTr[n]) n = 2*n+1;
  107.         //If there's nothing at that value.. then go ahead and return T.
  108.         if(!pTr[n]){
  109.             return n;
  110.         }
  111.         //if the thing at that node is T, then by George, we've found it! Return the position!
  112.         if(*pTr[n]==T){
  113.             return n;
  114.         }
  115.         //But if not, then do this same thing again from the new part of the tree.
  116.     }
  117.  
  118. }
  119.  
  120. size_t insert(T){
  121.     n=0;
  122.     while(true){
  123.         if(T>*pTr[n]) n = 2*n+2;
  124.         else if(T<*pTr[n]) n = 2*n+1;
  125.         //If there's nothing at that value.. then put T there.
  126.         if(!pTr[n]){
  127.             pTr[n] = &T;
  128.             return n;
  129.         }
  130.         //if the thing at that node is T, then it's already there. Just return the index.
  131.         if(*pTr[n]==T){
  132.             return n;
  133.         }
  134.         //But if we haven't found the right place yet, then do this same thing again from the new part of the tree.
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement