Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:19:01+00:00 2026-06-09T14:19:01+00:00

How do I create a loop to generate min, max, avg for 2 array

  • 0

How do I create a loop to generate min, max, avg for 2 array lists, i have only generated the min, max and avg with sum for single array lists so far.

These are the 2 arrays User[] & Withdrawals[]:

User, Withdrawals
1 , 90.00
2 , 85.00
4 , 75.00
5 , 65.00
2 , 40.00
1 , 80.00
3 , 50.00
5 , 85.00
4 , 80.00
1 , 70.00

size = 10

This is what i have tried, as i have no clue about 2 arrays interdependent:

double min = 0.0;
double max = 0.0;
double sum = 0.0;
double avg = 0.0;

for(int i = 0; i <size; i++){
.
.
for(int j = 0; j < Withdrawals.length; j++){
   if(Withdrawals[User[i]] > max){  
      max = Withdrawals[j];  
   }  
   if(Withdrawals[User[i]] < min){  
      min = Withdrawals[j];  
   }
}  
sum += Withdrawals[j];
avg = sum/size;
}

how do i print the min, max, avg from the no of withdrawals per user ? :S

I have already counted the number of withdrawals per user.

Conditions are: create everything from scratch instead of using available library features of Java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:19:02+00:00Added an answer on June 9, 2026 at 2:19 pm

    Divide and conquer 🙂
    Yes, I know that is a term used for an algorithm technique, in this case what I mean is… work with small parts.

    First having the min, max, avg for a simple array:

    double[] values = {2,3,4,5,6,7};
    
    double min = values[0];
    double max = values[0];
    double sum = 0;
    
    for (double value : values) {
         min = Math.min(value, min);
         max = Math.max(value, max);
         sum += value;
    }
    
    double avg = sum / values.length;
    
    System.out.println("Min: " + min);
    System.out.println("Max: " + max);
    System.out.println("Avg: " + avg);
    

    Note: Since you can’t use Java libraries for your assignment, is easy to do your own versions of the min/max functions (read the Math JavaDoc)

    Now you can encapsulate this code in a function, you can start by returning another array:

    static double[] minMaxAvg(double[] values) {
        double min = values[0];
        double max = values[0];
        double sum = 0;
    
        for (double value : values) {
            min = Math.min(value, min);
            max = Math.max(value, max);
            sum += value;
        }
    
        double avg = sum / values.length;
    
        return new double[] {min, max, avg};
    }
    
    public static void main(String[] args) {
        double[] values = {2,3,4,5,6,7};
        double[] info = minMaxAvg(values);
        System.out.println("Min: " + info[0]);
        System.out.println("Max: " + info[1]);
        System.out.println("Avg: " + info[2]);
    }
    

    Using an array is a little bit ugly to read, so is better if you create a class to hold the min, max, avg. So lets refactor the code a little bit:

    class ValueSummary {
        final double min;
        final double max;
        final double avg;
    
        static ValueSummary createFor(double[] values) {
            double min = values[0];
            double max = values[0];
            double sum = 0;
    
            for (double value : values) {
                min = Math.min(value, min);
                max = Math.max(value, max);
                sum += value;
            }
    
            double avg = sum / values.length;
    
            return new ValueSummary(min, max, avg);
        }
    
        ValueSummary(double min, double max, double avg) {
            this.min = min;
            this.max = max;
            this.avg = avg;
        }
    
        public String toString() {
            return "Min: " + min + "\nMax: " + max +"\nAvg: " + avg;
        }
    }
    
    
    public static void main(String[] args) {
        double[] values = {2,3,4,5,6,7};
        ValueSummary info = ValueSummary.createFor(values);
        System.out.println(info);
    }
    

    You don’t specify it in your question, but I assume that you have an array for each user (maybe each withdrawals is another array).
    Now that you have the bottom parts, we can switch to a top-down thinking.

    So your code could be something like this:

    for (User aUser : users) {
         System.out.println("User: " + aUser);
         System.out.println(ValueSummary.createFor(withdrawalsOf(aUser)));
    }
    

    Ok, but this is just the idea, you still have the problem to relate aUser with its withdrawals. You have several options here:

    1. Make a “table” User-> Withdrawals, that is what you are trying to do with the two arrays. The User index in the array acts like a “user id”. When you learn about Map you will see that you can use a better representation for the index.
    2. Having a Map or array is just an optimization, of the relationship User->Withdrawls, but you can represent that relationship with an object (ie UserWithdrawls)

    Option 1:

    static class User {
        final String name;
        public User(String s) { name = s; }
    }
    public static void main(String[] args) {
        User[] users = { new User("John"), new User("Doe")};
        double[][] withdrawals = {
             new double[] { 1, 2, 3}, new double[] { 10,22, 30} 
        };
        for (int i = 0; i < users.length; i++) {
            System.out.println("User: " + users[i].name);
            System.out.println(ValueSummary.createFor(withdrawals[i]));
        }
    }
    

    Option 2:

    static class User {
        final String name;
        public User(String s) { name = s; }
    }
    static class UserWithdrawls {
        final User user;
        final double[] withdrawals;
        final ValueSummary summary;
        UserWithdrawls(User user, double[] withdrawals) {
            this.user = user;
            this.withdrawals = withdrawals;
            this.summary = ValueSummary.createFor(withdrawals);
        }
    }
    public static void main(String[] args) {
        UserWithdrawls[] userWithdrawls = {
                new UserWithdrawls(new User("John"), new double[] { 1, 2, 3}),
                new UserWithdrawls(new User("Doe"), new double[] { 10, 22, 30})
        };
        for (UserWithdrawls uw : userWithdrawls) {
            System.out.println("User: " + uw.user.name);
            System.out.println(uw.summary);
        }
    }
    

    Additional notes: If you are studying Computer Science, you’ll learn in the future that the loop to calculate max, min, avg has a complexity of O(n). If the values array is fully loaded in memory, doing the max/min/avg in three different functions (thus reading the array 3 times) is still an algorithm of O(n) order with a bigger constant. With the power of today’s computers the constant is so small, that most of the time you’ll not get any gain from calculating min/max/avg in the same loop. In contrast you can gain code readability, for example in Groovy the minMaxAvg code could be written like this:

     def values = [2,3,4,5,6,7];
     println values.min()
     println values.max()
     println values.sum() / values.size()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use a loop to create 10 of these, incrementing
I am trying to dynamically generate content using JSP. I have a <c:forEach> loop
I have two arrays a={a,b,c,d} b={1,2,3,4} now I want to create a 3rd array
I am trying the create a loop to extract data from an array created
I have a for loop to generate a row of div s, my code
I want to create a single shape file from multiple mxd's that have multiple
I'm trying to iteratively generate some functions using a For Loop: # Create a
I have to make a loop to generate a 5 randomly-picked-letter string, and then
I want to create a loop that will iterate over several strings, but unable
I would like to create a loop that names the variables based on i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.