7 Replies - 501 Views - Last Post: 14 February 2012 - 10:05 AM Rate Topic: -----

Topic Sponsor:

#1 mosdojaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 12

Replacing the lowest and highest array element with the average C++

Posted 07 February 2012 - 12:24 PM

I can find highest and lowest values, and calculate the array average. Now I need to replace the highest and lowest value with the average. I am wondering what would be the correct direction to go to assign this value.



#include <iostream>

using namespace std;

int main()
{
// variables and array size

  const int SIZE =10;
  int array[SIZE];
  int outlierBig;
  int outlierSmall;
  double averageReadout;

  for (int i = 0; i< SIZE;i++){ // simulate sensor data stored in an array


        array [i] =i;

  }

   outlierSmall =outlierBig = array[0];

   //remove bigest/smallest value from array
   for (int i = 1; i <SIZE; i++){


   if (array[i] > outlierBig)
   outlierBig = array[i];

   if (array[i] < outlierSmall)
   outlierSmall = array[i];

   }

   //calculate average
    for (int i = 1; i <SIZE; i++){
   averageReadout +=array[i];
    averageReadout = (averageReadout-outlierBig-outlierSmall)/(SIZE-2);


  
    return 0;
}
}



thank you for looking

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing the lowest and highest array element with the average C++

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 187
  • View blog
  • Posts: 505
  • Joined: 12-July 09

Re: Replacing the lowest and highest array element with the average C++

Posted 07 February 2012 - 12:40 PM

Instead of storing the values of the highest and lowest elements, store their indices, so you can modify them after the loop.

You also never initialize averageReadout, so it has an undefined value. When you calculate the average, you have to start at index 0.
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 123
  • Joined: 08-July 08

Re: Replacing the lowest and highest array element with the average C++

Posted 07 February 2012 - 05:41 PM

You don't need to remove biggest or smallest from the array.
When you iterate through the array include a statement like:
if(array[i]==biggest || array[i]== smallest)array[i]==average;

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • D.I.C. Regular
  • member icon

Reputation: 548
  • View blog
  • Posts: 3,105
  • Joined: 09-June 09

Re: Replacing the lowest and highest array element with the average C++

Posted 07 February 2012 - 08:59 PM

Calculate the average before hand. Loop through the array and find the indices for max and min elements and set those elements to the average.
Was This Post Helpful? 0
  • +
  • -

#5 mosdojaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 12

Re: Replacing the lowest and highest array element with the average C++

Posted 14 February 2012 - 09:22 AM

Thanks i should have time this weekend to work on my home project some more. I will repost with what I come up with.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Replacing the lowest and highest array element with the average C++

Posted 14 February 2012 - 09:28 AM

Since calculating the average requires iterating through the array I'd find the high and low indices in the same loop.
Was This Post Helpful? 0
  • +
  • -

#7 mosdojaf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-February 12

Re: Replacing the lowest and highest array element with the average C++

Posted 14 February 2012 - 09:35 AM

I do need to remove min and max before average. I am treating the min and max as sensor errors. From using the after market sensors for lego mindstorms their are jumps and dips in the data they return. I am trying to componsate for this.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Replacing the lowest and highest array element with the average C++

Posted 14 February 2012 - 10:05 AM

You can loop through the entire array and get a sum along with the highest and lowest indices. Then all you need to do is subtract the highest and lowest values from the sum before calculating the average. Still no need for more than one loop!

Oh, and don't forget to set the highest and lowest to the average after the loop!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1