Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Array Subscript is out of range. What does that mean? Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mew123mew




PostPosted: Sat Feb 14, 2015 12:08 am   Post subject: Array Subscript is out of range. What does that mean? Help

I have to write a program where i am throwing a party, but i can't invite all my friends

So I must number your friends 1, 2, . . . , K and place them in a list in this order. Then perform m rounds. In each round, use a number to determine which friends to remove from the ordered list.
The rounds will use numbers r1, r2, . . . , rm. In round i remove all the remaining people in positions that are multiples of ri (that is, ri , 2ri , 3ri , . . .) The beginning of the list is position 1.
Output the numbers of the friends that remain after this removal process.

Input Specification
The first line of input contains the integer K (1 ≤ K ≤ 100). The second line of input contains
the integer m (1 ≤ m ≤ 10), which is the number of rounds of removal. The next m lines each
contain one integer. The ith of these lines (1 ≤ i ≤ m) contains ri ( 2 ≤ ri ≤ 100) indicating that
every person at a position which is multiple of ri should be removed.

What I have so Far:

Turing:

var x :int %number of guests
var y :int %number of rounds of removal
var z :int %removal

var number: flexible array 1..0 of int
var removal: flexible array 1..0 of int

get x % get number of guests
new number, x

for i: 1..x

number(i) := i
end for

get y%get rounds of removal

for a: 1..y
get z
removal(a) := z
end for

for i:1..x

if i mod removal(z) = 0 then
    new number, upper (number) - 1
end if
end for

for a: 1..x
    put number(a)
end for


Whenever I run it, it says Array Subscript is out of range. Why does it not work?




Mod Edit: Please wrap your code in syntax tags to preserve indentation.
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sat Feb 14, 2015 1:16 am   Post subject: RE:Array Subscript is out of range. What does that mean? Help

First try debugging the variables you know are causing problems. Since you're getting the error on removal (a) := z, add some debugging output just before that to get an idea of the state of everything that's causing the error.
Turing:

for a : 1 .. y
    get z
    put "a = ", a
    put "upper(removal) = ", upper (removal)
    removal (a) := z
end for


Output:
code:

a = 1
upper(removal) = 0
mew123mew




PostPosted: Sat Feb 14, 2015 2:20 pm   Post subject: RE:Array Subscript is out of range. What does that mean? Help

Thank you but I still don't understand how to fix it.
Zren




PostPosted: Sat Feb 14, 2015 3:34 pm   Post subject: RE:Array Subscript is out of range. What does that mean? Help

You resized the numbers flexible array with the new statement after you obtained the size from user input, but you didnt do the same for the removal flexible array.
mew123mew




PostPosted: Sun Feb 15, 2015 12:25 am   Post subject: Re: Array Subscript is out of range. What does that mean? Help

Oh Thanks! Very Happy

I was wondering is it possible to remove a specific element in a array. For example in my code i want to remove only, the element that can be divided by z.
Raknarg




PostPosted: Sun Feb 15, 2015 11:31 am   Post subject: RE:Array Subscript is out of range. What does that mean? Help

You have to shift all the elements bigger than it down by one. When you resize downwards, it just removes the last elements in the array.
mew123mew




PostPosted: Sun Feb 15, 2015 12:22 pm   Post subject: Re: Array Subscript is out of range. What does that mean? Help

How do you do that?
Zren




PostPosted: Sun Feb 15, 2015 3:23 pm   Post subject: RE:Array Subscript is out of range. What does that mean? Help

Turing:

View.Set ("text")
var arr : array 1 .. 5 of int := init (1, 2, 3, 4, 5)
var indexToRemove := 3

% Shifting to the left (copy the element to the right of it)
% arr(3) := arr(4)
% arr(4) := arr(5)
% ...and so on to the end of the array
% You should have noticed the pattern of copying arr(i) := arr(i + 1)
% arr(5) is the last element and should be removed by resizing the array to not include it.

% Shift (TODO)
% Here's how you can iterate the pairs of indexes.
for i : indexToRemove .. upper (arr) - 1
    put arr (i), " ", arr (i + 1)
end for

% Remove trailing element by resizing the array 1 size smaller (TODO)
% ...
Sponsor
Sponsor
Sponsor
sponsor
mew123mew




PostPosted: Mon Feb 16, 2015 4:14 pm   Post subject: Re: Array Subscript is out of range. What does that mean? Help

Thanks, but now i have another problem. I need to remove multiple elements at the same time.
Raknarg




PostPosted: Mon Feb 16, 2015 4:23 pm   Post subject: RE:Array Subscript is out of range. What does that mean? Help

Welk you can use this method, just do it more than once at a time. If you need to remove 5 elements, use this method 5 times for each element to be removed.
mew123mew




PostPosted: Mon Feb 16, 2015 4:31 pm   Post subject: Re: Array Subscript is out of range. What does that mean? Help

The problem for me is I need to remove all the elements for which that has a subscript that can be divided by 2. So I did:

code:

for a : 1 .. upper (people) %2
        put people (a)
        if (people (a) mod remove = 0) then

            for b : remove .. upper (people) - 1 %3

                people (b) := people (b + 1)

            end for %3
                decrease := decrease + 1
            for z : 1 .. guest
                put people (z)
            end for
        end if
    end for %2


The thing is, it keeps on removing the element that has a subscript of 2, and not any others
Insectoid




PostPosted: Mon Feb 16, 2015 5:14 pm   Post subject: RE:Array Subscript is out of range. What does that mean? Help

Is people(a) the name of the person or the subscript of that person?

You seem to have the right idea, but you're mixing up your variables.
mew123mew




PostPosted: Mon Feb 16, 2015 5:16 pm   Post subject: Re: Array Subscript is out of range. What does that mean? Help

people(a) is the subscript
Zren




PostPosted: Mon Feb 16, 2015 6:56 pm   Post subject: RE:Array Subscript is out of range. What does that mean? Help

Mutating an array while iterating it is generally a bad idea.

If you remove/shift the elements while iterating, you'll end up skipping elements.

Turing:

proc printIntSubArray (arr : array 1 .. * of int, s : int, e : int)
    for i : s .. e
        put "[", arr (i), "]" ..
    end for
    put ""
end printIntSubArray

proc printIntArray (arr : array 1 .. * of int)
    printIntSubArray (arr, 1, upper (arr))
end printIntArray

proc shift (var arr : array 1 .. * of int, startIndex : int)
    for i : startIndex .. upper (arr) - 1
        arr (i) := arr (i + 1)
    end for
end shift

View.Set ("text")
var arr : array 1 .. * of int := init (1, 2, 3, 4, 5, 6, 7, 8, 9)

printIntArray (arr)

for i : 1 .. upper (arr)
    put skip
    put "i = ", i
    put "arr(i) = ", arr (i)
    printIntArray (arr)
    put "shift (arr, ", i, ")"
    shift (arr, i)
    printIntArray (arr)
end for


code:

[1][2][3][4][5][6][7][8][9]


i = 1
arr(i) = 1
[1][2][3][4][5][6][7][8][9]
shift(arr, 1)
[2][3][4][5][6][7][8][9][9]


i = 2
arr(i) = 3
[2][3][4][5][6][7][8][9][9]
shift(arr, 2)
[2][4][5][6][7][8][9][9][9]


i = 3
arr(i) = 5
[2][4][5][6][7][8][9][9][9]
shift(arr, 3)
[2][4][6][7][8][9][9][9][9]


i = 4
arr(i) = 7
[2][4][6][7][8][9][9][9][9]
shift(arr, 4)
[2][4][6][8][9][9][9][9][9]
...


Notice how we skipped some of the elements?

One way to "filter" elements out of an array would be to keep track of the number of kept elements, and move them into position as you iterate through. Then at the end, resize the array.

code:

[1][2][3][4][5][6][7][8][9]


i = 1
arr(i) = 1
keep = false
newSize = 0
[1][2][3][4][5][6][7][8][9]



i = 2
arr(i) = 2
keep = true
newSize = 1
[2][2][3][4][5][6][7][8][9]
[2]


i = 3
arr(i) = 3
keep = false
newSize = 1
[2][2][3][4][5][6][7][8][9]
[2]


i = 4
arr(i) = 4
keep = true
newSize = 2
[2][4][3][4][5][6][7][8][9]
[2][4]

...

i = 9
arr(i) = 9
keep = false
newSize = 4
[2][4][6][8][5][6][7][8][9]
[2][4][6][8]


[2][4][6][8]
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: