yyzzer1234
yyzzer1234

Reputation: 153

How to change position of the elements in an array

I'm writing a program, that take 10 Integers from a keyboard and list them in an array indexed from 0-9, and reports the position of the lowest number in the array. If the lowest number has any other position than 0, then the program is supposed to switch the position of the lowest number input with the number in the first position in the array:

import java.util.Scanner;

public class q35 {

    public static void main(String args[]) {

        Scanner tastatur = new Scanner(System.in);

        int[] helTall = new int[10];
        int input;

        int lowest = Integer.MAX_VALUE;

        for(int i=0;i<helTall.length;i++) {

            System.out.println("Integers? ");
            input = tastatur.nextInt();  

            if (input < lowest) {
                lowest = input;
            }

            helTall[i] = input;

        }

        for (int i = 0; i < helTall.length; i++) {
            helTall[0] = lowest;
            System.out.println(helTall[i]);
        }

        System.out.println("Lowest number is " + lowest);
    }
}

The only problem is that instead of changing position with the lowest number with the number at helTall[0], it just completely replaces the first number in the sequence helTall[0] with the lowest Integer, that way if my input is 4 5 63 23 6 -4 7 33 23 99, then the output becomes -4 5 63 23 6 -4 7 33 23 99 (as you can see the first input number is completely erased), but it should have been -4 5 63 23 6 4 7 33 23 99 any tips/advice/solutions? Thanks in advance.

Upvotes: 1

Views: 345

Answers (3)

Jim W
Jim W

Reputation: 512

You need a separate variable to store what is in the bottom of the array before you overwrite it with your new lowest input number.

helTall[0]=lowest;

should be proceeded by

int placeholder = helTall[0]

and followed by

hellTall[i] = placeholder;

This way, you'll end up swapping the two array elements, the first swapped with the lowest. Is this what you're trying to accomplish? This leaves most of the array unsorted.

Upvotes: 0

Eran
Eran

Reputation: 394146

You should keep track of the index of the lowest number (each time you write lowest = input; you should add lowestIndex=i;.
Then helTall[lowestIndex] will be the lowest number.
So you swap helTall[lowestIndex] with helTall[0] instead of just overwriting the value of helTall[0].

I thought it was enough to describe the solution in words, but I guess it wasn't...

int lowest = Integer.MAX_VALUE;
int lowestIndex = 0;
for(int i=0;i<helTall.length;i++){
  System.out.println("Integers? ");
  input = tastatur.nextInt();  
  if (input < lowest){
    lowest = input;
    lowestIndex = i;
  }
  helTall[i]=input;
}
// swap the numbers
if (lowestIndex > 0) {
  int temp = lowest;
  helTall[lowestIndex] = helTall[0];
  helTall[0] = temp;
}
// display output
for (int i = 0; i < helTall.length; i ++) {
  System.out.println(helTall[i]);
}

Upvotes: 1

Xabster
Xabster

Reputation: 3720

This part is wrong:

for (int i = 0; i < helTall.length; i ++) {
    helTall[0]=lowest;
    System.out.println(helTall[i]);
}

First, you do not need to repeatedly (10 times) put lowest into helTall[0]. Let's move that outside first so it's only done once:

helTall[0]=lowest;
for (int i = 0; i < helTall.length; i ++) {
    System.out.println(helTall[i]);
}

Next, the line we put outside the loop overwrites the helTall[0] without regard for what is already in there. We need to temporarily save that number in there elsewhere, then overwrite the spot, so that we can use it to overwrite the spot where the lowest number was:

int numberAtLocationZero = helTall[0];
helTall[0]=lowest;
// Now, on this line we need to write helTall[lowestNumberIndex] = numberAtLocationZero;
for (int i = 0; i < helTall.length; i ++) {
    System.out.println(helTall[i]);
}

In the above code I wrote a comment. It relies on you either knowing where in the array the lowest number was, or to find it again. If you add a new variable to your code that takes in values called lowestNumberIndex and update that each time you detect a new lowest number, then you're pretty much done if you uncomment my comment.

Upvotes: 0

Related Questions