Mario Rudaru
Mario Rudaru

Reputation: 19

How to swap the largest number with the last number in an array?

I'm trying to create a program that asks the user to input a number which is how many random numbers between 1-100 will be generated in an array. Then I want the largest number to be swapped with the last number and the smallest number to be swapped with the first number. Here is my code so far:

import java.util.Scanner;
import java.util.Random;

public class smallbig
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        int num = scan.nextInt();
        int[] myArray = new int[num];

        for (int i = 0; i < myArray.length; ++i) {
            int randomInt = randomGenerator.nextInt(100);
            myArray[i] = randomInt;
        }
        int smallest = myArray[0];
        int largest = myArray[0];

        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
            }
        }

        for (int j = 1; j < myArray.length; j++) {
            int first = myArray[0];
            myArray[0] = smallest;
            smallest = first;

            int temp = largest;
            int last = myArray.length;
            myArray[last - 1] = largest;
            temp = myArray[last - 1];

            System.out.print(myArray[j] + " ");
        }
    }
}

I can't seem to get the numbers to properly swap. I created a loop which determines the smallest and largest numbers from the ones generated and these are stored. Then I create a loop which performs the necessary swaps but I can't seem to get it to work properly. It works fine for swapping the largest number with the last number but most of the time(not always) the outputted last number is also present somewhere else in the array. Here is what I mean:

input: 10
output: 62 48 34 0 91 14 64 60 91

I know there is a swapper method that I could use but I want to do it by manually swapping the numbers. Any help is appreciated, thanks.

Upvotes: 0

Views: 3369

Answers (7)

S.Gohil
S.Gohil

Reputation: 3

You did well by finding smallest and largest. Issue was in swap. I refactor the swap method. May be it works.

import java.util.Scanner;
import java.util.Random;

public class smallbig
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        int num = scan.nextInt();
        int[] myArray = new int[num];

        for (int i = 0; i < myArray.length; ++i) {
            int randomInt = randomGenerator.nextInt(100);
            myArray[i] = randomInt;
        }


        for(int k=0; k < myArray.length; k++)
        {
            System.out.print(myArray[k] + " ");
        }

        System.out.println();

        int smallest = myArray[0];
        int largest = myArray[0];

        int sIndx = 0;
        int lIndx = 0;

        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
                lIndx = i;
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
                sIndx = i;
            }
        }

        System.out.println();
        System.out.println("Smallest = "+smallest);
        System.out.println("largest = "+largest);
        System.out.println("Smallest Index = "+sIndx);
        System.out.println("largest Index = "+lIndx);

        swapSmallAndLargest(num, myArray, sIndx, lIndx);

        for(int k=0; k < myArray.length; k++)
        {
            System.out.print(myArray[k] + " ");
        }


    }

    private static void swapSmallAndLargest(int num, int[] myArray, int sIndx, int lIndx) {
        int temp = 0;

        temp = myArray[sIndx];
        myArray[sIndx] = myArray[0];
        myArray[0] = temp;

        temp = myArray[lIndx];
        myArray[lIndx] = myArray[num-1];
        myArray[num-1] = temp;
    }
}

Upvotes: 0

Lord_PedantenStein
Lord_PedantenStein

Reputation: 500

i would advise against using a scanner or random values for testing since the result cannot be reproduced (at least not in an easy way). Be careful with what is an arrays index and what is the value at a specific index. This can lead to confusion very quickly. ^^

import java.util.Scanner;
import java.util.Random;



public class smallbig
{

    private static int[] myArray;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        //int num = scan.nextInt(); //skip this for testing ^^-d
        int num = 10;
        myArray = new int[num];

        for (int i = myArray.length-1; i > 0; i--) {
            //int randomInt = randomGenerator.nextInt(100);
            int randomInt = i; //use test condition that can be reproduced!
            myArray[i] = myArray.length-i;
        }
        int smallest = 0;
        int largest = 0;

        int smallestIndex = 0;
        int largestIndex = 0;

        for (int i = 0; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
                largestIndex = i;
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
                smallestIndex = i;
            }
        }

        switchIndexOfmyArray(0, smallestIndex);

        switchIndexOfmyArray(myArray.length-1, largestIndex);

        for (int j = 0; j < myArray.length; j++) {
            System.out.print(myArray[j] + " ");
        }

    }
    public static void switchIndexOfmyArray(int index, int switchWithIndex){
        int temp = myArray[index];
        myArray[index] = myArray[switchWithIndex];
        myArray[switchWithIndex] = temp;
    }
}

Yielding

0 1 8 7 6 5 4 3 2 9 

I admit i was slow on this one since i am hungry and tired xD

Happy coding! ^^

Upvotes: 0

Naveen Karthikeyan
Naveen Karthikeyan

Reputation: 77

I just tried this. Hope this helps.

public class App {
static int[] a = new int[100];
public static void main(String[] args) {
    int i;
    for(i = 0; i<a.length;i++)
        a[i] = (int)(java.lang.Math.random() * 100);

    int smallest = 0, largest = 0;

    for(i =1; i<a.length; i++){
        if(a[i] < a[smallest])
            smallest = i;
        if(a[i] > a[largest])
            largest = i;
    }

    swap(0,smallest);
    swap(a.length-1,largest);

    for(i =0; i<a.length;i++)
        System.out.print(a[i] + " ");
}

public static void swap(int i, int j){
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}
}

Upvotes: 1

Ghaith Al-Tameemi
Ghaith Al-Tameemi

Reputation: 370

you have one simple mistake, your loop should start from '0' when you perform the swap

for (int j = 0; j < myArray.length; j++) {
        int first = myArray[0];
        myArray[0] = smallest;
        smallest = first;

        int temp = largest;
        int last = myArray.length;
        myArray[last - 1] = largest;
        temp = myArray[last - 1];

        System.out.print(myArray[j] + " ");

Upvotes: 3

Tanuj Yadav
Tanuj Yadav

Reputation: 1299

Write this code

import java.util.Scanner;
import java.util.Random;

public class smallbig
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random randomGenerator = new Random();

        int num = scan.nextInt();
        int[] myArray = new int[num];

        for (int i = 0; i < myArray.length; ++i) {
            int randomInt = randomGenerator.nextInt(100);
            myArray[i] = randomInt;
        }
        int smallest = myArray[0];
        int largest = myArray[0];
        int pos1,pos2;

        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > largest) {
                largest = myArray[i];
                pos1=i;
            }
            if (myArray[i] < smallest) {
                smallest = myArray[i];
               pos2=i;
              }
        }



myArray[pos1]=myArray[myArray.length-1];
myArray[myArray.length-1]=largest;

myArray[pos2]=myArray[0];
myArray[0]=smallest;

        for (int j = 1; j < myArray.length; j++) {


            System.out.print(myArray[j] + " ");
        }
    }
}

Upvotes: 0

Omar
Omar

Reputation: 105

Instead of a loop do

//find and stores poition of small
int smallPos = myArray.indexOf(small);

//stores tge values at 0
int tempSmall = myArray[0];

//swaps the values
myArray[0] = small;
myArray[smallPos] = smallTemp;

Just repeat this with the largest value and print it using a for loop. Tell me if it works.

Upvotes: 1

nicomp
nicomp

Reputation: 4647

Your last loop doesn't do anything loopy. It just does the same thing over and over. And what it does is not correct. It is swapping the smallest into the first element but it's not moving the first element anywhere: it's gone.

You need to keep track of where the largest and smallest elements were. Your swap logic doesn't take that into consideration.

Upvotes: 0

Related Questions