Question: C++ with comments #ifndef VECTOR_H #define VECTOR_H class Vector { public: Vector (); // default constructor Vector (int s); // makes size = s, //allocates

C++ with comments

#ifndef VECTOR_H #define VECTOR_H class Vector { public: Vector (); // default constructor Vector (int s); // makes size = s, //allocates s space // e.g. entries = new int[size], // makes all entries 0 Vector (const Vector & other); // copy constructor // makes a deep copy ~Vector (); // default destructor void print (); // Prints out the vector void set( int val, int pos); // if 0 <=pos

Test both valid and invalid input Test for every output expected Print ( ) Specification: Print will put out the elements surrounded by [ ]s as shown below [1 2 3] Write methods defined in header file and use the following stub Main to test header file #include Vector.h #include int main() {

// REQUIRED CODE Vector a, b(4), c(3) ; a.print(); // outputs [] b.print(); // outputs [ 0 0 0 0 ] c.set(0,-1); // output error message c.set(2,1); c.set(4,3); // outputs error message c.set(1,0); c.set(3,2); c.print(); // outputs [ 1 2 3 ] Vector d(c); d.print(); // outputs [ 1 2 3 ] d.set(0,1); d.print(); // outputs [ 1 0 3 ] c.print(); // outputs [ 1 2 3 ] proves deep copy // ADDITIONAL TEST CASES [ Insert your code for YOUR test cases here ] return 0; } Program 2. Write a function to calculate the square root of a float number with the following interface: double squareRoot( double x) { // assert that x is not negative ... } The function should return an approximation to using series approximation. Let x0 = x/2. Then xn+1 = (xn + x/xn)/2. Keep computing terms until the difference between xn and xn+1 is less than 0.0001. In addition, if the input variable x is negative, your function should stop the execution via the assert( ) function. You need to add a function call to assert( ) at the beginning of the above function to guarantee the precondition of this function is correct. In your main function, design it to request the user to enter as many numbers as they want (continue to enter values? y or n). Test cases must include values 3,0, and -3, as well as others you think are appropriate.

The specifics of the assert message shown depends on the specific implementation in the compiler, but it should include: the whose assertion failed, the name of the source file, and the line number where it happened. A usual expression format is: Assertion failed: file filename, line line number #include void print_number(int myInt) { assert (myInt > 5); This sample snippet will abort when myInt has value of 5 or less because myInt > 5 evaluates to false. Program 3. Redo program 2 using try/catch instead of assert, using multiple catches [negative, default, others?]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!