# Finding Total in Arrays

Page 1 of 1

## 7 Replies - 2713 Views - Last Post: 20 October 2009 - 05:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133141&amp;s=117c35dd70baa386e3f437541abb184c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 twinkestar

• New D.I.C Head

Reputation: 0
• Posts: 33
• Joined: 17-September 09

# Finding Total in Arrays

Posted 20 October 2009 - 12:39 AM

I need to find the minimum, maximum, and total(minus the min and max) in an array of numbers that the user enters. I have to do these each using separate methods. I've figured out how to find the minimum and maximum values in the array but I cannot figure out how to find the total of the grades minus the minimum grade and the maximum grade. Please help me!!

```	private static float findTotal(float[] grades)
{
float total = 0;

for(float grade : grades)
total += grade;

return total;
}

```

How do I find the total and then subtract the minimum and maximum values found from the other methods?

This post has been edited by twinkestar: 20 October 2009 - 12:41 AM

Is This A Good Question/Topic? 0

## Replies To: Finding Total in Arrays

### #2 jinnyishere

• D.I.C Head

Reputation: 46
• Posts: 127
• Joined: 06-July 09

## Re: Finding Total in Arrays

Posted 20 October 2009 - 01:06 AM

You can have a void method that caculates the total, then a return-value method that returns the total, then in your main method you can have a for loop that determines the min and max, then you just use the return-value method subtract your min and your max.....
Was This Post Helpful? 0

### #3 TimboTheChamp

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 11-October 09

## Re: Finding Total in Arrays

Posted 20 October 2009 - 01:09 AM

Couldn't you just find the total of the array and then subtract the values of the minimum and maximum or did you want to remove those values from the array?
Was This Post Helpful? 0

### #4 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Finding Total in Arrays

Posted 20 October 2009 - 01:13 AM

You're going to kick yourself.

```    private static float findTotal(float[] grades)
{
float total = 0;

for(float grade : grades)
total += grade;

float min = findMin(grades);
float max = findMax(grades);
// or whatever you called your min and max methods.

return total - min - max;
}
```

Was This Post Helpful? 0

### #5 AntonWebsters

• D.I.C Regular

Reputation: 89
• Posts: 428
• Joined: 15-August 09

## Re: Finding Total in Arrays

Posted 20 October 2009 - 04:50 AM

Here's an idea...why don't you use Array.sort() method to sort out the integers in your array? Then, the first integer would be the minimum, while the last integer would be the maximum. As for the total, declare a new integer variable with value of 0, and loop through the array, increasing it with the integers in the array at the same time.
Was This Post Helpful? 0

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12318
• Posts: 45,417
• Joined: 27-December 08

## Re: Finding Total in Arrays

Posted 20 October 2009 - 06:15 AM

@Anton: Why spend between n and n^2 steps sorting the collection when you can just iterate once?

Let's create the global variables min and max which can be accessed in the findTotal() method. This is what I mean:
```class MyArrays{
private static int max = 0,min = 0;

public float findTotal(float[] x){
float total = 0f;
for(float i:x){
if(i < min) min = i;
if(i > max) max = i;
total += i;
}
return total;
}
}

```

Was This Post Helpful? 0

### #7 twinkestar

• New D.I.C Head

Reputation: 0
• Posts: 33
• Joined: 17-September 09

## Re: Finding Total in Arrays

Posted 20 October 2009 - 04:56 PM

Thanks so much for everyone's help. I think I'm gonna go with cfoley's example since it seems the easiest. But I am curious about Array.sort(), how exactly would I do one of those? Also how would I do using the Math class?

This post has been edited by twinkestar: 20 October 2009 - 04:58 PM

Was This Post Helpful? 0

### #8 cfoley

• Cabbage

Reputation: 2388
• Posts: 5,013
• Joined: 11-December 07

## Re: Finding Total in Arrays

Posted 20 October 2009 - 05:43 PM

```float getTotal(float[] array) {
Arrays.sort(array);
// Now the array is sorted, min is the first and max is the last.
// We can just miss these out when we add up.
float total = 0;
for(int i = 1; i < array.length - 1; i++) {
total += array[i];
}
return total;
}

```

One disadvantage of this is that it alters the array by sorting it, and that behaviour might screw up something else in the program. As macosxnerd101 pointed out, it's also potentially slow. However, under normal circumstances you're not going to notice the difference. And I mean you'd have to call the method thousands of times with sizeable arrays to get a measurable difference.

Change macosnerd101's return line to return total-max-min; and you've got a method that does it running through the array just once. A downside is that you already wrote methods to find the max and min, and now you're repeating that functionality again, giving you two sets of identical code to maintain and debug.

My way (and I AM biased ) reuses the code you've already written and tested. Whatever could go wrong, it's unlikely to be the max and min values. You could argue it goes through the array three times (once for the total, the max and the min) so is wasteful. However, this is going to be very fast regardless. You will waste more time making it more efficient than the computer will ever spend executing my inefficient version.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }