0

I have a homework where I'm supposed to prompt the user to enter five numbers and arrange them from min to max and since we didn't take arrays

I'm only left with Math.min & Math.max or if statement.

I wrote a code where the first, second and last number are always correct But I can't seem to figure out how to do the 3rd and 4th number

Here's an example:

    if (a <= b && a <= c && a <= d && a <= e) {
        System.out.println("Numbers in Ascending order "
                + a + " " +Math.min(Math.min(b, c), Math.min(d, e)) +
                " " + "?" +
                " " + "?" +
                " " +Math.max(Math.max(b, c), Math.max(d, e)));
    }

If you know any idea that might help me solve this task?

clxo
  • 1
  • 4

4 Answers4

2

Here is one possible solution

public static void main(String... args) {
    int a = 32;
    int b = 42;
    int c = 2;
    int d = 88;
    int e = 92901;
    int counter = 0;
    while (counter < 5) {
        int currentMin = findMin(a, b, c, d, e);

        // Printing smallest number yeat
        System.out.print(currentMin + " ");

        if (a == currentMin){
            a = Integer.MAX_VALUE;
        }

        if (b == currentMin){
            b = Integer.MAX_VALUE;
        }

        if (c == currentMin){
            c = Integer.MAX_VALUE;
        }

        if (d == currentMin){
            d = Integer.MAX_VALUE;
        }

        if (e == currentMin){
            e = Integer.MAX_VALUE;
        }
        counter++;
    }
}

private static int findMin(int a, int b, int c, int d, int e) {
    int smallest = Math.min(a, Math.min(b, Math.min(c, Math.min(d, e))));
    return smallest;
}

Notice how I am using the Integer.MAX_VALUE to remove the smallest number yeat for example in the first iteration 2 will be returned which is equals to c now I have to make the code somehow to ignore c in the next iteration because it was already used if I was using the Integer object i could have set c to be null but int cannot be setted to null so what i can do is to set it to number so large that findMin function would never choose it again that's why I use MAX_VALUE

urag
  • 1,228
  • 9
  • 28
2

If you have to do it using if,else if statements then you will have to write one if statement and 119 else if statements i.e. number of ways in which 5 numbers can be arranged. 5!=120.

if you are allowed to use for loop check this link

NAK
  • 478
  • 2
  • 9
0
int aux;
if (b < a){
    aux = b; b = a; a = aux;
}
if (c < a){
    aux = c; c = b; b = a; a = aux;
}
else{
    if (c < b){
        aux = c; c = b; b = aux;
    }
}
...

I guess you get the idea, in the end a will be the smallest and e will be the biggest

For more information, since it looks like you're getting started to programming and sorting algorithms, this is called Insertion Sort (I believe). More information here https://en.wikipedia.org/wiki/Insertion_sort

Shinra tensei
  • 1,283
  • 9
  • 21
0

This is a possible answer. Since loop and arrays cannot be used, much of the code is repetition. In the end a,b,c,d,e contain the values arranged from min to max. Hope this helps.

import java.io.*;

class SortFiveElements {

    //utility function
    //returns index as a:1, b:2, c:3, d:4, e:4
    public static int find_min_index(int a,int b, int c,int d, int e, int min)
    {
        return a==min?1:(b==min?2:(c==min)?3:(d==min?4:5));
    }

    public static void main (String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        int c = Integer.parseInt(br.readLine());
        int d = Integer.parseInt(br.readLine());
        int e = Integer.parseInt(br.readLine());

        //temp is a temporary var to store the i-th value which may get replaced
        //smallest stores the min value among i-th to 5th element
        //idx stores the minimum value's index
        int temp,smallest,idx;

        //i=1,  i.e element 'a'
        //temp has value of 1st element that is a
        //find minimum among 5 elements in 'smallest', its index in 'idx'
        //then swap
        temp = a;
        smallest = Math.min(a,Math.min(b,Math.min(c,Math.min(d,e))));
        idx = find_min_index(a,b,c,d,e,smallest);
        a = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        //i=2,  i.e element 'b'
        //temp has value of 2nd element that is b
        //find minimum among 4 elements in 'smallest', its index in 'idx'
        //NB: a already has the smallest value, so replace a with MAX_VALUE while finding index
        //then swap
        temp = b;
        smallest = Math.min(b,Math.min(c,Math.min(d,e)));
        idx = find_min_index(Integer.MAX_VALUE,b,c,d,e,smallest);
        b = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        //repeat above process for 'c' and 'd'.
        //'e' will automatically fall in place
        temp = c;
        smallest = Math.min(c,Math.min(d,e));
        idx = find_min_index(Integer.MAX_VALUE,Integer.MAX_VALUE,c,d,e,smallest);
        c = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        temp = d;
        smallest = Math.min(d,e);
        idx = find_min_index(Integer.MAX_VALUE,Integer.MAX_VALUE,
                             Integer.MAX_VALUE,d,e,smallest);
        d = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        //we have the values in sorted order in a,b,c,d,e
        System.out.println(a+" "+b+" "+c+" "+d+" "+e);
    }
}
Deepayan Ghosh
  • 185
  • 2
  • 9