0

I'm a beginner at java and was wondering on how to find the maximum difference between numbers inputted into a for loop. My program takes x amount of odometer readings (Between consecutive trips) from a car, e.g 100km, 150km, 400km, and is supposed to take the maximum distance traveled for all trips, which in this example would be 250km, as well as the minimum which would be 50km and average distance traveled between each odometer reading.

So far i've only managed to find a way to calculate the biggest value and smallest value for each odometer reading, given by the variables maximum and minimum, however have no idea on how to approach or begin to program finding the difference between trips. I tried implementing an array of some sort (not shown in this code) but i keep receiving too many errors. I could really use some advice on how to approach this problem or some insight; it would be greatly appreciated. Thank you for your time.

System.out.print("Input number of trips: ");    
carSample.numberOfTrips = input.nextInt();


    int maximum = Integer.MIN_VALUE;
    int minimum = Integer.MAX_VALUE;
    int total = 0;

for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("Odometer reading " + (i + 1) + ": ");
    int odometerReading = input.nextInt();
    total += odometerReading;
    if (odometerReading > maximum){
        maximum = odometerReading;
        }
    if (odometerReading < minimum){
        minimum = odometerReading;  
    }
3
  • I may have misunderstood you, but if the only value you still want to calculate is the average distance between readings, you could do (maximum-minimum)/carSample.numberOfTrips Commented Apr 9, 2020 at 8:33
  • Hello, you want to calculate min difference and max difference between consecutive elements of arrays like [100, 150, 400] , so always already sorted ? Commented Apr 9, 2020 at 8:38
  • Have a variable previousReading, initially 0, and substract it from the odometerReading to get the difference for each reading, then find the maximum difference just as you did for the reading. BTW, why do you need the maximum reading? Isn't it just the last reading? Commented Apr 9, 2020 at 9:30

2 Answers 2

1
int previous = 0;
int minimumTrip = Integer.MAX_VALUE;
int maximumTrip = Integer.MIN_VALUE;

for (int i = 0; i < carSample.numberOfTrips; i++) {
    System.out.print("Odometer reading " + (i + 1) + ": ");
    int odometerReading = input.nextInt();
    int currentTrip = odometerReading - previous;
    if (currentTrip > maximumTrip){
        maximumTrip = currentTrip;
        }
    if (currentTrip < minimumTrip){
        minimumTrip = currentTrip;  
    }
    previous = odometerReading;
}

If reading starts not from 0 consider previous = first odometer reading

Sign up to request clarification or add additional context in comments.

2 Comments

this actually works really well! thank you so much however i still can't figure out how to start my loop from 0. I tried assigning previous = first odometer reading but i'm not sure if i executed it correctly because it is not working. Thanks otherwise!
you should assign first reading before loop then start looping from 1
0

If you want to calculate the maximum and minimum values of each mileage, the initial values of both are 0, and the maximum and minimum values are updated every time a mileage value is read, and all the mileage values can be obtained by looping .

If you want to calculate the average of all mileage values, then add up all the values divided by the number of values For example, av = (a + b + c) / 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.