Hello im a newbie having problems.

I have got two arraylist with values in them. I want to subtract each value from the oposite value in the second array list to get a new list C which i am going to plot on a graph. For example
ArrayLists A,B

A - B C
0.564 - 0.356 = 0.??

Im trying to do this for each value in the list.

Any Ideas would be grate full. So far my code is as follows:

using System;

namespace differenceofmeans
{
    public class TraceSet
    {
        public static void Main(string[] args)
        {
            string file = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TraceSet.csv");

            ArrayList rows = new ArrayList(file.Split('\n'));
            {

                foreach (string row in rows)
                {
                    string r = row.Remove(row.Length - 1, 1);

                    ArrayList rowVals = new ArrayList(r.Split(','));

                    float sum = 0;
                    float addedItems = 0;

                    foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

                    if (addedItems > 0)
                    {
                        float avrg = sum / addedItems;
                        ArrayList avrgvalues = new ArrayList();
                        avrgvalues.Add(avrg);

                        Console.Write(avrg);
                    }

                    string filetwo = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\SboxOutput.csv");

                    ArrayList columns = new ArrayList(filetwo.Split('\n'));
                    {

                        foreach (string column in columns)
                        {
                            string c = column.Remove(column.Length - 1, 1);

                            ArrayList columnVals = new ArrayList(c.Split(','));

                            float sumcolumn = 0;
                            float addedItems1 = 0;

                            foreach (string item in columnVals)
                            {
                                try
                                {
                                    float num = float.Parse(item);
                                    sumcolumn += num;
                                    addedItems1++;
                                }
                                catch (Exception)
                                {
                                    //throw;
                                }
                            }

                            if (addedItems > 0)
                            {
                                float avrg1 = sum / addedItems;
                                ArrayList avrg1values = new ArrayList();
                                avrg1values.Add(avrg1);

                                Console.Write(avrg1);
                            }
                        }

                        Console.ReadKey();

                    }
                }
            }
        }
    }
}

Recommended Answers

All 19 Replies

You type float (if you have decimals), or decimal:

float a = 0.564F;
 float b = 0.356F;
 float c = a - b;

You type float (if you have decimals), or decimal:

float a = 0.564F;
 float b = 0.356F;
 float c = a - b;

Yes but i have two long array lists and i want it to read through both of them and subtract all the values from each other. Float a and b arnt constants they keep changing.

Does that make any sence? sorry

It doesnt matter if are constant or not. As long as the value is a type of float (or decimal, or even some other math type), you can do marh operations over them.
So you only have to get two varibles, and do a substraction over them. New value will be saved into an array list (or where ever you want it).

It doesnt matter if are constant or not. As long as the value is a type of float (or decimal, or even some other math type), you can do marh operations over them.
So you only have to get two varibles, and do a substraction over them. New value will be saved into an array list (or where ever you want it).

How do i load each value of the array list to the variable A and B to do the equation?

Im not sure where would you like to put the calulated value, but this is how to be done, and like you already did it:

foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

You have to get the other value (let say "num" is value A), value B. And the you subtract one from antoher. The value you will get, put into a new arraylist.

Btw, I know you have no idea how to do it, it would be best to show me how the csv file is made, so I can show you on the exmple code.

try this once

float[] A = { 5.5F, 6.8F, 9.3F, 10.9F };
            float[] B = { 6.9F, 8.9F, 7.2F, 12.10F };
            foreach (float val in A)
            {
                foreach (float val1 in B)
                {
                    float[] resultlist = new float[5];
                    for (int i = 0; i < A.Length; i++)
                    {
                        resultlist[i] = val-val1;
                    }
                }
            }

Im not sure where would you like to put the calulated value, but this is how to be done, and like you already did it:

foreach (string item in rowVals)
                    {
                        try
                        {
                            float num = float.Parse(item);
                            sum += num;
                            addedItems++;
                        }
                        catch (Exception)
                        {
                            //throw;
                        }
                    }

You have to get the other value (let say "num" is value A), value B. And the you subtract one from antoher. The value you will get, put into a new arraylist.

Btw, I know you have no idea how to do it, it would be best to show me how the csv file is made, so I can show you on the exmple code.

Thank You for your help so far. mmmmm il copy and paste a bit of the two files in here:

Trace Set.csv
Tr0 0 -0.0025 -0.0025 -0.0025 -0.00375
Tr1 0 0 0 -0.00125 0
Tr2 0 0 0 0.00125 0
Tr3 0 0 0.00125 0 0
Tr4 0 0 0 -0.00125 0.00125
Tr5 0 0 0 0 0
Tr6 0 0.00125 0.00125 0 0.00375

SboxOutput
99 124 119 123
124 99 123 119
119 123 99 124
123 119 124 99
242 107 111 197
107 242 197 111
111 197 242 107

What im trying to do is:

Load the traceset values
Add the values in the rows and find the average for each row.

Load the SboxOutput file
Add the values in the columns and find the average.

then subtract the average of row 1 from the average of column 1
and reapeat for each row and column
(ie. row 2 average - column 2 average, row 3 average - column 3 average)

Then when i have all these values im going to load them into a graph.

Not a good example, sorry. Why? No need to explaining.

then subtract the average of row 1 from the average of column 1
and reapeat for each row and column...

From which row, and from which column? Please be more specific!!!

float[] A = { 5.5F, 6.8F, 9.3F, 10.9F };
            float[] B = { 6.9F, 8.9F, 7.2F, 12.10F };
            foreach (float val in A)
            {
                foreach (float val1 in B)
                {
                    float[] resultlist = new float[5];
                    resultlist[i] = val-val1;
                }
            }

Instead of the above mentioned code, try this it will solve out, like the above u load the row avg values into arraylist like given A and B and do the above coding.

From which row, and from which column? Please be more specific!!!

i dont really understand what you mean. I have posted a sample of the csv file above.

add the values in each row find the average
add the values in each column find the average

I then get two array lists with averages for each file.
then subtract the value from its corsiponding value in the opposite list.

I dont know how to explain it any better than that???

Umm not sure this is what you looking for but had fun doing it

//Pseduo data
var row1 = "1,2,3,5,6" + Environment.NewLine + "5,5,6,7,8";
var row2 = "3,5,6,7,8" + Environment.NewLine + "3,2,5,3,1";

var dataSet1 = row1.Split('\n');
var dataSet2 = row2.Split('\n');

var floatList1 = new List<float>();

foreach (var row in dataSet1){
  var split = row.Split(',');
  var rowTotal = split.Sum(columnVal => float.Parse(columnVal));
  floatList1.Add(rowTotal / split.Length);
}

var floatList2 = new List<float>();

foreach (var row in dataSet2) {
  var split = row.Split(',');
  var rowTotal = split.Sum(columnVal => float.Parse(columnVal));
  floatList2.Add(rowTotal / split.Length);
}

var difList = floatList1.Select((t, i) => t - floatList2[i]).ToList();

foreach (var item in difList){
  Console.WriteLine(item);
}

And then going really deep into queries

//Pseduo data
var row1 = "1,2,3,5,6" + Environment.NewLine + "5,5,6,7,8";
var row2 = "3,5,6,7,8" + Environment.NewLine + "3,2,5,3,1";

var dataSet1 = row1.Split('\n');
var dataSet2 = row2.Split('\n');

var floatList1 = (from row in dataSet1
select row.Split(',')
into split let rowTotal = split.Sum(columnVal => float.Parse(columnVal)) select rowTotal/split.Length).ToList();

var floatList2 = (from row in dataSet2
select row.Split(',')
into split let rowTotal = split.Sum(columnVal => float.Parse(columnVal)) select rowTotal/split.Length).ToList();

var difList = floatList1.Select((t, i) => t - floatList2[i]).ToList();

foreach (var item in difList) {
  Console.WriteLine(item);
}

Pretty much achieves what your code does. Assumes a few things
1) All columns are float parseable
2) Row counts from both data sets are equal

Pretty certain it can be made more fault tolerant with a bit of work :)

Umm not sure this is what you looking for but had fun doing it

//Pseduo data
var row1 = "1,2,3,5,6" + Environment.NewLine + "5,5,6,7,8";
var row2 = "3,5,6,7,8" + Environment.NewLine + "3,2,5,3,1";

var dataSet1 = row1.Split('\n');
var dataSet2 = row2.Split('\n');

var floatList1 = new List<float>();

foreach (var row in dataSet1){
  var split = row.Split(',');
  var rowTotal = split.Sum(columnVal => float.Parse(columnVal));
  floatList1.Add(rowTotal / split.Length);
}

var floatList2 = new List<float>();

foreach (var row in dataSet2) {
  var split = row.Split(',');
  var rowTotal = split.Sum(columnVal => float.Parse(columnVal));
  floatList2.Add(rowTotal / split.Length);
}

var difList = floatList1.Select((t, i) => t - floatList2[i]).ToList();

foreach (var item in difList){
  Console.WriteLine(item);
}

And then going really deep into queries

//Pseduo data
var row1 = "1,2,3,5,6" + Environment.NewLine + "5,5,6,7,8";
var row2 = "3,5,6,7,8" + Environment.NewLine + "3,2,5,3,1";

var dataSet1 = row1.Split('\n');
var dataSet2 = row2.Split('\n');

var floatList1 = (from row in dataSet1
select row.Split(',')
into split let rowTotal = split.Sum(columnVal => float.Parse(columnVal)) select rowTotal/split.Length).ToList();

var floatList2 = (from row in dataSet2
select row.Split(',')
into split let rowTotal = split.Sum(columnVal => float.Parse(columnVal)) select rowTotal/split.Length).ToList();

var difList = floatList1.Select((t, i) => t - floatList2[i]).ToList();

foreach (var item in difList) {
  Console.WriteLine(item);
}

Pretty much achieves what your code does. Assumes a few things
1) All columns are float parseable
2) Row counts from both data sets are equal

Pretty certain it can be made more fault tolerant with a bit of work :)

Thanks will have to look through it a bit closer see what i can do with it but thank you for your post.

Thanks will have to look through it a bit closer see what i can do with it but thank you for your post.

You shouldn't have to do too much to it, just replace the datasources initial, so row1 will become your first file read in (remove empty lines) and row 2 will become your second file read in (again remove empty lines)

I just reread your requirements and I fail to see how you can subtract the average of a row for the average of a column? the row average is based on the sum of the rows column values / the total number of columns. What the code above does is calculate the average for each row and then subtracts the averages of each row from the other dataset. I think that was the requirement? With a single value though it is going to be difficult to plot. What I think youmight be looking for is the one dataset value alongside the other dataset value but manipulate the code, hopefully you can derive a solution from it :)

Here is the code you want. But I find it hard that you will underatand it:

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!!!
            //I hope this helps
            //(but I know my effort was worthless, becuase you will not understand what I did)
            //or am I wrong?

            //the code above is EXACTLY what you have asked me to do, in the post where you pasted the file example!!

Please, take some (or more) time to study this code, it does what you were asking me to do.
- It gets the averages of the rows of the "TraceSet.csv" file
- It gets the averages of the columns of the "SBoxOutput.csv" file.
- it calculated the subtraction of the value from row1 anf column1.

I hope you will find it working.

Here is the code you want. But I find it hard that you will underatand it:

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!!!
            //I hope this helps
            //(but I know my effort was worthless, becuase you will not understand what I did)
            //or am I wrong?

            //the code above is EXACTLY what you have asked me to do, in the post where you pasted the file example!!

Please, take some (or more) time to study this code, it does what you were asking me to do.
- It gets the averages of the rows of the "TraceSet.csv" file
- It gets the averages of the columns of the "SBoxOutput.csv" file.
- it calculated the subtraction of the value from row1 anf column1.

I hope you will find it working.

oh im sorry i didnt see this>i apologise. THANK YOU!!

oh im sorry i didnt see this>i apologise. THANK YOU!!

i only have problem with one line:

dic = dic.ToDictionary(d => d.Key, d => d.Key == j ? d.Value + intValue : d.Value);

I havnt used dictionary before. It wont build this bit. Im not sure what this bit does?

Maybe you are dont use Linq namespace:

using System.Linq; //add this on the top of the class

Or you use 1.1 VS, becuase 2.0 has Linq included

Same solution would be :

dic[j]+= intValue;
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.