Domi - 1 year ago 60
C# Question

# Columns in Multidimensional Arrays c#

I have data in txt like this:

flea,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,6

frog,0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,5

frog,0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,5

I need to count the number of zeros in a chosen column, for example in the first column there are 3 zeros.

Here is my code so far:

``````        //data patch

//full of array
string[] tablica = tekst;

for(int s=0;s<tablica.Length;s++)
{
Console.WriteLine(tablica[s]);
}

//----------------Show all of array---------------------------//

//----------------Giv a number of column-----------------////
Console.WriteLine("Podaj kolumne");

//Console.WriteLine("Podaj wiersz");

int n = tablica.Length;

int m = tablica[a].Split(',').Length;

string[,] liczby = new string[n, m];

for (int j = 0; j < n; j++)
{
int suma = 0;
for (int i = 0; i < m; i++)
{
//somethink should be here

}

}
``````

Any ideas for solving this?

try:

``````//data patch

//full of array - you allready have a string[], no need for a new one
//string[] tablica = tekst;

for(int s=0;s<tekst.Length;s++)
{
Console.WriteLine(tekst[s]);
}

//----------------Show all of array---------------------------//

//----------------Giv a number of column-----------------////
// try to use names with some meaning for variables, for example instead of "a" use "column" for the column
Console.WriteLine("Podaj kolumne");

// your result for zeros in a given column
int suma = 0;

// for each line in your string[]
foreach ( string line in tekst )
{
// get the line separated by comas
string[] lineColumns = line.Split(',');
// check if that column is a zero, remember index is base 0
if ( lineColumns[column-1] == "0" )
suma++;
}

Console.WriteLine(suma);
``````

EDIT: Just make sure to validate that the column they ask for, really exist in your array, AND if you do not consider the first column as the one with the name, adjust this part

``````lineColumns[column] // instead of lineColumns[column-1]
``````