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);
}
}