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).
– Maniero