Receive data in a matrix c#

Asked

Viewed 180 times

0

Hello,

I was testing code to learn how to mess with the matrix, but I’m not able to store broken numbers in the variable.

he of the error of System.IndexOutOfRangeException and I can’t solve.

Someone could give me a light.

    {
        Boolean stop = true;
            int x = 0;
            double[] dados = new double[x];
        do
        {

            Console.WriteLine("Digite um numero");
            dados[x] = double.Parse(Console.ReadLine());
            x += 1;
            if (Console.ReadKey().Key != ConsoleKey.Y)
            {
                stop = false;
            }
        } while (stop);

        Console.WriteLine("Dados recebidos");
        for(int i = 0; i < dados.Length; i++)
        {
            Console.WriteLine(dados[i]);
        }
        Console.ReadKey();

    }
  • just a comment, it’s not strange to start the code with a variable stop = true? already starting to stop? and while (stop) It’s also strange... if the variable were called continuar or something similar (may not be continue) would be better

  • x = 0 or your array has no position or positions? How will you save values without space!

2 answers

3

I believe you’re using Array for some kind of academic work, hence the use of List should be out of the question. Resizing a Array, something that is quite simple and direct using the Array.Resize (Array.Resize(T[], Int32) Method).


Using its base code, it would look that way:

int x = 0;
double[] dados = new double[1];

do
{
    Console.WriteLine("Digite um numero");
    dados[x] = double.Parse(Console.ReadLine());
    x++;

    if (Console.ReadKey().Key == ConsoleKey.Y)
        break;

    Array.Resize(ref dados, x + 1);
} while (true);

Console.WriteLine("Dados recebidos");

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

Console.ReadKey();
  • 1

    I wanted to understand the negative vote, is it because of the List and part Academia or Array.resize? I was curious

  • 2

    @Virgilionovic I also wanted to understand, but as we both know there are lots of users that negative answers but that unfortunately do not justify.

  • 1

    @Virgilionovic neither one reason nor the other. I clicked wrong even, it was to be a positive. João Martins, please edit the question so I can correct my mistake.

  • 1

    @Augustovasques so from my question I found strange the vote ... but, it may of course be reversed and good to always remember ...

  • @Virgilionovic has to reverse without the AR edit the answer?

  • 1

    I’m not sure, but, it seems that yes gives a tempted now I made an issue @Augustovasques

  • @Virgilionovic worked. Thank you! João Martins sorry for the inconvenience.

Show 2 more comments

2


Gustavo,

In your code, you create an array with the size of x, which in the case is zero:

int x = 0;
double[] dados = new double[x];

With this, you have an array of size zero, so when trying to access the index of the same:

dados[x] = double.Parse(Console.ReadLine());

You have the mistake that System.Indexoutofrangeexception


You can change the code to have a fixed size array by assigning a size to the array:

double[] dados = new double[10]; //Agora o array tem o tamanho de dez

And in the while condition, have a new check, to avoid typing more than the array supports, an example would be like this:

if (x < dados.Length)
{
    Console.WriteLine("Digite Y para continuar digitando valores:");

    if (Console.ReadKey().Key != ConsoleKey.Y || x >= dados.Length)
    {
        stop = false;
    }
}
else
{
    stop = false;
}

In the end, your code would look something like this:

using System;

class ArrayTeste {
  static void Main()
  {
    Boolean stop = true;
    int x = 0;
    double[] dados = new double[3];

    do
    {
        Console.WriteLine("\nDigite um numero:");
        dados[x] = double.Parse(Console.ReadLine());

        x += 1;

        if (x < dados.Length)
        {
            Console.WriteLine("Digite Y para continuar digitando valores:");

            if (Console.ReadKey().Key != ConsoleKey.Y)
            {
                stop = false;
            }
        }
        else
        {
            stop = false;
        }
    } while (stop);

    Console.WriteLine("\nDados recebidos");

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

    Console.ReadKey();
  }
}

Note: The do/while would be easily exchanged for a FOR, since you knows the size of the array.


Maybe, you are interested in lists, see your code using list, it is much simpler... And with foreach:

using System;
using System.Collections.Generic;

class ListTeste {
  static void Main()
  {
    Boolean stop = true;
    List<double> dados = new List<double>();

    do
    {
        Console.WriteLine("\nDigite um numero:");
        dados.Add(double.Parse(Console.ReadLine()));

        Console.WriteLine("Digite Y para continuar digitando valores:");

        if (Console.ReadKey().Key != ConsoleKey.Y)
        {
            stop = false;
        }
    } while (stop);

    Console.WriteLine("\nDados recebidos");

    foreach(double valor in dados)
    {
        Console.WriteLine(valor);
    }

    Console.ReadKey();
  }
}

The list does not have a predefined size, so I can keep the while while you have the patience to type numbers.


Finally, you can create the matrix based on an informed number:

int tamanho = int.Parse(Console.ReadLine());
double[] dados = new double[tamanho];
Console.WriteLine(dados.Length);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.