0

I am making a program that the user puts in their Class ID then enter the amount of Boxes sold. I am trying to get the computer in the end to tell me which of the ten classes had the most boxes. But I can't seem to figure out how to get the program to tell me which one of the ten had the most boxes. I think I need to find the biggest number in the array if that is even possible?

import java.util.*;

public class Boxs {

int ID, boxs;

public static void main(String[] args) {
    int p = 0;
    Scanner scan = new Scanner(System.in);

    Boxs[] bx = new Boxs[10];

    for (int i = 0; i <= 9; i++) {
        bx[i] = new Boxs();
        System.out.print("Enter Class ID: ");
        bx[i].ID = scan.nextInt();
        System.out.print("Enter boxs sold: ");
        bx[i].boxs = scan.nextInt();
    }

    int temp = 0;
    int temp2 = 0;
    for (int j = 0; j < 9; j++) {
        for (int h = 0; h < 9; h++) {
            if (bx[h].boxs > bx[h+1].boxs) {
                temp2 = bx[p].boxs;
                bx[h].boxs = bx[p+1].boxs;
                bx[p+1].boxs = temp;
                temp = bx[h].ID;

                bx[h].ID = bx[p+1].ID;
                bx[h+1].ID = temp2;

                System.out.println(bx[h].boxs);
                System.out.println(bx[h+1].boxs);
            }
        }
    }

    System.out.println("The Class ID with the most boxes is: " + bx[0].ID +  " and sold " +  bx[0].boxs + " boxs.");
}
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302

2 Answers2

1

Not sure if I got your question right (because your code conflicts with your statements) but you can use the following method to find out the largest number of sold boxes:

int ind = 0;
for (int j = 1; j < bx.length; j++){
    if(bx[j].boxs>bx[ind].boxs)
    {
        ind=j;
    }               
}     

System.out.println("The Class ID with the most boxes is: "
                + bx[ind].ID +  " and sold " +  bx[ind].boxs + " boxs.");
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
  • 1
    To make a better answer an explanation would be good. The algorithm starts with assuming that the first index has the most boxes (stored as ind. Then it starts checking the following indexes for more boxes. If it finds an index with more boxes the new index is assumed to have the met boxes and ind is changed to this index. When the last index has been checked, ind will contain the index with the most boxes. – Roger Lindsjö Jan 29 '12 at 09:37
0

Here is the heapshort algorythm.

/**  
 * sortiert ein Array mit heapsort
 * @param a das array
 */
private static void heapSort(int[] a) {
        generateMaxHeap(a);

        //hier wird sortiert
        for(int i = a.length -1; i > 0; i--) {
                vertausche(a, i, 0);
                versenke(a, 0, i);
        }
}

/**
 * Erstellt einen MaxHeap Baum im Array 
 * @param a das array
 */
private static void generateMaxHeap(int[] a) {
        //starte von der Mitte rückwärts.
        for(int i = (a.length / 2) - 1; i >= 0 ; i--) {
                versenke(a, i, a.length);
        }
}

/**
 * versenkt ein element im baum
 * @param a Das Array
 * @param i Das zu versenkende Element
 * @param n Die letzte Stelle im Baum die beachtet werden soll
 */
private static void versenke(int[] a, int i, int n) {
        while(i <= (n / 2) - 1) {
                int kindIndex = ((i+1) * 2) - 1; // berechnet den Index des linken kind

                //bestimme ob ein rechtes Kind existiert
                if(kindIndex + 1 <= n -1) {
                        //rechtes kind existiert
                        if(a[kindIndex] < a[kindIndex+1]) {
                                kindIndex++; // wenn rechtes kind größer ist nimm das 
                        }
                }

                //teste ob element sinken muss 
                if(a[i] < a[kindIndex]) {
                        vertausche(a,i,kindIndex); //element versenken
                        i = kindIndex; // wiederhole den vorgang mit der neuen position
                } else break;
        }
}

/**
 * Vertauscht die arraypositionen von i und kindIndex
 * @param a Das Array in dem getauscht wird
 * @param i der erste index
 * @param kindIndex der 2. index
 */
private static void vertausche(int[] a, int i, int kindIndex) {
        int z = a[i];
        a[i] = a[kindIndex];
        a[kindIndex] = z;
}

Then you can print of the first element of the shorted array. Source:Wiki

Hydroid
  • 119
  • 3
  • 16