004-004-008
Array Assignment: Swapping Elements
Medium
Problem Description
Array Assignment: Swapping Elements
Learning Objective: Understand array element swapping and master safe swapping using temporary variables
Overview
When swapping two elements in an array, a temporary variable (temp) is used. Direct assignment would cause the original value to be lost.
Specifications
Implement the following:
- Declare an integer array
{10, 20, 30, 40, 50} - Swap the first element (index 0) with the last element (index 4)
- Use a temporary variable
tempfor the swap - Output the swapped array in "10 20 30 40 50" format (space-separated)
Output Format
50 20 30 40 10
Hint
- Three assignment statements are needed for swapping
- Use the pattern: temp = a; a = b; b = temp;
Test Cases
※ Output examples follow programming industry standards
Input:
Expected Output:
50 20 30 40 10
Input:
Expected Output:
50 20 30 40 10
Input:
Expected Output:
50 20 30 40 10
Input:
Expected Output:
50 20 30 40 10
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 6 free executions remaining
