5 Replies - 1055 Views - Last Post: 16 October 2011 - 01:26 AM Rate Topic: -----

#1 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

[Mergesort] gets an undefined variables

Posted 12 October 2011 - 08:44 AM

Hey I have programmed a MergeSort, but apparently I have some undefined variable according to RadRails compiler, but I have been over and over the
code I can't find the my mistake, can anyone help:
class ArrayMethods
  
  def initialize
  
  end

  def mergeSort(arr=[])
    if(arg.length <= 1)
      return arr;
    end
    left=[], right=[], result=[]; 
    x = 0, y = 0;
    
    for i in (0..arr.length/2)
        left[x] = arr[i];
        x+1;
    end
    for i in (arr.length/2..arr.length)
      right[y] = arr[i];
      y+1;
    end
    left = mergeSort(left);
    right = mergeSort(right);
    result = merg(left, right);
    return result;
  end
  
  def merge(left = [], right = [])
    result=[];
    i = 0, j = 0, n = 0;
    while(i < left.length && j < right.length)
      if(left[i] <= right[j])
        result[n] = left[i];
        n+1;
        i+1;
      end
      if(right[j] <= left[i])
        result[n] = right[j];
        n+1;
        j+1;
      end
    end
    while(i < left.length)
      result[n] = left[i]
      n+1;
      i+1;
    end
    while(j < right.length)
      result[n] = right[j];
      n+1;
      j+1;
    end
    return result;
   end
end



This is btw what I do to run the program:
if __FILE__ == $0
require 'ArrayMethods.rb' 
  # TODO Generated stub
  arr = [45, 3, 50, 42, 84, 74, 10, 11, 23, 10];
  
  at = ArrayMethods.new;
  
  arr2 = at.mergeSort(arr);
  puts arr2;
end



Is This A Good Question/Topic? 0
  • +

Replies To: [Mergesort] gets an undefined variables

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: [Mergesort] gets an undefined variables

Posted 12 October 2011 - 09:05 AM

First, can you past the text of the error?

Second, I'm seeing things like x + 1 and y + 1 where I think you mean to put x += 1. Lines to consider: 16, 20, 34, 35, 39, 40, 45, 46, 50, 51.
Was This Post Helpful? 0
  • +
  • -

#3 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: [Mergesort] gets an undefined variables

Posted 12 October 2011 - 11:47 AM

Thanks i am brand new to RUBY and RadRails gave me a warning using x += 1

The eror code:
./ArrayMethods.rb:8:in `mergeSort': undefined local variable or method `arg' for #<ArrayMethods:0x107fbb050> (NameError)
from arrayTest.rb:8

And I have now change the x+1 to x+=1 in every line didn't help

This post has been edited by lnc12: 12 October 2011 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 906
  • View blog
  • Posts: 3,171
  • Joined: 12-May 09

Re: [Mergesort] gets an undefined variables

Posted 12 October 2011 - 12:31 PM

It didn't help yet because you're still getting the first error (both were causing problems, we just haven't solved the first yet). It likely removed some errors in the future.

As for your current error, look at the line it's referring to:

  if(arg.length <= 1)


You're using arg, but the array is called arr. Change arg to arr and that first error should be solved.

This post has been edited by xclite: 12 October 2011 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: [Mergesort] gets an undefined variables

Posted 13 October 2011 - 01:33 AM

Thank you such much… think I need new classes
Was This Post Helpful? 0
  • +
  • -

#6 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: [Mergesort] gets an undefined variables

Posted 16 October 2011 - 01:26 AM

This code is really bad. Read some good book about Ruby idioms - http://www.amazon.co...l/dp/0321584104
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1