expand icon
book ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff cover

ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff

Edition 2ISBN: 978-0131409095
book ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff cover

ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff

Edition 2ISBN: 978-0131409095
Exercise 2
Although the following program segment is structured, it is not a correct solution to Problem 1:
/ * Search the entries of the n × n matrix mat in rowwise order for an entry equal to item * / bool found;for (int row = 0; row n; row++) for (int col = 0; col n; col++) if (mat[row][col] == item) found = true; else found = false;if (found) cout "item found\n";else cout "item not found\n";
a. Write a program that incorporates this program segment and then perform black-box testing of it, beginning with the matrix mat =
Although the following program segment is structured, it is not a correct solution to Problem 1:  / * Search the entries of the n × n matrix mat in rowwise order for an entry equal to item * / bool found;for (int row = 0; row n; row++) for (int col = 0; col n; col++) if (mat[row][col] == item) found = true; else found = false;if (found) cout item found\n;else cout item not found\n;  a. Write a program that incorporates this program segment and then perform black-box testing of it, beginning with the matrix mat =     and item = 77 as the first set of test data. Produce enough output to show that your testing has turned up an error. b. Use program tracing to determine what is wrong with this program segment. and item = 77 as the first set of test data. Produce enough output to show that your testing has turned up an error.
b. Use program tracing to determine what is wrong with this program segment.
Explanation
Verified
like image
like image
(a)
Program:
/ / include required header file
#include
/ / for cout and cin statement
using namespace std;
/ / main function
int main()
{
/ / declare variable found as bool
bool found;
/ / declare the matrix as integer and also declare item, n as integer
int mat[50][20], item, n;
/ / get the n value
cout "Enter the n value\n";
cin n;
/ / get the matrix using for loop
cout "Enter the Matrix\n";
/ / matrix for row
for (int row = 0; row n;row++)
/ / matrix for column
for (int col = 0; col n;col++)
/ / get the matrix
cin mat[row][col];
/ / get the search item
cout "Enter search item\n";
cin item;
/ / search the given item using for loop
/ / search the item in row wise
for (int row = 0; row n;row++)
/ / search the item in column wise
for (int col = 0; col n;col++)
/ / check whether the item is found or not
if (mat[row][col] == item)
/ / if found, then assign the found is true
found = true;
else
/ / otherwise assign the found is false
found = false;
/ / if found equal to true, then display the item is found
if (found=true)
cout "item found\n";
/ / otherwise, display the item not found
else
cout "item not found\n";
}
Black-box testing:
Test case 1:
Enter the n value
3
Enter the Matrix
45
77
93
78
79
85
72
96
77
Enter search item
77
Item found
Test case 2:
Enter the n value
3
Enter the Matrix
45
77
93
78
79
85
72
96
77
Enter search item
88
Item not found
Test case 3:
Enter the n value
3
Enter the Matrix
45
77
93
78
79
85
72
96
77
Enter search item
45
Item found
Test case 4:
Enter the n value
3
Enter the Matrix
45
77
93
78
79
85
72
96
77
Enter search item
70
Item not found
Explanation:
• The corrected given code is used to check whether the search item is found in the given matrix or not and also it performs the black box testing.
• Black box testing means outputs formed for several input are tested for correctness which does not assuming the inside the program unit in the structure itself.
• From the given code,
o In the main function, declare the variable found as bool data type and item, n and mat [][] declare as integer data type.
o Get the n value form user and also get the matrix using for loop.
o Get the search item value.
o After that check the given item value is in the given matrix or not using if loop. If the item is found then assign the found=true. Otherwise assign the found = false.
o Then if found is equal to true, then display the item found. Otherwise display the item not found.
(b)
• In the given code, the wrong program segments are
o Does not the declare the variable for "item", "n", and "mat[][]" as "integer" data type.
o Missing for loop for row and column wise matrix which is for get the matrix.
o In the "if (found)" no condition, so we have to rewrite the "if" loop that is the correct condition is "if (found = true)".
close menu
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
cross icon