Quiz 1: Software Engineering

Computing

Structured Program: The below program is the structured program from the given unstructured program. / / declare the all variable as integer int row = 0, col, n, mat[20][20],item; / / initializes the column value is zero A: col = 0; / / if column less than n then go to B statement. if (col n) goto B; / / otherwise go to A statement goto A; / / if row less than n then go to the next loop for searching whether the item found or not B: if (row n) { / / if the item is found in the matrix then it will display the item is found if (mat[row][col] == item) { / / display the item found cout "item found\n"; } / / otherwise else { / / increment the column value col++; / / if the column value is less than n then again chech the row if (col n) goto B; / / increase the row value row++; / / after increment goto A goto A; } } / / otherwise it will display the item not found else { / / display the item not found cout "item not found\n"; } Program: / / Include required header files #include using namespace std; / / Function main int main() { / / declare the all variable as integer int row = 0, col, n, mat[20][20],item; / / initializes the column value is zero A: col = 0; / / if column less than n then go to B statement. if (col n) goto B; / / otherwise go to A statement goto A; / * if row less than n then go to the next loop for searching whether the item found or not * / B: if (row n) { / *if the item is found in the matrix then it will display the item is found* / if (mat[row][col] == item) { / / display the item found cout "item found\n"; } / / otherwise else { / / increment the column value col++; / *if the column value is less than n then again chech the row * / if (col n) goto B; / / increase the row value row++; / / after increment goto A goto A; } } / / otherwise it will display the item not found else { / / display the item not found cout "item not found\n"; } return 0; } Sample Output: item found

(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)".

Analysis: When analyzing the given program segment, it is used to check the whether the given search item is found in the given matrix or not. If the search item is found, it returns true. Otherwise returns false. Reason: In the problem 1, the variable "found" is not declared and initialized which means "bool found = false" is missing in the given program. Another reason is that cannot use the goto statement in given program which is not structured. Explanation: • The given program is used to check whether the search item is found in the given matrix or not. • The given program is not efficient to problem 1. Because in this program, the matrix is searched completely and find out the given searched value is present in the matrix or not using "found" variable. • But in the problem 1, variable "found" is not declared and initialized. So that program cannot return the given search item is found or not. • So given program is not efficient to problem "1". After inserting the "bool found = false", it will return the search item.