0

I am writing a program that should output an array, the elements of the array reversed, the sum of the array, the average of the array, min and max of array, and the array sorted (ascending by selection sort and descending by bubble sort). Everything seems to check out, BUT the module descendingOrderBBS()'s output is incorrect. This is my code so far:

import java.util.Scanner;
import java.lang.String;


public class Main
{

public static String input;
public static int n;

public static void main(String[] args)
{
    int[] file1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int[] file2 = new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    int[] file3 = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};

    inputData();

    switch (input)
    {
        case "file1":
        {
            System.out.println("original array: ");
            printArray(file1);

            System.out.println(" ");

            System.out.println("reversed array: ");
            reverseArray(file1);

            System.out.println(" ");

            System.out.println("The sum of all elements: " + sum(file1));

            System.out.printf("The average of all elements: %.1f%n", average(file1));

            max(file1);

            min(file1);

            //System.out.printf("Array in ascending order (Selection Sort): %-10s", ascendingOrderSS(file1));

            ascendingOrderSS(file1);

            System.out.println(" ");

            descendingOrderBBS(file1);

            //System.out.printf("Array in ascending order (Bubble Sort): %-10s", ascendingOrder(file1));

            break;
        }
        case "file2":
        {
            System.out.println(file2);
            break;
        }
        case "file3":
        {
            System.out.println(file3);
            break;
        }
        default :
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("File not found, try again: ");
            inputData();

            break;
        }
    }

}

private static String inputData()
{
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Please input file: ");
    input = keyboard.nextLine();

    return input;
}


private static void printArray(int f[])
{
    for (int i = 0; i < f.length; i++)
    {
        System.out.printf("%-10s", f[i]);
    }

}

private static void reverseArray(int f[])
{
    int end = f.length - 1;

    for (int start = 0; start < end; start++, end--)
    {
        int temp = f[start];
        f[start] = f[end];
        f[end] = temp;
    }

    for (int i = 0; i < f.length; i++)
    {
        System.out.printf("%-10s", f[i]);
    }

}

private static int sum(int f[])
{
    int sum = 0;

    for (int i = 0; i < f.length; i++)
    {
        sum += f[i];
    }

    return sum;
}

private static float average(int f[])
{
    float average = 0;

    int sum = 0;

    for (int i = 0; i < f.length; i++)
    {
        sum += f[i];
    }

    average = sum / f.length;

    return average;
}

private static void max(int f[])
{
    int largest = f[0];

    for (int i = 0; i < f.length; i++)
    {
        if (f[i] > largest)
        {
            largest = f[i];
        }
    }

    System.out.println("Max: " + largest);
}

private static void min(int f[])
{
    int smallest = f[0];

    for (int i = 0; i < f.length; i++)
    {
        if (f[i] < smallest)
        {
            smallest = f[i];
        }
    }

    System.out.println("Min: " + smallest);
}

private static void descendingOrderBBS(int f [])
{
    int i = 0;
    int j = 0;
    int temp = 0;

    for (i = 0; i < f.length; i++)
    {
        for (j = i + 1; j < f.length; j++)
            if (f[i] < f[j])
            {
                temp = f[i];
                f[i] = f[j];
                f[j] = temp;
            }
    }
    System.out.println("Array in descending order (Bubble Sort): ");

    for (int x = 0; x < f.length; x++)
    {
        System.out.printf("%-10s", f[x]);
    }
}

private static void ascendingOrderSS( int f [])
{
    int a = 0;
    int b = 0;
    int minIndex = 0;

    for (a = 0; a < f.length - 1; a++)
    {
        minIndex = a;

        for (b = a + 1; b < f.length; b++)
        {
            if (f[b] < f[minIndex])
            {
                minIndex = b;
            }
        }

        int temp = f[minIndex];
        f[minIndex] = f[a];
        f[a] = temp;
    }

    System.out.println("Array in ascending order (Selection Sort): ");

    for (int x = 0; x < f.length; x++)
    {
        System.out.printf("%-10s", f[x]);
    }
  }

}

Here is the whole program run together

Please input file: file1
original array: 
1         2         3         4         5         6         7         8         9                10         
reversed array: 
10        9         8         7         6         5         4         3         2         1          
The sum of all elements: 55
The average of all elements: 5.0
Max: 10
Min: 1
Array in ascending order (Selection Sort): 
1         2         3         4         5         6         7         8         9         10         
Array in descending order (Bubble Sort): 
2         1         3         4         5         6         7         8         9         10        
Process finished with exit code 0

I've tried changing the variables to a/b instead of i/j. I made all the modules private to see if that would make a difference, but nothing I've tried has done a thing. Oh and I've also tried using different IDEs. This is the output with both sorting modules together:

 ascendingOrderSS(file1);

 System.out.println(" ");

 descendingOrderBBS(file1);

Array in ascending order (Selection Sort): 
1         2         3         4         5         6         7         8         9         10         
Array in descending order (Bubble Sort): 
2         1         3         4         5         6         7         8         9         10        
Process finished with exit code 0

Output with ascending module ignored:

//ascendingOrderSS(file1);

System.out.println(" ");

descendingOrderBBS(file1);


Array in descending order (Bubble Sort): 
10        9         8         7         6         5         4         3         2         1         
Process finished with exit code 0

I'm not sure on why descendingOrderBBS() is changing. I've been up till 5:12am finishing this last project.

All fixed:

Array in ascending order (Selection Sort): 
1         2         3         4         5         6         7         8            9         10         
Array in descending order (Bubble Sort): 
10        9         8         7         6         5         4         3         2         1         
Process finished with exit code 0
11
  • 3
    sum / f.length; is integer division. Make sum a float. Commented Dec 6, 2018 at 13:19
  • 1
    Thanks!! Average works perfect now :) Any ideas about the modules ?? Commented Dec 6, 2018 at 13:24
  • Of course it affects, in your sorting algorithms you are modifying your input array in both of them. Sort over a copy, without changing your original one. And I notice your Bubblesort is clearly wrong and needs to be repaired. Commented Dec 6, 2018 at 13:30
  • @RafaelPalomino True although the point of a method that sorts an array is that it sorts it in the correct order no matter what order it was previously in. Commented Dec 6, 2018 at 13:31
  • Also I cannot reproduce this issue, only executing descendingOrderBBS without executing ascendingOrderSS still gives the unwanted result, not the result in the question Commented Dec 6, 2018 at 13:32

1 Answer 1

1

For your first issue why it's printing 5.0 and not 5.5 is because sum / f.length is a division of integers and the value after the . will be dropped, however turning sum into float or casting sum to float will solve this issue.

Your current code (this prints 1.0):

float average = 3 / 2;
System.out.println(average);

The fix (this prints 1.5):

float sum = (float)3 / 2;
System.out.println(sum);

3 and 2 are the variables sum and f.length in your code.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I knew that part would be an easy fix, but any ideas on the sorting modules??
@N.Page I cannot reproduce that issue, when I'm only calling descendingOrderBBS the output is still 2 1 3 4 5 6 7 8 9 10
I'm not sure why, but descendingOrderBBS works fine by itself like how i copy/pasted it, at least for me. It doesn't work for me when it's with ascendingOrder. When its with the other module is when i get this bad result. But apparently everyone thinks the bubble sort has issues, so i must be wrong. I wish i knew what they were lol!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.