Computer Science Canada Array Subscript is out of range. What does that mean? Help |
Author: | mew123mew [ 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:
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. |
Author: | Zren [ 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.
Output:
|
Author: | mew123mew [ 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. |
Author: | Zren [ 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. |
Author: | mew123mew [ Sun Feb 15, 2015 12:25 am ] |
Post subject: | Re: Array Subscript is out of range. What does that mean? Help |
Oh Thanks! ![]() 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. |
Author: | Raknarg [ 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. |
Author: | mew123mew [ 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? |
Author: | Zren [ Sun Feb 15, 2015 3:23 pm ] | ||
Post subject: | RE:Array Subscript is out of range. What does that mean? Help | ||
|
Author: | mew123mew [ 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. |
Author: | Raknarg [ 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. |
Author: | mew123mew [ 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:
The thing is, it keeps on removing the element that has a subscript of 2, and not any others |
Author: | Insectoid [ 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. |
Author: | mew123mew [ 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 |
Author: | Zren [ 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.
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.
|