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
sum / f.length;is integer division. Makesumafloat.descendingOrderBBSwithout executingascendingOrderSSstill gives the unwanted result, not the result in the question