how to save the values in an array?

2
//+===================================================================
//
//   Acerca del programa : 
//   29. Matriz 4. Elaborar un programa que cree una matriz y 
//     que le muestre al usuario un menú con tres opciones: 1) Insertar un elemento  2) Imprimir matriz y  3) Salir del programa.   
//
//   Autor del codigo : Gilberto Quintero Armenta
//
// 
// 
//+===================================================================

using System;

public class Program
{
    public static void Main()
    {


        int opcion,cordenadaArray,valorArray;
        Console.WriteLine("Bienvenidos al programa 29");
        Console.WriteLine("-----------------------------");

        Console.WriteLine(" tres opciones: 1) Insertar un elemento  2) Imprimir matriz y  3) Salir del programa.");

        Console.WriteLine("Selecciona el numero:");
        string opcionapedir = Console.ReadLine();
        opcion = Convert.ToInt32(opcionapedir);
        /*
        Si el usuario selecciona la primera opción, el programa debe pedirle las coordenadas 
        de una casilla en la matriz y un número entero que se insertará en la casilla que se haya determinado.
        */

       int[] myArr1 = new int[1];


      switch (opcion)
      {
          case 1:
           Console.WriteLine("Dame la cordenada donde quieres que se agrege el valor :"); 
           string cordenada = Console.ReadLine();
           cordenadaArray = Convert.ToInt32(cordenada);
          Console.WriteLine("Dame el valor para agregar en la cordenada anterior:");
          string valor =Console.ReadLine();
          valorArray = Convert.ToInt32(valor);
          myArr1.SetValue( valorArray, cordenadaArray );




          break;
          case 2:
        for (int i = 0; i < myArr1.Length; i++)
            {
                Console.Write(myArr1[i] + " ");

            }

          break;
          default:
              Console.WriteLine("Default case");
              break;
      }     

    }

}

I'm trying to save the values requested by the user in an array and after printing it, does anyone know what my error could be?

As in this line:

myArr1.SetValue( valorArray, cordenadaArray);

I'm trying to save the values in the array "miArr1"

    
asked by Gilberto 29.01.2018 в 02:11
source

2 answers

1

Definitely your code should be throwing an error. For future publications please clarify.

To fill the value of a vector, you just have to do:

myArr1[pos] = valor

Where pos the position of the array you want to fill, and valor the value you want to add in that position. Remember that if it is an array of objects, you must initialize the object that you are passing by using new (especially if you want to obtain the value of it without previously setting it).

    
answered by 29.01.2018 в 02:28
0

Your error is in that you create an array of fixed size (1), you should put a bigger size and / or specify to the user that the value of the coordinate must be within the limits, of course it can not be a coordinate either negative, because the arrays go from 0 to the specified size

  

int [] myArr1 = new int [5000];

using System;

public class Program
{
    public static void Main()
    {


    int opcion,cordenadaArray,valorArray;
    Console.WriteLine("Bienvenidos al programa 29");
    Console.WriteLine("-----------------------------");

    Console.WriteLine(" tres opciones: 1) Insertar un elemento  2) Imprimir matriz y  3) Salir del programa.");

    Console.WriteLine("Selecciona el numero:");
    string opcionapedir = Console.ReadLine();
    opcion = Convert.ToInt32(opcionapedir);
    /*
    Si el usuario selecciona la primera opción, el programa debe pedirle las coordenadas 
    de una casilla en la matriz y un número entero que se insertará en la casilla que se haya determinado.
    */

   int[] myArr1 = new int[5000];


  switch (opcion)
  {
      case 1:
       Console.WriteLine("Dame la cordenada donde quieres que se agrege el valor :"); 
       string cordenada = Console.ReadLine();
       cordenadaArray = Convert.ToInt32(cordenada);
      Console.WriteLine("Dame el valor para agregar en la cordenada anterior:");
      string valor =Console.ReadLine();
      valorArray = Convert.ToInt32(valor);
      myArr1.SetValue( valorArray, cordenadaArray );




      break;
      case 2:
    for (int i = 0; i < myArr1.Length; i++)
        {
            Console.Write(myArr1[i] + " ");

        }

      break;
      default:
          Console.WriteLine("Default case");
          break;
  }     

 }
}
    
answered by 29.01.2018 в 02:36