Error executing program using "args"

0

I'm trying to run a program by passing some values as partions on the command line but it is returning the error below.

C:\Users\joao.mello\Documents\C#> .\exercicio32.exe 5 1 0 4 9 32 4
  

Unhandled Exception: System.IndexOutOfRangeException: Index was   outside the bounds of the array. at   exercise.exercise32.Main (String [] args)

Code:

using System;

namespace exercicio32{
    class exercicio32{
        static void Main(string[] args){
            int l = args.Length;
            int[] numeros = new int[args.Length];
            int[] ordenado = new int[args.Length];

            for(int i = 0;i<=l;i++)
            {
                numeros[i] = int.Parse(args[i]);
            }
            ordenado = orderna_vetor(ref numeros, ref l);
            Console.Write("Vetor: ");
            for(int i=0;i <= l; i++){
                Console.Write(numeros[i]);
            }
            Console.WriteLine("Vetor Ordenado: ");
            for(int i = 0;i <= l; i++){
                Console.Write(ordenado[i]);
            }
        }
        public static int[] orderna_vetor(ref int[] vetor, ref int k){
            int[] aux = new int[k];
            for(int i = 0;i <= k;i++){
                int x = vetor[i];
                for(int j=0;j <= k;j++){
                    int y = vetor[j];
                    if(x < y){
                        aux[i] = x;
                    }
                }
            }
            return(aux);
        }
}
    
asked by anonymous 05.02.2017 / 23:13

2 answers

2

The error is in indexing the array, if it has 5 1 0 4 9 32 4, the length equals 7.

And we are using the stop condition i

05.02.2017 / 23:48
2

This error specifically is that in for s you are using the <= operator when it should be < . The array scan has to finish a number before its size since it starts at 0.

The code has several other errors and not sorted anything, besides being a bit disorganized. I solved these problems too, simplified and modernized the code.

using static System.Console;

namespace exercicio32 {
    public class exercicio32 {
        public static void Main(string[] args) {
            int[] numeros = new int[args.Length];
            for (int i = 0; i < args.Length; i++)   {
                int numero;
                if (int.TryParse(args[i], out numero)) {
                    numeros[i] = numero;
                } else {
                    WriteLine("Dado inválido! Digite apenas números separados por espaço");
                }
            }
            Write("Vetor: ");
            for (int i = 0; i < args.Length; i++) {
                Write($"{numeros[i]} ");
            }
            OrdernaVetor(numeros);
            WriteLine("\nVetor Ordenado: ");
            for (int i = 0; i < args.Length; i++) {
                Write($"{numeros[i]} ");
            }
        }
        public static void OrdernaVetor(int[] vetor) {
            for (int i = 0; i < vetor.Length; i++) {
                for (int j = 0; j < vetor.Length - 1; j++) {
                    if (vetor[j] > vetor[j + 1]) {
                        int temp = vetor[j + 1];
                        vetor[j + 1] = vetor[j];
                        vetor[j] = temp;
                    }
                }
            }
        }
    }
}

See Running at Coding Ground . And in .NET Fiddle (you can not pass arguments on it). Also put it on GitHub for future reference .

    
05.02.2017 / 23:50