Computer Science Canada shifting number in an array |
Author: | sammi [ Sat Oct 29, 2011 12:45 pm ] | ||
Post subject: | shifting number in an array | ||
What is it you are trying to achieve? <Make the first number 68 and get the rest of the numbers in my partially filled array to shift to the right> What is the problem you are having? <Can only get 2 of the number the move and the rest stay the same> Describe what you have tried to solve this problem <Answer Here> Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <Answer Here>
Please specify what version of Turing you are using <Answer Here> |
Author: | Insectoid [ Sat Oct 29, 2011 1:04 pm ] |
Post subject: | RE:shifting number in an array |
Quote: Can only get 2 of the number the move and the rest stay the same
Maybe if you do this often enough, all of them will have moved. |
Author: | Tony [ Sat Oct 29, 2011 1:52 pm ] | ||
Post subject: | RE:shifting number in an array | ||
adding elements to the beginning of an array is incredibly expensive (performance) and complicated (shifting all the other elements). Why would you want to do this? E.g. you can add the element at the end
All the elements preserve the same relative order as in your example, but the insert doesn't require any shifting of elements. |
Author: | Beastinonyou [ Sat Oct 29, 2011 2:28 pm ] | ||
Post subject: | Re: shifting number in an array | ||
However, If you really need to do it like that, you could use something as simple as this:
|
Author: | sammi [ Sat Oct 29, 2011 4:19 pm ] | ||
Post subject: | RE:shifting number in an array | ||
now im getting some error message that says "variable has no value" but i declared it above so i dont know what this means
|
Author: | Insectoid [ Sat Oct 29, 2011 4:44 pm ] | ||||
Post subject: | RE:shifting number in an array | ||||
"variable has no value" means you're trying to do something with a variable that has no value yet. When you declare a variable it doesn't yet have a value.
This won't execute. What is i? How can I add to i, if I don't know what it is?
Now we know that i is 0, so we can add to it no problem. Now, in your code you've created an array of random length between 8 and 14. So 'upper' is at least 8. You then go on to execute a for loop 4 times, filling in the first 4 values in the array and making the array 4 spaces bigger. The array is now between 12 and 18 slots in length. Then, you iterate over the entire array and try to output the value at each index in the array. But...we have at least 12 elements, and we only assigned 4 values. That leaves six elements with no value, that you are trying to print. And there, sir, is your error. |
Author: | sammi [ Sat Oct 29, 2011 4:53 pm ] |
Post subject: | RE:shifting number in an array |
so how would i input that into my code.....???? |
Author: | Beastinonyou [ Sat Oct 29, 2011 5:08 pm ] | ||||||
Post subject: | Re: shifting number in an array | ||||||
This:
Is your Problem You're starting your array with an upper bound of 8 to 12. Then later, you're adding 4 more. You need to start with 0.
And also, Why are you printing the array a third time?
fix your flexible array bounds, and run it. You'll see what I mean by the output window. |
Author: | evildaddy911 [ Mon Oct 31, 2011 3:07 pm ] | ||
Post subject: | Re: shifting number in an array | ||
heres a way that i figures out, you just need to have the computer know what each element is:
then you figure out what the new aray(1) is and your all set |