Error while running program using "args"

Asked

Viewed 180 times

0

I am trying to run a program passing some values as parameters on the command line but 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 exercicio32.exercicio32.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);
        }
}
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

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

And gone is using the stopping condition i<=l, what will happen is numbers[7], as there is no number in that position gives the exception.

You need to fix all your conditions so that no further indexing error occurs, this error is very common in people who are learning to program.

The correct condition would be i < l, thus i goes from 0 to 6 as it should.

2

That mistake specifically is that we fors you are using the operator <= when it should be <. The sweep of array has to finish a number before its size since it starts at 0.

The code has several other errors and ordered nothing but being a little 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;
                    }
                }
            }
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle (has no way to pass arguments on it). Also put on the Github for future reference.

Browser other questions tagged

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