are there anybody can help me ? i'm writting a program ( reversing an array ), but it doesn't run :(

104 views Asked by At

I'm writing a program about reversing an array with 3 methods (GenerateNumber, reverse and PrintOut).

Unfortunately it doesn't run. Can you help me to find the error and fix them?

Why doesn't it run?

  public class Program
{
    static int[] GenerateNumber()
    {
        string a = Console.ReadLine();
        int b = Convert.ToInt32(a);
        int[] number = new int [b];
        string[] c = new string[b];
        for (int index = 0; index < number.Length; index++)
        {
            c[index] = Console.ReadLine();
            number [index]= Convert.ToInt32(c[index]);
       }
        return number;
    }
    static int[] reverse(int[] array)
    {

        for (int index =0; index <array.Length; index++)
        {
            int c = array[index];
            array[index] = array[array.Length - index - 1];
            array[array.Length - index - 1]= c;
        }
        return array;
    }
    static int[] PrintOut (int[] array)
    {
        for (int index = 0; index > array.Length; index++)
            Console.Write(array[index]);
        return array;
    }
           static void Main(string[] args)
    {
        int[] number = GenerateNumber();
        reverse(number);
        PrintOut(number);
        Console.ReadKey();
    }
1

There are 1 answers

4
Dmitry Bychenko On BEST ANSWER

The immediate cause of the misbehaviour is in the

static int[] PrintOut (int[] array)
{
    for (int index = 0; index > array.Length; index++) // <- wrong condition
        Console.Write(array[index]);

The comparison should be < instead of >:

    for (int index = 0; index < array.Length; index++) 

A better choice, however, is foreach loop instead of for

    foreach (var item in array)
      Console.Write(item); // propably, you want WriteLine not Write 

Some suggestions:

  public class Program {
    static int[] GenerateNumber() {
      // You don't want "c" array, but "number"
      int[] number = new int [Convert.ToInt32(Console.ReadLine())];

      for (int index = 0; index < number.Length; index++)  
        number [index] = Convert.ToInt32(Console.ReadLine());

      return number;
    }

    // Nice implementation, nothing to improve but naming (reverse -> Reverse)  
    static int[] Reverse(int[] array) {
        for (int index = 0; index <array.Length; index++) {
            int c = array[index];
            array[index] = array[array.Length - index - 1];
            array[array.Length - index - 1] = c;
        }
        return array;
    }

    static int[] PrintOut (int[] array) {
        // foreach is easier to implement and easier to read
        foreach (var item in array)
          Console.WriteLine(item); // <- you, probably, want WriteLine not Write 

        return array;
    }

    static void Main(string[] args) {
        int[] number = GenerateNumber();
        Reverse(number);
        PrintOut(number);
        Console.ReadKey();
    }