The dictionary part isnt working. can anybody help??

string path = @"C:\1\";
            string[] file1_Lines = File.ReadAllLines(path + @"TraceSet.csv");
            string file2 = File.ReadAllText(path + @"SBoxOutput.csv");
            string[] file2_Lines = file2.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
            
           //creating 2 lists for average values of file1 and file2:
            List<float> averageFile1 = new List<float>();
            List<float> averageFile2 = new List<float>();

            //filling list1 - with average calculation of the file1 (row average):
            foreach (string line in file1_Lines)
            {
                string[] columns = line.Split(';');
                float fAvg = 0f;
                for (int i = 1; i < columns.Length; i++)
                {
                    float value = Convert.ToSingle(columns[i]);
                    fAvg += value;
                }
                //get the average and add to list:
                fAvg = fAvg / (columns.Length - 1);
                averageFile1.Add(fAvg);
            }

            //filling list2 - with average calculation of the file2 (column average):
            //creating a dictionary collection for get the sum of values in the columns
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < file2_Lines.Length; i++)
            {
                string[] lineValues = file2_Lines[i].Split(';');
                for (int j = 0; j < lineValues.Length; j++)
                {
                    int intValue = Convert.ToInt32( lineValues[j]);
                    if (dic.ContainsKey(j))
                        dic = dic.ToDictionary(d => d.Key, d => d.Key == j ? d.Value + intValue : d.Value);
                    else
                        dic.Add(j, intValue);
                }
            }
            
            //get the average into the list2 from the dictionary:
            int rows = file2_Lines.Length;
            foreach (KeyValuePair<int, int> _value in dic)
            {
                averageFile2.Add(_value.Value / rows);
            }

            //
            //
            //subtract the average of row 1 from the average of column 1 and reapeat for each row and column 
            //
            List<float> newList = new List<float>();
            for (int i = 0; i < averageFile2.Count; i++)
            {
                for (int j = 0; j < averageFile1.Count; j++)
                {
                    if (j == i)
                    {
                        float _newValue = averageFile2[i] - averageFile1[j];
                        newList.Add(_newValue);
                        break;
                    }
                }
            }

            //"newList" now holds the values you wanted!!!

Thank You

What is line 35 trying to do? Why not dic[j] += intValue ?

What is line 35 trying to do? Why not dic[j] += intValue ?

yeh that seems to have worked!thanks

but it isnt recognising ToDictionary.It says:

Error 1 'System.Collections.Generic.Dictionary<int,int>' does not contain a definition for 'ToDictionary'

do you know how to fix this?

ToDictionary is a LINQ extension method and I don't see any reason to use it here.

ToDictionary is a LINQ extension method and I don't see any reason to use it here.

OK I REPLACED THAT. I JUST CANT GET THIS WORKING.IT NOW SAYS:

int intValue = Convert.ToInt32( lineValues[j]); -Input string was not in a correct format.

Can you get it working???

OK I REPLACED THAT. I JUST CANT GET THIS WORKING.IT NOW SAYS:

int intValue = Convert.ToInt32( lineValues[j]); -Input string was not in a correct format.

Can you get it working???

namespace differenceofmeans
{
    public class TraceSet
    {
        public static void Main(string[] args)
        {

            string[] file1_Lines = File.ReadAllLines(@"C:\Users\40025634\Desktop\Project\TEST FILE.csv");
            string file2 = File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TEST FILE2.csv");
            string[] file2_Lines = file2.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
            
           //creating 2 lists for average values of file1 and file2:
            List<float> averageFile1 = new List<float>();
            List<float> averageFile2 = new List<float>();

            //filling list1 - with average calculation of the file1 (row average):
            foreach (string line in file1_Lines)
            {
                string[] columns = line.Split(',');
                float fAvg = 0f;
                for (int i = 1; i < columns.Length; i++)
                {
                    float value = Convert.ToSingle(columns[i]);
                    fAvg += value;
                }
                //get the average and add to list:
                fAvg = fAvg / (columns.Length - 1);
                averageFile1.Add(fAvg);
            }

            //filling list2 - with average calculation of the file2 (column average):
            //creating a dictionary collection for get the sum of values in the columns
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < file2_Lines.Length; i++)
            {
                string[] lineValues = file2_Lines[i].Split(',');
                for (int j = 0; j < lineValues.Length; j++)
                {
                    int intValue = Convert.ToInt16(lineValues[j]);
                    
                    if (dic.ContainsKey(j))
                        {
                        dic[j] += intValue;
                        }
                    else
                        dic.Add(j, intValue);
                }
            }
            
            //get the average into the list2 from the dictionary:
            int rows = file2_Lines.Length;
            foreach (KeyValuePair<int, int> _value in dic)
            {
                averageFile2.Add(_value.Value / rows);
            }

            //
            //
            //subtract the average of row 1 from the average of column 1 and reapeat for each row and column 
            //
            List<float> newList = new List<float>();
            for (int i = 0; i < averageFile2.Count; i++)
            {
                for (int j = 0; j < averageFile1.Count; j++)
                {
                    if (j == i)
                    {
                        float _newValue = averageFile2[i] - averageFile1[j];
                        newList.Add(_newValue);
                        Console.Write(_newValue);
                        break;
                    }
                }
            }

            //"newList" now holds the values!!!
            }
        }
    }

This was the code I did for him, and I used a Linq there (dont know why, just spontaniously I guess, even if your way is good the same way - just to assign new value to the appropriate key).

Is it working now? It surely was working before too. I tested it.

This was the code I did for him, and I used a Linq there (dont know why, just spontaniously I guess, even if your way is good the same way - just to assign new value to the appropriate key).

Is it working now? It surely was working before too. I tested it.

no its still not working! It says:

int intValue = Convert.ToInt32( lineValues[j]);

Input string was not in a correct format.

Now you habe to figue out what exactly do you get into some variables. Of course you will get this error, if the value is not an integer (full number, with no decimals, and no string at all).

The code we will provide you, it will never work for 100%, until you will not understand the concept of the code. Until then, its worthless. There will always be something that it will interupt your code.
I cannot help you here, especiually because the code I gave you, it WAS WORKING 100%, and now you are saying its not working. Well then its not working. Find out why its not working. Spend some 10s of hours on the project and find out what is the matter. You are the one who has to understand the code here, and faster you will get it, better you you will be - believe me.

So, back to your issue:
As said. the value that is now in the "lineValues[j]" it is NOT an integer. You find out how its possible that some other type of value came into it. It surely is not my exmaple code you gave me, otherwise this wouldn`t have happened.

no its still not working! It says:

int intValue = Convert.ToInt32( lineValues[j]);

Input string was not in a correct format.

Got it working thanks. Now just to plot it on a graph!!!

Now you habe to figue out what exactly do you get into some variables. Of course you will get this error, if the value is not an integer (full number, with no decimals, and no string at all).

The code we will provide you, it will never work for 100%, until you will not understand the concept of the code. Until then, its worthless. There will always be something that it will interupt your code.
I cannot help you here, especiually because the code I gave you, it WAS WORKING 100%, and now you are saying its not working. Well then its not working. Find out why its not working. Spend some 10s of hours on the project and find out what is the matter. You are the one who has to understand the code here, and faster you will get it, better you you will be - believe me.

So, back to your issue:
As said. the value that is now in the "lineValues[j]" it is NOT an integer. You find out how its possible that some other type of value came into it. It surely is not my exmaple code you gave me, otherwise this wouldn`t have happened.

yeh it wasnt splitting the strings because it was searching for the ; rather than a , which is used in a csv file! thank you!

Yes, my csv file was slitted with semicolon. Im really glad you find the cause of the problem. This is only best for you, because this way you will learn the most. And do not forget to use Breakpoints (always), if you dont use them already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.