How could I implement a loop to add values of my array; rather than add them manually? (Cant think outside the box)

Answered How could I implement a loop to add values of my array; rather than add them manually? (Cant think outside the box)

  • 2012年5月1日 下午 01:33
     
      包含代碼

    Basically I will have more methods that will all use "num2" thats why I used the HoldArray to clean up the Main Method...

    So basically in the Max method I want to use some kind of loop to add all of the "storeMax" indexes to gather rather than do it all manually, but I dont know how that possible? Please help me free my brain from thinking "inside the box" Here is the code:

    public static void HoldArray(double[,] Data)
            {
                Max(Data);
            }
    
            static void Main(string[] args)
            {
                double[,] num2 = new double[7, 2] { { 61.1, 81.1 }, { 62.2, 82.2 }, { 63.3, 83.3 }, { 64.4, 84.4 }, { 65.5, 85.5 }, { 66.6, 86.6 }, { 67.7, 87.7 } };
                HoldArray(num2);
                Console.ReadLine();
            }
    
            public static void Max(double[,] Data)
            {
                double[] storeMax = new double[7]; //stores all highvalues in a single dimensional array//
                
                for (int i = 0; i < 7; i++) //this loop puts all high values of Data(num2↑) into storemax//
                {
                    storeMax[i] = (Data[i, 1]);
                    //put/populate all of data(num2) [i(global 0,1,2,3,4,5,6,7), index1's(high temps)] into storeMax//
                }
                double average = ((storeMax[0] + storeMax[1] + storeMax[2] + storeMax[3] + storeMax[4] + storeMax[5] + storeMax[6]) /7);
                Console.WriteLine(average);
            }

所有回覆

  • 2012年5月1日 下午 01:44
     
     已答覆

    Create a list, then use List.AddRange() method to add values to this list then use List.ToArray() to get the array needed.

  • 2012年5月1日 下午 01:48
     
     已答覆 包含代碼

    You should use either a for loop or a foreach loop to access each object in an array.

    for(int i = 0; i < storeMax.Length; i++)
    {
      double value = storeMax[i];
      //do something with value
    }
    
    foreach(double value in storeMax)
    {
      //do something with value
    }

    You can also use LINQ to get the Max/Average from a collection of data.  Just add a 'using System.Linq' to the top of the page.

    var maxValues = num2.Select(innerArray => innerArray.Max());
    
    double average = maxValues.Average();

  • 2012年5月1日 下午 05:27
     
     已答覆 包含代碼

    Hi Hoff;

    If you are using Visual Studio 2010 you can use Linq to Object to get what you need.

    public static void Max(double[,] Data)
    {
        var average = (from row in Enumerable.Range(0, Data.GetLength(0))
                       select Data[row, 1] ).Average();
                        
        Console.WriteLine(average);
    }

    this should do what you need.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • 2012年5月1日 下午 05:55
     
     
    Thank you all for your suggestions, but unfortunately my professor will most likley not accept LINQ because we have not gone over it yet. But I will definantly try ro learn LINQ now