Home > Back-end >  Bus stops - number of people that get on and off. Arrays 2D [closed]
Bus stops - number of people that get on and off. Arrays 2D [closed]

Time:10-01

I'm having difficulties solving this exercise. I appreciate any help. I was able to start, and another community gave some tips, but I'm still confused about how to proceed.

EXERCISE:

  • I've to count how many passengers there're at the last stop.

  • For that, I have a 2D array where each position represents the passengers that get on and off the bus.

  • Example:

    • [6,0] [8,2] [1,2].
    • 1st stop: 6 passengers get on.
    • 2nd stop: 8 passengers get on and 2 get off.
    • 3rd stop: 1 passenger get on and 2 get off.
    • There're 11 passengers at the last stop.
  • I should count first the passengers that leave the bus, later the ones that enter. "Let people out of the bus then thou can enter"

  • With two error messages:

    • When on the 1st stop, passages try to leave.
    • When more people try to leave than the total that there're.

CODE:

    public static int finalBUS(int[][] stops) {
        int paxEntering = 0;
        int paxLeaving = 0;
        if (stops[0][1] > 0) {
            System.out.println("[ERROR] No passenger can get off at the first stop!!");
            return -1;
        }
        for (var i = 0; i < stops.length; i  ) {

            if (stops[paxEntering][0] < stops[paxLeaving][1]) {
                System.out.println("[ERROR] Stop number "   stops  ":No more passengers can get off the bus("   stops[i][1]   ") than there are!! ("   stops[i][0]   ")");
                return -1;

                }
            return i;
            }
        }

TESTS provided:

    @Test
    @Order(1)
    void testfinalBUS() {
        assertEquals(19, PEx2.finalBUS(new int[][] { {7,0}, {8,2}, {9,5}, {3,1} }));
        assertEquals(-1, PEx2.finalBUS(new int[][] { {7,0}, {1,6}, {2,6}, {3,1} }));
        assertEquals(1, PEx2.finalBUS(new int[][] { {7,0}, {1,6}, {0,2}, {1,0} }));
        assertEquals(-1, PEx2.finalBUS(new int[][] { {5,1}, {1,6}, {0,2}, {1,0} }));
        assertEquals(10, PEx2.finalBUS(new int[][] { {5,0}, {1,5}, {2,1}, {10,2} }));
        assertEquals(2, PEx2.finalBUS(new int[][] { {3,0}, {2,3}, {2,1}, {1,2} }));

        assertEquals(7, PEx2.finalBUS(new int[][] { {7,0} }));
        assertEquals(0, PEx2.finalBUS(new int[][] { {7,0}, {0,7} }));
        assertEquals(7, PEx2.finalBUS(new int[][] { {7,0}, {7,7} }));
        assertEquals(7, PEx2.finalBUS(new int[][] { {7,0}, {0,0} }));
        assertEquals(0, PEx2.finalBUS(new int[][] { {0,0}, {1,0}, {0,1} }));

        assertEquals(-1, PEx2.finalBUS(new int[][] { {7,0}, {0,8} }));
        assertEquals(-1, PEx2.finalBUS(new int[][] { {7,0}, {7,8} }));
    }

Sorry for bothering you. Thank you.

CodePudding user response:

I think the issue is in this if statement.

for(var i = 0; i < stops.length; i  ){
     if (stops[paxEntering][0] < stops[paxLeaving][1]) {
           System.out.println("[ERROR] Stop number "   stops  ":No more 
                passengers can get off the bus("   stops[i][1]   ") than 
                there are!! ("   stops[i][0]   ")");
           return -1;
     }
}

you never update the values of paxEntering and paxLeaving so they are always 0 so you are always checking stops[0][0] and stops[0][1]. Surely this is meant to be the i variable like in the error message.

Furthermore, your function seems to have to output the total number of passengers on the bus at the final stop buy you never seem to calculate this you just return i which is the length of the list essentially. so you would need to create a variable that you store the number of passengers on the bus in. then return this instead of i.


Edit

int numberOfPassengers = 0;
for(var i = 0; i < stops.length; i  ){
     if ((numberOfPassengers   stops[i][0]) < stops[i][1]) {
           System.out.println("[ERROR] Stop number "   i  ":No more 
                passengers can get off the bus("   stops[i][1]   ") than 
                there are!! ("   (numberOfPassengers   stops[i][0])   
                ")");
           return -1;
     }else{
           numberOfPassengers  = (stops[i][0] - stops[i][1])
     }
}

return numberOfPassengers;

This tracks the number of passengers on the bus and allows you to check if there are more people leaving the bus than there are on the bus. Before you had the issue that if 6 people got on at the first stop, and then at the next stop no one got on but 4 people got off you would have had an error.

  • Related