Question
Smallest Swap This exercise will require the searching and manipulation of an array using loops. The program will read in a positive integer nn from
Smallest Swap
This exercise will require the searching and manipulation of an array using loops.
The program will read in a positive integer nn from input. You should then add code to
-
Create an array of that size.
-
Read in that many numbers from the user (they will all be unique).
-
Find the smallest number.
-
Swap the smallest number with whatever is in the first cell of the array.
-
Print out the numbers in the array in order each on a new line.
To keep things clean, there will be no additional prompts or print outs for this one.
For example, if 3 was entered, an array of size 3 should be created. If then the numbers 4, 5, 2 where entered, the program should print out
2
5
4
import java.util.Scanner;
public class SmallestSwap { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //Let the looping begin! (and declare the array) //While this can be done in two loops, //if you're not sure, do the three //steps that require loops as separate loops. //Always remember, working is better than //perfect. } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started