Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. /*********************************************************************
  2. PROGRAM: CSCI 241 Assignment 5
  3. PROGRAMMER: Sam Vi
  4. LOGON ID: Z1787166
  5. DUE DATE: 10/27/2016
  6.  
  7. FUNCTION: This program tests the functionality of the VectorN
  8. class.
  9. *********************************************************************/
  10.  
  11. #include <iostream>
  12. #include <stdexcept>
  13. #include "VectorN.h"
  14.  
  15. using std::cout;
  16. using std::endl;
  17. using std::out_of_range;
  18.  
  19. int main()
  20. {
  21. int test = 1;
  22.  
  23. cout << "\nTest " << test++ << ": Default constructor and printing\n" << endl;
  24.  
  25. const VectorN v1;
  26.  
  27. cout << "v1: " << v1 << endl;
  28.  
  29. cout << "\nTest " << test++ << ": Array constructor and printing\n" << endl;
  30.  
  31. double ar2[] = {1.0, 2.0, 3.0};
  32.  
  33. const VectorN v2(ar2, 3);
  34.  
  35. cout << "v2: " << v2 << endl;
  36.  
  37. cout << "\nTest " << test++ << ": Clear and size\n" << endl;
  38. VectorN v3(ar2, 3);
  39.  
  40. cout << "The size of v3: " << v3 << " is " << v3.size() << endl;
  41.  
  42. v3.clear();
  43.  
  44. cout << "After clearing, the size of v3: " << v3 << " is ";
  45. cout << v3.size() << endl;
  46.  
  47. cout << "\nTest " << test++ << ": Subscripting\n" << endl;
  48.  
  49. double ar3[] = {-1.0, -3.0, -5.0, 7.0};
  50. const VectorN v4(ar3, 4);
  51.  
  52. cout << "v4: " << v4 << endl;
  53. cout << "v4[0]: " << v4[0] << " v4[1]: " << v4[1] << endl;
  54. cout << "v4[2]: " << v4[2] << " v4[3]: " << v4[3] << endl;
  55.  
  56. VectorN v5(ar3, 4);
  57. v5[0] = 17; v5[1] = 3; v5[2] = 0; v5[3] = -9;
  58. cout << "v5: " << v5 << endl;
  59.  
  60. cout << "\nTest " << test++ << ": Copy constructor\n" << endl;
  61.  
  62. const VectorN v6(ar2, 3);
  63. const VectorN v7 = v6;
  64.  
  65. cout << "v6: " << v6 << " size: " << v6.size() << endl;
  66. cout << "v7: " << v7 << " size: " << v7.size() << endl;
  67.  
  68. VectorN v8(ar3, 4);
  69. VectorN v9 = v8;
  70.  
  71. cout << "v8: " << v8 << " size: " << v8.size() << endl;
  72. cout << "v9: " << v9 << " size: " << v9.size() << endl;
  73.  
  74. v8[1] = 300.0;
  75. cout << "Changing..." << endl;
  76. cout << "v8: " << v8 << " size: " << v8.size() << endl;
  77. cout << "v9: " << v9 << " size: " << v9.size() << endl;
  78.  
  79. cout << "\nTest " << test++ << ": Assignment operator\n" << endl;
  80. VectorN v10(ar3, 4);
  81. VectorN v11;
  82.  
  83. cout << "v10: " << v10 << endl;
  84. cout << "v11: " << v11 << endl;
  85.  
  86. v11 = v10;
  87.  
  88. v10[0] = 0.0;
  89.  
  90. cout << "v10: " << v10 << endl;
  91. cout << "v11: " << v11 << endl;
  92.  
  93. cout << endl;
  94.  
  95. // Chained assignment
  96. VectorN v12, v13;
  97.  
  98. cout << "v11: " << v11 << endl;
  99. cout << "v12: " << v12 << endl;
  100. cout << "v13: " << v13 << endl;
  101.  
  102. v13 = v12 = v11;
  103.  
  104. cout << "v11: " << v11 << endl;
  105. cout << "v12: " << v12 << endl;
  106. cout << "v13: " << v13 << endl;
  107.  
  108. cout << endl;
  109.  
  110. // Assignment to self
  111.  
  112. v13 = v13;
  113.  
  114. VectorN v14(ar2, 3);
  115.  
  116. cout << "v13: " << v13 << endl;
  117.  
  118. cout << "\nTest " << test++ << ": Addition and subtraction\n" << endl;
  119.  
  120. double ar4[] = {-2.0, 3.0, -1.0};
  121. double ar5[] = {0, 1, 2, -3};
  122.  
  123. const VectorN v15(ar2, 3), v16(ar4, 3);
  124. const VectorN v17(ar5, 4);
  125.  
  126. cout << "v13: " << v13 << endl;
  127. cout << "v15: " << v15 << endl;
  128. cout << "v16: " << v16 << endl;
  129. cout << "v17: " << v17 << endl;
  130.  
  131. cout << endl;
  132. cout << "v15 + v16 is " << v15 + v16 << endl;
  133. cout << "v13 + v17 is " << v13 + v17 << endl;
  134. cout << "v15 + v13 is " << v15 + v13 << endl;
  135. cout << "v17 + v16 is " << v17 + v16 << endl;
  136.  
  137. cout << endl;
  138. cout << "v15 - v16 is " << v15 - v16 << endl;
  139. cout << "v13 - v17 is " << v13 - v17 << endl;
  140. cout << "v15 - v13 is " << v15 - v13 << endl;
  141. cout << "v17 - v16 is " << v17 - v16 << endl;
  142.  
  143. cout << "\nTest " << test++ << ": Vector multiplication\n" << endl;
  144.  
  145. cout << "The scalar product of " << v15 << " and " << v16 << " is ";
  146. cout << v15 * v16 << endl;
  147.  
  148. cout << "The scalar product of " << v13 << " and " << v17 << " is ";
  149. cout << v13 * v17 << endl;
  150.  
  151. cout << "The scalar product of " << v13 << " and " << v15 << " is ";
  152. cout << v13 * v15 << endl;
  153.  
  154. cout << "The scalar product of " << v16 << " and " << v17 << " is ";
  155. cout << v16 * v17 << endl;
  156.  
  157. cout << "\nTest " << test++ << ": Scalar multiplication\n" << endl;
  158.  
  159. double k = 2.345;
  160.  
  161. cout << v17 << " * " << k << " = " << v17 * k << endl;
  162. cout << k << " * " << v17 << " = " << k * v17 << endl;
  163.  
  164. cout << "\nTest " << test++ << ": Equality\n" << endl;
  165.  
  166. double ar6[] = {1, 2, 2};
  167. double ar7[] = {1, 2, -2};
  168. double ar8[] = {1, 2, 2, 0};
  169. double ar9[] = {1, 2, -2, 0};
  170.  
  171. const VectorN v18(ar6, 3), v19(ar7, 3);
  172. const VectorN v20(ar8, 4), v21(ar9, 4);
  173.  
  174. cout << v18 << " and " << v18 << " are ";
  175.  
  176. if (v18 == v18)
  177. cout << "equal" << endl;
  178. else
  179. cout << "not equal" << endl;
  180.  
  181. cout << v18 << " and " << v19 << " are ";
  182.  
  183. if (v18 == v19)
  184. cout << "equal" << endl;
  185. else
  186. cout << "not equal" << endl;
  187.  
  188. cout << v18 << " and " << v20 << " are ";
  189.  
  190. if (v18 == v20)
  191. cout << "equal" << endl;
  192. else
  193. cout << "not equal" << endl;
  194.  
  195. cout << v21 << " and " << v19 << " are ";
  196.  
  197. if (v21 == v19)
  198. cout << "equal" << endl;
  199. else
  200. cout << "not equal" << endl;
  201.  
  202. cout << "\nTest " << test++ << ": Write form of at() method\n" << endl;
  203.  
  204. VectorN v22(ar9, 4);
  205.  
  206. try
  207. {
  208. v22.at(0) = 9.1;
  209. v22.at(1) = 3.97;
  210. v22.at(-1) = -7.43;
  211. }
  212. catch (out_of_range orex)
  213. {
  214. cout << "Caught "<< orex.what() << endl;
  215. }
  216.  
  217. cout << "\nTest " << test++ << ": Read form of at() method\n" << endl;
  218.  
  219. try
  220. {
  221. cout << "v22: (";
  222. for (unsigned i = 0; i <= v22.size(); i++)
  223. cout << v22.at(i) << ", ";
  224. cout << ")\n\n";
  225. }
  226. catch (out_of_range orex)
  227. {
  228. cout << endl << "Caught "<< orex.what() << endl;
  229. }
  230.  
  231. return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement