Вы находитесь на странице: 1из 15

Date 12/02/2017

Assignment two
Question One
1. Write a program to implement the concept of while, for,
dowhile, switch and ifelse 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;
}
Different number of test cases expected outcome and
compare it with actual outcomes for while loop

CASE Sr. Inputs Expected Observed Output Match


No. no. Size Set of Integers Outputs

Case 1: a 2 1 1,2,3,4,5 1,2,3,4,5 Yes


Positive b 3 3,4,5 3,4,5 Yes
value in
range
Case 2 a 1 -4 -4,-3,-2,- 4,-3,-2,- Yes
Negative 1,0,1,2,3, 4,5 1,0,1,2,3,4,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,1,2,3,4,5 Fail

Character
input

II. dowhile 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 << "value : ";
do {
cout << num << ",";
num = num + 1;
}while( num <=5 );

return 0;
}

Different number of test cases expected outcome and


compare it with actual outcomes for dowhile loop

CASE Sr. Inputs Expected Observed Output Match


No. no. Size Set of Integers Outputs

Case 1: A 2 1 1,2,3,4,5 1,2,3,4,5 Yes


Positive b 3 3,4,5 3,4,5 Yes
value in
range
Case 2 A 1 -4 -4,-3,-2,- 4,-3,-2,- Yes
Negative 1,0,1,2,3, 4,5 1,0,1,2,3,4,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,1,2,3,4,5 Fail

Character
input
III. if else
//Program that can display 0 to 5 numbers
#include <iostream>
using namespace std;

int main(){
int mark;
cout << "What mark did you get in the test?" << endl;
cin >> mark;

if(mark >= 90)


{
cout << "You got an A*" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 80)
{
cout << "You got an A" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 70)
{
cout << "You got an B" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 60)
{
cout << "You got an C" << endl;
cout << "You Passed!" << endl;
}
else if(mark >= 50)
{
cout << "You got an D" << endl;
cout << "You Failed!" << endl;
}
else if(mark >= 40)
{
cout << "You got an E" << endl;
cout << "You Failed!" << endl;
}
else
{
cout << "You got an U" << endl;
cout << "You Failed!" << endl;
}

return 0;
}
Different number of test cases expected outcome and
compare it with actual outcomes for ifelse statement

CASE No. Sr. 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
IV. Selection (switch..case)
//A program using switch case
#include <iostream>
using namespace std;

int main () {
char grade;
cout<<"Enter a Letter A -to - D"<<endl;
cin >>grade;
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
return 0;
}
Different number of test cases expected outcome and
compare it with actual outcomes for switch case.

CASE No. Sr. Inputs Expected Observed Output Match


no. 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 loop
//Program that can display n to 5 numbers using for loop
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number"<<endl;
cin>>num;
cout<<"values : ";
for(int j=num; j <=5; j++ )
{
cout << j << ",";
}
return 0;
}
Different number of test cases expected outcome and
compare it with actual outcomes for For loop

CASE No. Sr. Inputs Expected Observed Match


no. Size Set of Integers Outputs Output
Case 1: A 2 1 1,2,3,4,5 1,2,3,4,5 Yes
Positive value b 3 3,4,5 3,4,5 Yes
in range
Case 2 A 1 -4 -4,-3,-2,- 4,-3,-2,- Yes
Negative values 1,0,1,2,3, 4,5 1,0,1,2,3,4,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,1,2,3,4,5 Fail
Character input message

Question two
2. Consider a program for the determination of the nature of
roots of a quadratic equation. Its input is a triple of
positive integers (say a, b, c) and values may be from
interval [0,100]. The program output may have one of the
following words. [Not a quadratic equation; Real roots;
Imaginary roots; Equal roots] Design the boundary value
test cases.
Solution:
This program is for the determination of the nature of roots of a
quadratic equation. Its input is a triple of positive integers (Say
a, b, c) and values may be from interval [0, 100]. The program
output may have one of the following words as given on the
question.

[Not a quadratic equation Real roots; Imaginary

roots, Equal roots] Quadratic Equation will be of

type: ax2 + bx + c = 0

The outputs of the program could be:-

I. Roots are real if (b2 4 ac) > 0


II. Roots are imaginary if (b2 4 ac) < 0
III. Roots are equal if (b2 4 ac) = 0
IV. 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
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.h>
using namespace std;
int main()
{
char answer;
cout<<"C++ program to determine roots of quadratic equation\n";
do
{
float a,b,c,determinant,root1,root2;
cout<<"Enter the value of a: "<<endl;
cin>>a;
cout<<"Enter the value of b: "<<endl;
cin>>b;
cout<<"Enter the value of c: "<<endl;
cin>>c;
determinant=b*b-4*a*c;
if((a<0)||(b<0)||(c<0)||(a>100)||(b>100)||(c>100))
{
cout<<"please enter valid numbers\n";
}
else if(a==0)
{
cout<<"Not quadratic\n";
cout<<"-------------";
}
else if(determinant==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Equal Roots\n";
cout<<"-----------";
}
else if(determinant<0)
{
root1=(-b)/(2*a);
root2=sqrt(-determinant)/(2*a);
cout<<"Imaginary Roots \n";
cout<<"--------------";
}
else
{
root1=-(b+sqrt(determinant))/(2*a);
root2=-(b-sqrt(determinant))/(2*a);
cout<<"Real Roots \n";
cout<<"-----------";
}
cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2;
cout<<"\nDo you wish to calculate another? y/n"<<endl;
cin>> answer;
}
while((answer=='y') || (answer=='Y'));
return 0;
}

Screenshot of the program output

Вам также может понравиться