0

I got an assignment that my professor has given me the code but I have to fill the answers in the assigned line to complete the code

I got with the fixed array in my problem but when I rearrange position of the numbers in my array My algorithm doesn’t work

here’s my code:

class Main {
  public static void main(String[] args) {
    int[] arr = {1, 2, 2, 2, 3, 3, 3, 3};
    int maxCnt = Integer.MIN_VALUE;
    int mem = arr[0];
    int cur = arr[0];
    int curCnt = 1;//you can modify this line with any value but it has to be curCnt =
    int mode = arr[0];
    
    int i;
    for(i=1;i<arr.length;i++) {
        cur = arr[i];
        if(cur == mem) {
            curCnt++;
        }
        else {
            if(curCnt > maxCnt) {
                mode = mem;
                maxCnt = curCnt;
            }
            /*mem = cur; you can modify in these two lines
            curCnt = 1;*/
        }
    }
    if(curCnt > maxCnt/*you can modify in this block*/) {
        mode = mem;
        maxCnt = curCnt;
    }
    System.out.printf("mode = %d%nfreq = %d", mode, maxCnt);
  }
}

The answers have to be filled in where I commented in the code

5
  • Please explain (with examples) what this code is supposed to do. Commented Jul 3 at 15:53
  • i want to print out the number which is mode and how often it appears in the array
    – doeeyes
    Commented Jul 3 at 16:03
  • 1
    If you run this with a debugger, it should become clear what the problem is. Commented Jul 3 at 16:42
  • How do I ask and answer homework questions?
    – Abra
    Commented Jul 3 at 18:21
  • I got with the fixed array in my problem but when I rearrange position of the numbers in my array My algorithm doesn’t work That's because your algorithm assumes that the array is sorted.
    – Abra
    Commented Jul 3 at 18:24

0

Browse other questions tagged or ask your own question.