0

So I have to input a list of numbers stored in an array, specify how many int values that are to be stored in the array, and print out the original array plus the list of integers with the maximum and minimum values swapped.

I am not allowed to use library other than java.util.Scanner

So, if I input {1 2 3 4 5 6 7}, I'm supposed to get {7 2 3 4 5 6 1}. Everything in my code works except the swapping part, but I've managed to find the maximum and minimum values, I just don't know how to deal with them. Here's what I got:

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter array size (minimum size 7)? ");
        int size = in.nextInt();
        int [] array = new int [size];
        System.out.print("Enter " + size + " distinct integer values to store in the array: ");
        
        int i = 0;
        for (i = 0; i < size; i++) {
            array[i] = in.nextInt();
        }
            System.out.println();
            System.out.println("Original array:");
        
        for (i = 0; i < size; i++) {
            System.out.print(array[i]);
            System.out.print("\t");
        }
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Array after swapping the smallest and largest:");
        
        int large = array[0];
        int small = array[0];
        for (i = 0; i < size; i++) {
            if (array[i] > large) {
                large = array[i];
            }
        }
        for (i = 1; i < size; i++) {
            if (array[i] < small) {
                small = array[i];
            }
        }
        
        int temp = small;
        large = large;
        large = temp;
        
        for (i = 0; i < size; i++) {
            System.out.print(array[i]);
            System.out.print("\t");
        }
    
        
    }
4
  • Your issue is int big = 0; and this loop for (i = 0; i < size; i++) { you never actually change the value of big anywhere, so it just stays at 0 so when you swap [0] for [0] using temp it remains as 1. Rethink that loop, and assign big correctly any you will solve your issue. Commented Mar 22, 2022 at 5:11
  • @sorifiend Okay I fixed the part where I find the smallest and largest values. The "big" part was a mistake I think it should work now, but it still prints the same array. Thoughts? Commented Mar 22, 2022 at 5:18
  • The answer from Bohemian below will solve your issues, specifically array[small] = i should change to small = i as well as your other change where array[big] = i should change to big = i Commented Mar 22, 2022 at 5:19
  • After the array values have been entered by User: Arrays.sort(array); int tmp = array[array.length - 1]; array[array.length - 1] = array[0]; array[0] = tmp;. Commented Mar 22, 2022 at 5:27

2 Answers 2

1

Your main bug is here:

int small = 0;

should be

int small = Integer.MAX_VALUE;

But that alone won't fix your program. You also need to fix these:

  • for (i = 1; i < size; i++) should loop from 0, not 1.
  • if (array[small] < array[i]) should be if (array[small] > array[i])
  • if (array[big] > array[i]) should be if (array[big] < array[i])
  • array[small] = i should be small = i
  • array[big] = i should be big = i

I recommend replacing your code altogether with:

int small = Arrays.stream(array).min().getAsInt();
int big = Arrays.stream(array).max().getAsInt();
Sign up to request clarification or add additional context in comments.

2 Comments

The thing here is, I'm not allowed to use any other library than java.util.Scanner. I did try to fix my code to find the largest and smallest values though. Thoughts?
@Dandouni if you apply the changes I have detailed, it should work. The fact that most of your code has bugs tells me you are not debugging your code. One of the best skills you can learn is how to do that. I recommend you use your IDE's debugger and step through your code to find your bugs - you would be amazed at how fast you will find and fix problems all by yourself.
0
import java.util.*;

public class Solution {
    public static void swapMinMax(int arr[]){
        int minIdx = 0, maxIdx = 0, min = arr[0], max = arr[0];
        for(int i = 0; i < arr.length; i++){
            if(min < arr[i]){
                min = arr[i];
                maxIdx = i;
            }else if(max > arr[i]){
                max = arr[i];
                minIdx = i;
            }
        }
        for(int i = 0; i < arr.length; i++){
            if(i == minIdx){
                arr[i] = min;
            }else if(i == maxIdx){
                arr[i] = max;
            }
            System.out.print(arr[i] + " ");
        }
    }

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int ln = scn.nextInt();
        int arr[] = new int [ln];
        for(int i = 0; i < arr.length; i++){
            arr[i] = scn.nextInt();
        }
        swapMinMax(arr);
    }
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.