Results 1 to 9 of 9
- 03-14-2011, 11:34 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Pick random array index and change the element value
Supposin' you have an array
{1,2,3,4,5,6,7,8,9,10}.
Also supposin' you want to randomly pick an index in that array and set the element of that random index to the value of 5.
Now, simply print out the number of the changed index and:
Lastly Supposin'
you then want to add up all the elementsof the changed array to get the sum.
example:
index 3 was randomly picked. the value of index 3's element is changed from 4 to 5.
When ran, the program then displays :
4 - 57
for the life of me i can't seem to find out how to choose a random index and manipulate its element.
I don't have any useful code for this so far, because i simply can't get past this whole 'random index' business.
Any help is greatly appreciated.Last edited by Space Ranger; 03-14-2011 at 11:42 PM. Reason: used wrong number
- 03-14-2011, 11:40 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Random randnum = new Random(10);
yourarray[randnum] = 5;
then just do a for loop to run through the array and add the value of it to an integer i.e. sum += yourarray[counter];
- 03-14-2011, 11:45 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thank you.
I'll give it a whirl
- 03-21-2011, 08:45 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Still can't get it
Thanks for the help.
but i still can't seem to get this one going.
I've searched high and low and i still can't seem to get this lil bit of code to go.
for the sake of clarity, i should correct my former post.
if you have an array {1,2,3,4,5,6,7,8,9,10} and you want to pick a random index and then change the value of the element of that random index to 5 and then add all of the elements of the resultant array to get a sum.
example:
the code randomly selects index 3, which it then changes that element value to 5 and then prints out the index changed and then the sum of the array.
to get:
3 - 56 (i messed up both these number in the previous post).
i tried what was suggested, but no go.
I'm not necessarily looking for the code to be written for me. but a good hard shove in the right direction would be appreciated.
Thanks for putting up with me.Last edited by Space Ranger; 03-21-2011 at 08:46 PM. Reason: bad spelling
- 03-22-2011, 03:37 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
im still a little confused :o
10+9+8+7... = 55, and if you mean adding the array's new elements it would be
1+2+5+4+5... = 57, when you changed index 3 to 5
printing the index changed should be pretty simple, so ill try to help with the other problem...which i dont fully understand :p whats the specific problem? or any of the wrong output?
- 03-22-2011, 03:41 AM #6
That provides zero information. What was suggested was exactly what you needed. If you didn't get it to work then you did something wrong. What did you do wrong? We have no idea since you did not provide relevant infirmation. Such as code, exceptions, incorrect output.
- 03-22-2011, 04:51 AM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
I'm sorry to say this is wrong and this is the reason why it is still not working. Have you ever tried printing the value of "randnum"? It is not an integer. It is an object. You cannot index an array using an object.
A much straight forward way will be
PS : this is applicable in a case where you need to generate a number between 0 and 10. Which means, your array should be of size 11. If you want to do it on a 10 size array, then you can do something like this.Java Code:int randNum = (int)(Math.random()*10); youarray[randNum] = 5;
But this is not such an efficient way to generate a random number because the probability of 1 is higher than other numbers.Java Code:int randNum = ((int)(Math.random()*10))%9; youarray[randNum] = 5;
So better refer to Random API and try nextInt(int n). I think this is a perfect solution.
Random (Java 2 Platform SE v1.4.2)
- 03-22-2011, 04:53 AM #8
Yes well spotted. I missed that the code provided never called the nextInt method.
[slaps wrist]
- 03-29-2011, 10:18 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Sorry. i'm fairly new to this stuff
I thank you all for your help.
the problem i had with the 'yourarray[randnum] = 5;' was that the compiler gave me an 'incompatible types' error.
i searched high and low to find out what i was doing wrong, but in the end, i simply gave up.
I'll definitely try Subith86's solution.
I'm not trying to be deliberately vague. the very heart of my little program is getting this random index operation going. until i do, i have no useful code to show all of you.
Thank you again for your help and patience.
Similar Threads
-
how to change a single index of a string
By ftrengnr in forum New To JavaReplies: 5Last Post: 11-26-2010, 04:08 AM -
Change of index in charArray
By kovalensue in forum New To JavaReplies: 19Last Post: 11-10-2010, 11:34 AM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks