8 Replies - 5737 Views - Last Post: 14 June 2010 - 08:25 AM Rate Topic: -----

#1 drfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 10

remove elements from array

Posted 14 June 2010 - 05:12 AM

Hi,

I have to create an array from a txt file of numbers, removed the highest number and lowest number and then average the rest.
So far I've been able to get the text file to the array, I've sorted it with array.sort but I can't work out how to remove the highest and lowest elements form the array.
Is there an easy way of doing this?

Cheers
Is This A Good Question/Topic? 0
  • +

Replies To: remove elements from array

#2 jbpercyii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 09

Re: remove elements from array

Posted 14 June 2010 - 05:36 AM

Can we see your code so we know what you already have?
You cannot remove elements from an array because they are of a fixed size.
Can you use another Collection Type other than an Array?
Was This Post Helpful? 0
  • +
  • -

#3 drfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 10

Re: remove elements from array

Posted 14 June 2010 - 05:49 AM

I have to read the file to an array. Is it possible to create another array that excludes the highest and lowest figures if I can't delete elements.
Here is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string fileString = File.ReadAllText("scores.txt");
            
          string[] aFieldString = fileString.Split('\n');
          Array.Sort(aFieldString);
           
          listBox1.Items.AddRange(aFieldString);
            double[] arrDouble = new double[aFieldString.Length];
            for (int i = 0; i < aFieldString.Length; i++)
            {
                arrDouble[i] = Convert.ToDouble(aFieldString[i]);
                //Array.Sort(arrDouble);
            }
            
            //avarage calc
            double avrageDouble = 0.0;
            double sumInteger = 0.0;
            for (int i = 0; i < arrDouble.Length; i++)
            {
                sumInteger = sumInteger + arrDouble[i];
            }
            avrageDouble = sumInteger / arrDouble.Length;
            averageLabel.Text = avrageDouble.ToString();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,382
  • Joined: 04-October 09

Re: remove elements from array

Posted 14 June 2010 - 06:08 AM

I'd ask your instructor if they literally mean "create an array without the min/max" or if they mean "take the average but don't include the min/max value".
Was This Post Helpful? 0
  • +
  • -

#5 drfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-June 10

Re: remove elements from array

Posted 14 June 2010 - 06:18 AM

is there a way of not including those values into the average?

This post has been edited by drfire: 14 June 2010 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 jbpercyii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 09

Re: remove elements from array

Posted 14 June 2010 - 06:22 AM

Yes, you can create another array to house the results, if you know the size you are going to need.

Question, If there are two values that are the same for the lowest (or highest) do you exclude both? For example, If your sorted array looks like
1, 1, 4, 5, 6, 7, 10, 10 should your result set be 1, 4, 5, 6, 10? or 4, 5, 6, 7?

If your double array is already sorted highest to lowest with Array.Sort(array), then you know the lowest value is at array[0] and your highest value is at array[array.length - 1].

string[] aFieldString = fileString.Split('\n');
Array.Sort(aFieldString);
           
listBox1.Items.AddRange(aFieldString);
double[] arrDouble = new double[aFieldString.Length];
for (int i = 0; i < aFieldString.Length; i++)
{
  arrDouble[i] = Convert.ToDouble(aFieldString[i]);
               
}
Array.Sort(arrDouble);

//Do this if you need to remove multiple instances of the highest and lowest values.
List<double> resultSet = new List<double>();
foreach(double d in arraDouble){
  if((d > arrDouble[0]) && (d < arrDouble[arrDouble.Length - 1])){
    resultSet.Add(d); 
  }
}
arrDouble = resultSet.ToArray();


//do this if you only need to remove one instance of the highest and lowest;
double[] resultSet = new double[arrDouble.Length - 2]();
for(int i = 1; i< arrDouble.Length; i++){
 resultSet[i-1] = arrDouble[i];
}
arrDouble = resultSet;

averageLabel.Text = arrDouble.Average().ToString(); //I believe .Average() is built into the Array class, could be wrong though.


Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: remove elements from array

Posted 14 June 2010 - 07:00 AM

Just so you know, you can't remove items from an array. You either have to create a new array and move the items that you want to keep to the new array, or use something else....like a generic list, which does allow you to remove items.

This post has been edited by eclipsed4utoo: 14 June 2010 - 07:00 AM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: remove elements from array

Posted 14 June 2010 - 07:54 AM

Move the contents of index 1 into index 0
Move the contents of index 2 into index 1
and so on. In other words, a loop that shifts everything downward.
Then use Array.Resize to lower the upper limit by -2 since you are moving the top and bottom
Done
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,462
  • Joined: 08-June 10

Re: remove elements from array

Posted 14 June 2010 - 08:25 AM

View Postdrfire, on 14 June 2010 - 04:49 AM, said:

I have to read the file to an array. Is it possible to create another array that excludes the highest and lowest figures if I can't delete elements.
Here is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string fileString = File.ReadAllText("scores.txt");
            
          string[] aFieldString = fileString.Split('\n');
          Array.Sort(aFieldString);
           
          listBox1.Items.AddRange(aFieldString);
            double[] arrDouble = new double[aFieldString.Length];
            for (int i = 0; i < aFieldString.Length; i++)
            {
                arrDouble[i] = Convert.ToDouble(aFieldString[i]);
                //Array.Sort(arrDouble);
            }
            
            //avarage calc
            double avrageDouble = 0.0;
            double sumInteger = 0.0;
            for (int i = 0; i < arrDouble.Length; i++)
            {
                sumInteger = sumInteger + arrDouble[i];
            }
            avrageDouble = sumInteger / arrDouble.Length;
            averageLabel.Text = avrageDouble.ToString();
        }
    }
}



I think some people are making this harder than it has to be. If you have already sorted the array, and you want to exclude the first and last value from the sum and average, just modify your loop:

for (int i = 1; i < arrDouble.Length - 1; i++)
{
    sumInteger = sumInteger + arrDouble[i];
}
avrageDouble = sumInteger / (arrDouble.Length - 2);
averageLabel.Text = avrageDouble.ToString();


Starting at index 1 and stopping at Length-1 will exclude the first and last values. Then to get the average, you divide by length - 2, because you removed 2 values from the sum.

One thing that confuses me is that you have a variable: sumInteger. But it's a double! Don't needlessly confuse yourself like that.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1