How can I possibly count only the specific whole column I want in a multidimensional array
I do the counting of my whole column and row like this
//COLUMN
for(int col = 0; col < table.GetLength(0); col++)
{
int sum = 0;
//ROW
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[col,row] != null)
{
sum++;
}
}
Debug.Log("table column: " + col + " has " + sum + " data");
}
What I want is just to get the specific whole Column then move to another Column just like that. I need to do this because i need to compare it with the last column value to the next one.
For example : I want to check how many data are there in the 2nd column then compare it with the 1st column.
You´re almost there. All you have to do is store the sum of the current column into a list:
var List<int> sums = new List<int>();
//COLUMN
for(int col = 0; col < table.GetLength(0); col++)
{
int sum = 0;
//ROW
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[col,row] != null)
{
sum++;
}
}
Debug.Log("table column: " + col + " has " + sum + " data");
sums.Add(sum);
}
Now you can easily compare the number of rows in colum 1 and that of column 2:
bool areEqual = sums[0] == sums[1];
Got a solution :)
//generic function
public static int CountRow<T>(T[,] table, int col)
{
if (table == null || col < 0 || col >= table.GetLength(1))
{
//handle error
return -1;
}
//this is the same as the block of the outer for loop
int sum = 0;
for (int row = 0; row < table.GetLength(1); row++)
{
if(table[col,row] != null)
{
sum++;
}
}
return sum;
}
then used it like this
int prevSum = -1;
for (int col = 0; col < table.GetLength(0); ++col)
{
int sum = CountRow(table, col);
Debug.Log("table column :" + col + " has " + sum + " data");
if (sum == prevSum)
{
//comparison happens
}
}
Related
IList<IWebElement> rows = _driver.FindElements(By.XPath("//div[#id='data_3']/div/div/div[3]/table/tbody/tr[#class]"));
for (int i = 1; i <= rows.Count; i++)
{
IList<IWebElement> columns = _driver.FindElements(By.XPath("//div[#id='data_3']/div/div/div[3]/table/tbody/tr[#class][" + i + "]/td[#class]"));
for (int j = 1; j <= columns.Count; j++)
{
}
}
From what I understand, you need to sum values from three rows and check if it is equal to the value from the first one. I will use the path you have provided.
public int GetRowValue(int rowNumber)
{
var row = _driver.FindElements(By.XPath("//div[#id='data_3']/div/div/div[3]/table/tbody/tr[#class]"))[rowNumber - 1] // decreased by 1, because if you would like to get first row, it would have 0th index in the array
var columns = row.FindElements(By.XPath("./td"); // find all tds of the given row.
int value = 0;
foreach(var column in columns)
{
if (String.IsNullOrEmpty(column.Text)) continue;
result += Int32.Parse(column.Text);
}
return value;
}
With that, you can easily check what you want.
var row1Value = GetRowValue(1);
var sum = GetRowValue(3);
sum += GetRowValue(6);
sum += GetRowValue(8)
Assert.Equals(row1Value, sum);
I have a multidimensional array which is declared like this
string[,] table = new string[104,15];
Now it has all the data that I need and I got the data to put it on the table by doing this
int xIndex = 0;
int yIndex = 0;
string newPreviousValue = "placeholder";
for (int i = 0; i < list.Count; i++)
{
newString[0] += list[i].r;
newString[0] += ",";
}
string[] newChars = newString[0].Split(',');
foreach (string previousValue in newChars)
{
table[xIndex, yIndex] = result;
}
Now what I am trying to do is from this Data A
All these value of Data B is depending on what value of Data A has
RED means "The same in length"
BLUE means "Not the same in length"
So here how it works .
PSEUDO CODE
//I am talking about Data A
if table[1,1] is equal to null or T then
{
if table[1,0] and table[0,0] has the same length
//Display blue circle on Data B
display a blue circle
}
//I am talking about Data A
else if table[1,1] is not equal to null then
{
compare table[0,1] and table[0,0] if they have the same length as table[1,1] and table[1,0]
//Display a red circle
display a red circle }
}
If I am not clear here's the rule . What I am talking about is the Big Eye Road Rules.
What I have tried so far is this
//lets check for the 2nd row and 2nd column of the big road table
if (table[1,1] == null && table[2,0] != null)
{
Move = true;
if (Move)
{
//lets move to the 3rd row and compare if they have the same in length (1st row and 1st column)
if (table[0, 0] != null && table[1, 0] != null)
{
//red circle
}
else
{
//blue circle
}
}
}
What I want to achieve here is to count every row and column in DATA A if how many data's that are stored there for example
table[0,0] to table[0,6] has 1 data
table[1,0] to table[1,6] has 1 data
table[2,0] to table[2,6] has 2 data
Just like that . Could someone help with it please. Thank you.
EDIT
On the last part of my question I will share what I did
//COLUMN
for(int col = 0; col < table.GetLength(0); col++)
{
int sum = 0;
//ROW
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[col,row] != null)
{
sum++;
}
}
Debug.Log("table column: " + col + " has " + sum + " data");
}
I can now get this
table[0,0] to table[0,6] has 1 data
table[1,0] to table[1,6] has 1 data
table[2,0] to table[2,6] has 2 data
for(int col = 0; col < table.GetLength(0); col++)
{
int sum = 0;
//ROW
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[col,row] != null)
{
sum++;
}
}
Debug.Log("table column: " + col + " has " + sum + " data");
}
I am currently using this code:
using System;
using NCalc;
namespace SandboxConsoleApp
{
class Program
{
static void Main(string[] args)
{
double[,] data = new double[100, 10];
Random random = new Random();
var numberOfRows = data.GetLength(0);
var numberOfColumns = data.GetLength(1);
for (int row = 0; row < numberOfRows; row++)
{
for (int col = 0; col < numberOfColumns; col++)
{
data[row, col] = random.Next();
}
}
// in the case of 10 columns the expression looks like: [x0] + [x1] + [x2] + [x3] + [x4] + [x5] + [x6] + [x7] + [x8] + [x9]
var stringExpression = "";
for (int col = 0; col < numberOfColumns - 1; col++)
{
stringExpression += string.Format("[x{0}] + ", col);
}
stringExpression += string.Format("[x{0}]", (numberOfColumns - 1));
var exp = new Expression(stringExpression);
var total = 0.0;
for (int row = 0; row < numberOfRows; row++)
{
for (int col = 0; col < numberOfColumns; col++)
{
exp.Parameters[string.Format("x{0}", col)] = data[row, col];
}
if (row % 100000 == 0)
{
Console.WriteLine(row);
}
if (!exp.HasErrors())
{
total += (double)exp.Evaluate();
}
}
}
}
}
Here the fake 'dynamic' expression/formula:
[x0] + [x1] + [x2] + [x3] + [x4] + [x5] + [x6] + [x7] + [x8] + [x9]
adds 10 columns of a 10000000 row 'flat file'. The execution is not very fast and I hit limits, if I have lets say 100 million rows. Is there anything I can do to execute the above faster or should I use some other technologies to execute dynamically created formulas like this? Not sure how fast MySql would be - here I would generate the formula as SQL to the db via (e.g. Dapper).
I have just started using 2D arrays, but can't seem to figure out how to get the average of each column. I am using a for loop to have the user enter the data( a students grade), then a for loop to display the information user entered. But after the information is displayed, I want to display the average of each column. What should I do get the average of each column?
This is the code I have so far
static void Main(string[] args)
{
double[,] grades = new double[2, 3];
double result;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("Enter Grade " + (j + 1) + " For Group" + (i + 1) + ": ==>> ");
if (double.TryParse(Console.ReadLine(), out result)) grades[i, j] = result;
else
{
Console.WriteLine("*** INVALID GRADE ENTERED. PLEASE REENTER.");
}
}
}
for (int row = 0; row < 1; row++)
{
Console.WriteLine();
Console.Write(" Group " + (row + 1) + ": ");
Console.WriteLine(" Group " + (row + 2) + ": ");
Console.Write("=========== ===========");
for (int col = 0; col < 3; col++)
{
//String.Format("{0,-10} | {1,-10} | {2,5}",
//make pring for execise 2 Console.Write(string.Format("{0,-5}", grades[row, col]));
Console.WriteLine();
Console.Write(string.Format("{0,-9}", ""));
Console.Write(string.Format("{0,-20}",grades[0, col]));
Console.Write(grades[1,col]);
}
Console.WriteLine();
Console.WriteLine("=========== ===========");
}
Console.WriteLine("\n\npress any key to exit...");
Console.ReadKey();
//print it for exercise 1 myArr[o, column]; myArr[ , column]
}`
If you are looking for a special command that will do it for you, you're out of luck! You'll just have to write the code to do it, the same way you would normally average a series of numbers. Hint: The number of elements in the 'y' dimension of a 2D array is given by e.g. grades.GetLength(1).
To get Average per columns you need to traverse columns for a fixed row and add their values like this:
int columnTotal, average;
for (int row = 0; row < 2; row++)
{
columnTotal = 0;
for (int col = 0; col < 2; col++)
{
columnTotal += grades[row, col];
}
average = columnTotal/2;
Console.WriteLine("Average: {0}", average);
}
1.) ***Ok, so I have my DataTable that is imported from an Excel SpreadSheet and it is filled.I would like to sift through the DataTable and sum each row. I have to skip the first column & the first rows because they are labels. I am trying to reach each row in the table and total it and output to a "Row Total" column .. I am getting a "invalid cast specified" when I am trying to assign the 'number' variable to try and sum each cells value.
Example:
Row(0) --------------------ItemA......ItemB.......ItemC..............RowTotal
Column(1) CompanyA .....12 ..........12.............10....................34
2.) Also, I haven't reached it yet -- there is a potential issue with my trying to output it to the last column in the DataTable.
noted by: dr[dt.Columns.Count - 1] = Convert.ToInt32(sum);
Any thoughts or suggestions?
DataRow dr = dt.NewRow();
int sum = 0;
dt.Columns.Add("Row Totals", typeof(int));
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].ColumnName == "Client")
{
dt.Columns.Cast<DataColumn>().Skip(1);
}
else
{
for (int j = 0; j < dt.Rows.Count - 1; j++)
{
dt.Rows.Cast<DataRow>().Skip(1);
int number =0;
number = (dt.Rows[j].Field<int>(i));
sum += number;
dr[dt.Columns.Count - 1] = Convert.ToInt32(sum);
Console.WriteLine("Row : {0} , Column : {1} , Value : {2}", i,j, dt.Rows[i][j].ToString());
Console.WriteLine(sum);
}
Console.ReadLine();
}
}
UPDATE: 12/27/12 ************
So, a solution I'm trying is to just skip the rows and column I know are text. I am still getting the "specified cast not valid" when it tries to filter through each cell and sum it. Any more suggestions?
Thank you in advance.
DataRow dr = dt.NewRow();
int sum = 0;
int number = 0;
for (int i = 0; i < dt.Columns.Count-1; i++)
{
if (dt.Columns[i].ColumnName == "column1")
{
dt.Columns.Cast<DataColumn>().Skip(0);
}
if (dt.Columns[i].ColumnName == "column2")
{
dt.Columns.Cast<DataColumn>().Skip(1);
}
else
{
for (int j = 0; j < dt.Rows.Count; j++)
{
dt.Rows.Cast<DataRow>().Skip(0);
dt.Rows.Cast<DataRow>().Skip(1);
//if (number != -1 && number != 0)
//{
number = (dt.Rows[j].Field<int>(i));
sum += number;
dr[dt.Rows.Count] = Convert.ToInt32(sum);
//}
//else
//{
// number = 0;
//}
Console.WriteLine("Row : {0} , Column : {1} , Value : {2}", i, j, dt.Rows[i][j].ToString());
Console.WriteLine(sum);
}
Console.ReadLine();
dataGridView1.DataSource = dt;
}
**UPDATE 12/27 1:30pm
I have scratched all that previous code and am attempting a test sheet and output. It seems to be working except now I can't seem to get the items to total when I am adding them to the last row. I'm stuck in the "else"" section of the code.
for (int i = 0; i < dt.Columns.Count - 2; i++)
{
for (int j = 0; j < dt.Rows.Count - 1; j++)
{
string value = dt.Rows[i][j].ToString();
int num = 0;
bool res = int.TryParse(value, out num);
if (res == false)
{
num = 0;
}
else
{
int sum = 0;
sum += num;
DataRow dr;
dr["Totals"] = sum;
dt.Rows.Add(dr);
}
}
dataGridView1.DataSource = dt;
}
}
One possibility is that there is a blank value in your data table - you can't cast a null object to an integer, so the Field extension method may be failing.