0

In my java program, I am trying to display a table of two single arrays in descending order. I have managed to display it in both ascending and descending order. However, there is an additional array element -1 in my terminal. The -1 can be seen in the picture attached below.

-1 element inside array

This is my attempt so far:

import java.util.*;

public class Q2_Frequency {

    public static void main(String[] args) {

        int sum = 0, mean, temp;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the number of days: "); //take input from user
        int n = input.nextInt();

        int a[] = new int[n];
        int b[] = new int[n];
        int c = 0;

        System.out.println("Please enter number of trucks using a road over the " + n + " day period: ");
        for (int i = 0; i < n; i++) { //input into array
            a[i] = input.nextInt();
            sum = sum + a[i];
        }
        mean = sum / n;
        System.out.println("The mean is: " + mean); // calculate mean of n day period
        System.out.println("Sorted in ascending order");
        System.out.println("Input\tFrequency");//print table in ascending order
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = i + 1 ; j < n ; j++)
            {
                if (a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            c = 1;
            if (a[i] != 1) {
                for (int j = i + 1; j < n; j++) {
                    if (a[i] == a[j]) {
                        c = c + 1;
                        a[j] = -1;
                    }
                }
                b[i] = c;
            }
        }

        for (int i = 0; i < n; i++) {
            if (a[i] != -1)
            {
                System.out.println(a[i] + "\t\t\t"+ b[i]);
            }
        }

        System.out.println("Sorted in descending order");
        System.out.println("Input\tFrequency");//print table in ascending order
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = i + 1 ; j < n ; j++)
            {
                if (a[i] < a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;

                }
            }
            System.out.println(a[i] + "\t\t\t"+ b[i]);
        }

        ArrayLength(a);

    }

    private static void ArrayLength(int []array) // method to count number of inputs
    {
        if (array==null)
        {
            System.out.println("Number of input is 0.");
        }
        else
        {
            int arrayLength = array.length;
            System.out.println("Number of input is: "+arrayLength);
        }

    }
}

Does anyone have an idea why the -1 appears only in the descending order and why?

  • ok the reason the -1 appears is because when you have the same number of trucks in diferent days this if a[i] == a[j] adds +1 to frequency and then replaces it with -1. The first one doesn't show the -1 because when you are using the print you have this if a[i] != -1 witch skips the -1 but when you are printing after descending you are missing that if statement and you are also missing the desceding order of the frequency – Anon Dec 03 '22 at 10:13
  • I have tried to repeat the code which calculates the frequency. However, it keeps on repeating 1 as the frequency for the descending order – StudentOfLife Dec 03 '22 at 10:35
  • if you already have the frequency calculated then then only thing that you have to do is reverse the order. You can use the A array has a guide for the B array – Anon Dec 03 '22 at 10:40
  • I have tried using the same method as A array but could not get the desired output, could you give me an example? – StudentOfLife Dec 03 '22 at 11:44

1 Answers1

1

I refactor your code a bit but this fixes the issues you are having

public static void main(String[] args) {
    int sum = 0, mean = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter the number of days: ");
    int n = input.nextInt();
    int[] a = new int[n];
    int[] b = new int[n];
    System.out.println("Please enter number of trucks using a road over the " + n + " day period: ");
    for (int i = 0; i < n; i++) {
        a[i] = input.nextInt();
        sum += a[i];
    }
    if (n != 0) mean = sum / n;
    System.out.println("The mean is: " + mean);
    printHeaders("Sorted in ascending order");
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            sort(a[i] > a[j], a, b, i, j);
        }
    }
    calculateFrequency(n, a, b);
    printValues(n, a, b);
    printHeaders("Sorted in descending order");
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            sort(a[i] < a[j], a, b, i, j);
        }
    }
    printValues(n, a, b);
    ArrayLength(a);
}
private static void calculateFrequency(int n, int[] a, int[] b) {
    for (int i = 0; i < n; i++) {
        int c = 1;
        for (int j = i + 1; j < n; j++) {
            if (a[i] != a[j]) continue;
            c = c + 1;
            a[j] = -1;
        }
        b[i] = c;
    }
}
private static void printValues(int n, int[] a, int[] b) {
    for (int i = 0; i < n; i++) {
        if (a[i] != -1) System.out.println(a[i] + "\t\t\t" + b[i]);
    }
}
private static void sort(boolean statement, int[] a, int[] b, int i, int j) {
    if (!statement) return;
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;

    temp = b[i];
    b[i] = b[j];
    b[j] = temp;
}
private static void printHeaders(String stringOrder) {
    System.out.println(stringOrder);
    System.out.println("Input\tFrequency");
}
private static void ArrayLength(int[] array) {
    System.out.println("Number of input is: " + array.length);
}

this method is for sorthing the a and b array at the same time

private static void sort(boolean statement, int[] a, int[] b, int i, int j) {
    if (!statement) return;
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;

    temp = b[i];
    b[i] = b[j];
    b[j] = temp;
}

this one if for print the values of the 2 arrays

private static void printValues(int n, int[] a, int[] b) {
    for (int i = 0; i < n; i++) {
        if (a[i] != -1) System.out.println(a[i] + "\t\t\t" + b[i]);
    }
}
Anon
  • 385
  • 2
  • 6