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

  1. Create an array of that size.

  2. Read in that many numbers from the user (they will all be unique).

  3. Find the smallest number.

  4. Swap the smallest number with whatever is in the first cell of the array.

  5. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!