
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
Edition 2ISBN: 978-0131409095
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
Edition 2ISBN: 978-0131409095 Exercise 1
Write the following unstructured program segment in structured form (but different from those given in Problems 2 and 3):
?int row = 0, col; A: col = 0; if (col n) goto B; goto A; B: if (row n) goto C; goto E; C: if (mat[row][col] == item) goto D; col++; if (col n) goto B; row++; goto A; D: cout "item found\n"; goto F; E: cout "item not found\n"; F:;
?int row = 0, col; A: col = 0; if (col n) goto B; goto A; B: if (row n) goto C; goto E; C: if (mat[row][col] == item) goto D; col++; if (col n) goto B; row++; goto A; D: cout "item found\n"; goto F; E: cout "item not found\n"; F:;
Explanation
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
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
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
Why don’t you like this exercise?
Other Minimum 8 character and maximum 255 character
Character 255