Starting from:

$30

Lab 0301 Solution




1. Problem Statement




Create a program that contains a function to calculate the square root of a number.




2. Requirements




2.1 Assumptions




Written in C++



Only use command line input/output



User will input real numbers only



User will input only ‘y’ or ‘n’ when asked to repeat



2.2 Specifications




The program will prompt the user to input a real number



For valid real number input



Calculate the square root



For invalid input



If input is negative



Terminate program



Decomposition Diagram



Program



Input



User inputs real number via command line



User inputs ‘y’ or ‘n’ character



Process



Determine if input is valid



Use series approximation to calculate ​sqrt​(​x​)
Output



Print result of square root operation



Print error for invalid data



Test Strategy



Valid Data



Invalid Data



5. Test Plan Version 1







Test Strategy
#
Description
Input
Expected
Actual
Pass/Fail








Output
Output
















Valid Data
1
squareRoot​() at minimum possible








Valid Data
2
squareRoot​() above minimum








Valid Data
3
squareRoot​() higher order








Valid Data
4
squareRoot​() EVEN HIGHER








Invalid Data
1
squareRoot​() below minimum

















Initial Algorithm



Create Square Root Function ​squareRoot​()

Parameter



Double Input ​x
Method



Assert that input is greater than 0



Calculate Square root via series approximation



Set Xn equal to 2x



Evaluate such that xn+1 = (xn + xxn ) / 2



Keep evaluating until xn − xn+1 < 0.0001
Return Root Value



Create ​main​()

Prompt user to input double to use



Run ​squareRoot​()
Prompt user if they would like to calculate again



If input is ‘y’



Run again



If input is ‘n’



Exit






7. Test Plan Version 2







Test Strategy
#
Description
Input
Expected
Actual
Pass/Fail








Output
Output
















Valid Data
1
squareRoot​() at minimum possible
0
0




Valid Data
2
squareRoot​() above minimum
1
1




Valid Data
3
squareRoot​() higher order
3
~sqrt​(3)




Valid Data
4
squareRoot​() EVEN HIGHER
16
4




Invalid Data
1
squareRoot​() below minimum
-3
assert​()






More products