Click here to Skip to main content
15,891,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a multidimensional array with basically a key / value pair. I would like to sum all the values according to the key then pick the min and max values from this.
I tried making a List<keyvaluepair> and a dictionary to do this I got very close but it wouldn’t let me sort through a list of doubles. My lists and arrays are created correctly without issues. I would like to display the min/max of value and also display the key in the same line
C#
public static void PrintTable()
        {
            string text;
            var totals = new List<KeyValuePair<string, double>>();
            for(int i = 0; i < topics.Length; i++)
            {
                Console.Write(topics[i]  + " ");
                double num = 0;
                for(int j = 0; j < 10; j++)
                {
                    Console.Write(responses[i, j] + " ");
                    num += responses[i, j] * (j + 1);
                    totals.Add(new KeyValuePair<string, double>( topics[i], num));
                }
                average = num / userCounter;
                Console.Write("\t" + average.ToString("##.00"));
            }
            // Display highest points and lowest points.
            var addedSums = totals.GroupBy(k => k.Key).ToDictionary(a => a.Key, b => b.Sum(c => c.Value));
            foreach(var pair in addedSums.Values.OrderByDescending())
            {
                Console.WriteLine("Highest points: " + pair);
            }


What I have tried:

using arrays/lists/tuples/dictionarie/keyvaluepairs and more linq and many other ways to try this i thought it would of been simple
Posted
Updated 26-Dec-18 1:40am

Hi,

just update below code. cheking for non zero value here. [from line num 18 in previouse code]

List<KeyValuePair<string, double>> totals = new List<KeyValuePair<string, double>>()
                {
                   new KeyValuePair<string, double>("Key1", 10),

                   new KeyValuePair<string, double>("Key3", 0),
                   new KeyValuePair<string, double>("Key2", 15),
                   new KeyValuePair<string, double>("Key4", 13),
                   new KeyValuePair<string, double>("Key5",3)
               };

           double min = 0; double max = 0;
           string maxKey = "", minKey = "";
           foreach (KeyValuePair<string, double> kvp in totals)
           {
               //textBox1.Text +=string.Format("Key: {0} Value: {1} \n", kvp.Key, kvp.Value);
               if (max < kvp.Value)
               {
                   maxKey = kvp.Key;
                   max = kvp.Value;
               }
           }
           min = max;
           foreach (KeyValuePair<string, double> kvp in totals)
           {
               if (kvp.Value != 0) //(kvp.Value > 0)
               {
                   if (min > kvp.Value)
                   {
                       minKey = kvp.Key;
                       min = kvp.Value;
                   }
               }
           }
           textBox1.Text += string.Format("maxKey:{0} max: {1} minkey:{2} min: {3} \n", maxKey, max, minKey, min);
>

**Note: Mark as Answer if found useful.
 
Share this answer
 
List<KeyValuePair<string, double>> totals = new List<KeyValuePair<string, double>>()
                 {
                    new KeyValuePair<string, double>("Key1", 10),
                    new KeyValuePair<string, double>("Key2", 15),
                    new KeyValuePair<string, double>("Key3", 5),
                    new KeyValuePair<string, double>("Key4", 13)
                };

            double min = 0; double max = 0;
            string maxKey = "", minKey = "";
            foreach (KeyValuePair<string, double> kvp in totals)
            {
                if (max < kvp.Value)
                {
                    maxKey = kvp.Key;
                    max = kvp.Value;
                }
            }
            min = max;
            foreach (KeyValuePair<string, double> kvp in totals)
            {
                if (min > kvp.Value)
                {
                    minKey = kvp.Key;
                    min = kvp.Value;
                }
            }
            textBox1.Text += string.Format("maxKey:{0} max: {1} minkey:{2} min: {3} \n", maxKey, max, minKey, min);

**Note: I have written output to textbox instead of Console

Mark answer if found useful.
 
Share this answer
 
v2
Comments
TheBigBearNow 20-Dec-18 17:35pm    
This works! But now i have 1 small thing left. I want to exclude '0' So if it selects 0 from my list I want it to go to the next smallest number.
min cannot be 0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900