How to know how much data my vector has saved in C #

2
int[] VectorImpar = new int[20];

The user types in the keyboard x number of data, now, what the user wants is to know how many data he wrote, try with:

VectorImpar.legth() - > and this gives me is the value of the initial dimension

VectorImpar.Count() - > it also gives me the initial value of the vector

Neither of them tells me how much data the vector has saved

    
asked by deiby steven peralta 31.08.2017 в 00:10
source

3 answers

4

There is a major problem with the approach to your question. When initializing an array of integers, all array elements are initialized to 0 which is the default value for a int . Therefore, if 0 is a possible value that can be entered, the solution proposed by @Einer would not be valid, since it would exclude the zeros that the user has entered.

I see basically two options to solve your problem:

1.- Initialize the array with values that are not allowed in the user input . For example, if we know that the negative values will never be entered by the user, we could initialize the array to -1 or, for example, to the minimum value that can be entered in a int ( int.MinValue ), and then count the values that do not match:

int[] VectorImpar = new int[20];
//Inicializamos el vector
for (int k=0;k<VectorImpar.Length;k++)
{
     VectorImpar[k] = int.MinValue;
}
...
//Para contar los elementos introducidos
int cuenta=VectorImpar.Count(x=>x!=int.MinValue);

2.- Use a variable to count the elements entered by the user. Simply create a variable at the class level and increment it every time an element is added in the array

    
answered by 31.08.2017 в 09:17
1

You can achieve this by using the Where method, searching for all indexes with the value equal to 0:

int[] VectorImpar = new int[20];
VertorImpar[0] = 22;
VertorImpar[1] = -55;
int totalElementos = VertorImpar.Where(x=> x == 0).Count(); 
Console.WriteLine(totalElementos.ToString()); // 18

The Where() method filters the collection by the elements that compile the condition in the method that was sent to it by parameters and then the Count () method counts the result of the filter.

Although this is not how the use of the method with the method Count is translated, here is an idea of how it would be in essence and how to use it without the method:

 int totalVacios = 0;
    foreach(int i in VertorImpar)
    {
       if(i==0)
       {
          totalVacios++;
       }
    }

Console.WriteLine(totalVacios.ToString());
    
answered by 31.08.2017 в 01:22
1

Since it is a simple array in which you have already reserved the memory, both functions will give you the initial size of the array. I recommend you use some auxiliary class like:

List<int> integers = new List<int>();
integers.Add(1);
integers.Add(4);
integers.Add(7);

int someElement = integers[1];

These classes allow you to know the size of the array with those functions. They also allow you not to have to reserve an X memory size, but it is reserved dynamically.

Edited:

In case the size should be given and knowing that a class work, I recommend that you create your own class "MiVector" which has as attributes a int[] vector and a int contador .

Example with pseudocode:

 public class MiVector
   {
      // Class MiVector.
      private int[] vector;
      private int contador;
      public int MiVector(int size)
      {
          vector = new int[size];
          contador = 0;
      }
      //comportamiento de una cola (FIFO)
      public void set(int value)
      {
          vector[contador]=value;
          contador++;
      }

      public int  get(int indice)
      {
          return vector[indice];
      }
   }

You could also use it as MiVector[5] , for that you would have to overload the operator .

Another option (Completing what your colleagues have told you):

Work in this way:

int[] VectorImpar = new int[20];
int totalElementos = 0;
VertorImpar[0] = 22;
totalElementos++;
VertorImpar[1]++;

I hope this answers your question.

    
answered by 31.08.2017 в 00:17