006-005-011
break/continue: Labeled Loop Control
Hard
Problem Description
break/continue: Labeled Loop Control
In this problem, you will create a program that searches for a value read from standard input in a 3x4 two-dimensional array using labeled break in nested loops and displays the found coordinates to standard output.
Learning Objective: Understand how to use labels to specify the target of break/continue in nested loops
Overview
A regular break terminates the innermost loop, but using labels allows direct termination of outer loops. This is useful for two-dimensional array searches and complex loop processing.
Specifications
Create a program that searches for a specific value in a 2D array and displays the found coordinates:
- Initialize a 3x4 two-dimensional array with these values:
- Row 0: {1, 2, 3, 4}
- Row 1: {5, 6, 7, 8}
- Row 2: {9, 10, 11, 12}
- Read one integer from standard input and assign it to
target - Label the outer loop with
outer: - Display "Searching [row][col]" during search
- When found, display "Found at [row][col]" and exit both loops with labeled break
Input Format
target (the integer to search for)
Output Format (when input is 7)
Searching [0][0]
Searching [0][1]
Searching [0][2]
Searching [0][3]
Searching [1][0]
Searching [1][1]
Searching [1][2]
Found at [1][2]
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up