You are on page 1of 15

Date 12/02/2017

Assignment two
Question One
1. Write a program to implement the concept of while, for,
do—while, switch and if—else and prepare test cases,
expected output and compare it with the actual output.
I. While loop
//Program that can display n to 5 numbers
//where n is -5 to 5
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number"<<endl;
cin>>num;
cout<<"values : ";
while( num <=5 ) {
cout << num << ",";
num++;
}
return 0;
}

. Yes Negative 1.2.4.3..-3.5 3.5 Yes Positive b 3 3. int main () { int num.4. Size Set of Integers Outputs Case 1: a 2 1 1.4.2. cin>>num.5 Fail Character input II.5 Yes value in range Case 2 a 1 -4 -4. do…while loop //Program that can display n to 5 numbers //where n is -5 to 5 #include <iostream> using namespace std. cout << "value : ". 4.3.2.0.-2. Inputs Expected Observed Output Match No. Different number of test cases expected outcome and compare it with actual outcomes for while loop CASE Sr.0.4.1. cout<<"Enter a number"<<endl.1. 4. no.4. .-3.4.5 1.-2.3.1.3.2.5 1.5 values in range Case 3 a 2 10 Error message Null Fail Out b -7 Error message Null Fail range values Case 4 a 1 156777777777777777 Error message Null Fail Large number Case 5: a 1 m Error message 0.2.3.

4.-3.4. do { cout << num << ".5 values in range Case 3 A 2 10 Error message Null Fail Out B -7 Error message Null Fail range values Case 4 A 1 156777777777777777 Error message Null Fail Large number Case 5: A 1 r Error message 0.-3.1. Size Set of Integers Outputs Case 1: A 2 1 1. Yes Negative 1.-2. no. }while( num <=5 ).2.5 Yes value in range Case 2 A 1 -4 -4.1. return 0.4..3. Inputs Expected Observed Output Match No.1.5 1. 4. 4.3.3.3.-2.0.5 3.4.".5 Fail Character input .4. num = num + 1.5 1.0.2.2.2.4.2.3. } Different number of test cases expected outcome and compare it with actual outcomes for do…while loop CASE Sr.5 Yes Positive b 3 3..

cout << "You Passed!" << endl. cout << "You Passed!" << endl. cin >> mark. } else if(mark >= 80) { cout << "You got an A" << endl. . if(mark >= 90) { cout << "You got an A*" << endl. int main(){ int mark. if …else //Program that can display 0 to 5 numbers #include <iostream> using namespace std.III. } else if(mark >= 60) { cout << "You got an C" << endl. cout << "You Passed!" << endl. cout << "What mark did you get in the test?" << endl. } else if(mark >= 70) { cout << "You got an B" << endl.

cout << "You Failed!" << endl. cout << "You Failed!" << endl. } return 0. cout << "You Failed!" << endl. } else if(mark >= 50) { cout << "You got an D" << endl. } else { cout << "You got an U" << endl. cout << "You Passed!" << endl. } . } else if(mark >= 40) { cout << "You got an E" << endl.

Inputs Expected Observed Output Match no. Size Set of Integers Outputs Case 1: a 2 100 You got an A* You got an A* Yes Grade between You Passed! You Passed! Yes ranges 100 b 92 You got an A* You got an A* and 90 You Passed! You Passed! Case 2 a 1 80 You got an A You got an A* Yes Grade ranges You Passed! You Passed! between 80 and 89 Case 3: a 1 78 You got B You got B Yes Grade ranges You Passed! You Passed! between 70 and 79 Case 4 a 1 65 You got C You got C Yes Grade ranges You Passed! You Passed! between 60 and 69 Case 5: a 1 57 You got D You got D Yes Grade ranges You Failed! You Failed! between 50 and 59 Case 6: 40 You got an E You got an E Yes Grade ranges You Failed! You Failed! between 40 and 49 Case 4: You got an U You got an U Yes Grade ranges You Failed! You Failed! between 0 and 39 Case 7 a 2 108 Error message You got an A* Fail Out range You Passed! values b -7 You got an U Fail You Failed! Case 8 a 1 156777777777 Error message You got an A* Fail Large number 777777565656 You Passed! 56 Case 9: 1 1 Minichel Error message You got an U Fail Character or You Failed! string input . Sr. Different number of test cases expected outcome and compare it with actual outcomes for if…else statement CASE No.

cout<<"Enter a Letter A -to . break. int main () { char grade. } return 0. case 'D' : cout << "You passed" << endl. default : cout << "Invalid grade" << endl. Selection (switch. break.IV. case 'B' : case 'C' : cout << "Well done" << endl.D"<<endl. break. } . switch(grade) { case 'A' : cout << "Excellent!" << endl.. break.case) //A program using switch case #include <iostream> using namespace std. cin >>grade. case 'F' : cout << "Better try again" << endl.

Inputs Expected Observed Output Match no. Different number of test cases expected outcome and compare it with actual outcomes for switch case. For loop //Program that can display n to 5 numbers using for loop #include <iostream> using namespace std. j <=5. Size Set of Integers Outputs Case 1: a 2 B Well done Well done Yes Grade in range b D Passed You passed Yes Case 2 a 1 F Error Message Invalid grade Yes Out of range Case 3: a 1 801 Error Message Invalid grade Yes Number input Case 4: a 2 A Excellent Excellent Yes Block and b a Error message Invalid Yes Small letter Case 5: 1 1 Minichel Error message invalid Fail String input V. for(int j=num. cin>>num. CASE No. j++ ) . int main () { int num. Sr. cout<<"values : ". cout<<"Enter a number"<<endl.

} return 0. [Not a quadratic equation. Consider a program for the determination of the nature of roots of a quadratic equation.2.5 in range Case 3 A 2 10 Error Null Fail Out range B -7 message Null Fail values Error message Case 4 A 1 1567777777777 Error Null Fail Large number 77777 message Case 5: A 1 Q Error 0..2.4.-3.4.4.5 Yes in range Case 2 A 1 -4 -4. 4. .2.-3. { cout << j << ".5 Fail Character input message Question two 2.".3.4.-2. Yes Negative values 1.3.3.5 3.0. Equal roots] Design the boundary value test cases..5 1. Real roots. } Different number of test cases expected outcome and compare it with actual outcomes for For loop CASE No.5 1.1.1.-2. Inputs Expected Observed Match no.3.5 Yes Positive value b 3 3. Sr.1.4. The program output may have one of the following words.100]. 4. c) and values may be from interval [0.4.0. Imaginary roots.2. b. Its input is a triple of positive integers (say a.2.3. Size Set of Integers Outputs Output Case 1: A 2 1 1.

Equation is not quadratic if a = 0 Total number of Test cases = 4n + 1 where n → number of inputs = 4(3) + 1 = 13 Test cases . Roots are real if (b2 – 4 ac) > 0 II. Its input is a triple of positive integers (Say a. Roots are equal if (b2 – 4 ac) = 0 IV.Solution: This program is for the determination of the nature of roots of a quadratic equation. The program output may have one of the following words as given on the question. Roots are imaginary if (b2 – 4 ac) < 0 III. Equal roots] Quadratic Equation will be of type: ax2 + bx + c = 0 The outputs of the program could be:- I. 100]. c) and values may be from interval [0. [Not a quadratic equation Real roots. b. Imaginary roots.

cin>>c.determinant. cout<<"Enter the value of c: "<<endl. Boundary value test cases are given as: Test Case a b c Expected Output 1 0 50 50 Not Quadratic 2 1 50 50 Real Roots 3 50 50 50 Imaginary Roots Imaginary Roots 4 99 50 50 Imaginary Roots 5 100 50 50 Imaginary Roots 6 50 0 50 Imaginary Roots 7 50 1 50 Imaginary Roots 8 50 99 50 Equal Roots 9 50 100 50 Real Roots 10 50 50 0 Real Roots 11 50 50 1 Imaginary Roots 12 50 50 99 Imaginary Roots 13 50 50 100 // C++ program to determine the nature of the quadratic roots in #include<iostream> #include<math. int main() { char answer. .root2. cout<<"Enter the value of a: "<<endl. do { float a. cin>>b.root1.c. cout<<"Enter the value of b: "<<endl. cout<<"C++ program to determine roots of quadratic equation\n".b. cin>>a.h> using namespace std.

root2=-(b-sqrt(determinant))/(2*a). } else if(determinant<0) { root1=(-b)/(2*a). } else if(a==0) { cout<<"Not quadratic\n". cout<<"--------------". cout<<"-------------". root2=root1. } else if(determinant==0) { root1=(-b)/(2*a).determinant=b*b-4*a*c. cout<<"-----------". cout<<"Equal Roots\n". root2=sqrt(-determinant)/(2*a). . cout<<"Imaginary Roots \n". } else { root1=-(b+sqrt(determinant))/(2*a). if((a<0)||(b<0)||(c<0)||(a>100)||(b>100)||(c>100)) { cout<<"please enter valid numbers\n".

} cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2. cout<<"-----------". cout<<"\nDo you wish to calculate another? y/n"<<endl. } Screenshot of the program output . cin>> answer. } while((answer=='y') || (answer=='Y')). cout<<"Real Roots \n". return 0.