• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with arrays and loop  RSS feed

 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am trying to make a java application with arrays and loop. Im supposed to roll one dice 1000000 times and print out how many times the result will be higher than 12 when you throw the dice.
so 13,14,15,16,17, and up to 18
So my application has to tell how many times the result becomes 13, and 14, and 15 and so on. Im a bit stuck because i dont know how to continue. Im confused about my loop as well, how am i going to loop 1000000 times and get out of the loop when the throw is larger than 12?

My code so far :



any help is appriciated :)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what is the result array is going to contain?

Why are you initializing it with 13, 14, 15 etc. (lines 11-16) like that? What do these values mean?

Why does the loop in line 18 start with i = 13?

There's a semi-colon at the end of line 18 that should not be there.

What's line 19 supposed to do? Remember that result is an array of integers. What are you trying to assign to result in line 19?

If you throw one die, then how can the result ever be higher than 6? What do you mean by "how many times the result will be higher than 12"?
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Can you explain what is the result array is going to contain?

Why are you initializing it with 13, 14, 15 etc. (lines 11-16) like that? What do these values mean?

Why does the loop in line 18 start with i = 13?

There's a semi-colon at the end of line 18 that should not be there.

What's line 19 supposed to do? Remember that result is an array of integers. What are you trying to assign to result in line 19?


Hello and thanks for reply.

My thought is that since the application has to display the values bigger than 12 i started from 13 to 18.
I thought first id start from 1-18 and create arrays for all those values but isnt that unnecessary? Ok so i should rename my array then? Lets say ill name it dice
Line 19 is supposed to generate random numbers between 1-6 since its a dice.
The application is supposed to throw the dice 1000000 times and see how many times you get a result higher than 12. Is it more clear and understandable. Sorry,im a newbie
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:The application is supposed to throw the dice 1000000 times and see how many times you get a result higher than 12.

What exactly is the result you are talking about? When you throw a die, the result will always be a number between 1 and 6, right? How can this ever be higher than 12?

Before you even think about writing a program, make sure you understand exactly what the assignment is, and make sure you have an idea that you can describe in plain English (or your own native language) how to solve it.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Patricia Andersen wrote:The application is supposed to throw the dice 1000000 times and see how many times you get a result higher than 12.

What exactly is the result you are talking about? When you throw a die, the result will always be a number between 1 and 6, right? How can this ever be higher than 12?

Before you even think about writing a program, make sure you understand exactly what the assignment is, and make sure you have an idea that you can describe in plain English (or your own native language) how to solve it.


This is the assignment

Alice and Bob discuss vividly how the outcome would be if they repeatedly would throw
a dice x number of times until the sum is greater than twelve (≥ 12). Alice claim 13 is the
most probable sum, while Bob claims it is 15.
Write an application to simulate the process a million times and produce a table of
the percentage of times that the sum is 13, 14, 15, 16, 17 and 18 respectively.


Do you understand it better now? How the outcome can be greater than 6... ? Well, if you think about it, it CAN!
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:Alice and Bob discuss vividly how the outcome would be if they repeatedly would throw
a dice x number of times until the sum is greater than twelve (≥ 12).

You didn't tell us that before.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Patricia Andersen wrote:Alice and Bob discuss vividly how the outcome would be if they repeatedly would throw
a dice x number of times until the sum is greater than twelve (≥ 12).

You didn't tell us that before.


i think i did if you look further up. the x number of times is actually a million times, so where is the difference? Anyhow, if no one can help me than thank you anyway but ill look around somewhere else
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patricia Andersen wrote:
So my application has to tell how many times the result becomes 13, and 14, and 15 and so on. Im a bit stuck because i dont know how to continue. Im confused about my loop as well, how am i going to loop 1000000 times and get out of the loop when the throw is larger than 12?


Throw. Singular. Not "sum of dice rolls". you should have said that you are summing up the rolls.
We're happy to help you, but you need to give us the correct details for us to be able to help you.

By the way: Since the value of a die roll is at least one, you never need more than 12 rolls to get a sum of 12. There's no need to do it a million times.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Patricia Andersen wrote:Alice and Bob discuss vividly how the outcome would be if they repeatedly would throw
a dice x number of times until the sum is greater than twelve (≥ 12).

You didn't tell us that before.


allright, i see, i didnt write repetedly. My bad, sorry
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My interpretation is this:
process = Throw a dice any number of times and add the results until the sum on results is 13 or more
execute process one million times.

Well, it would be better if OP just stated clearly her requirements.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
Patricia Andersen wrote:
So my application has to tell how many times the result becomes 13, and 14, and 15 and so on. Im a bit stuck because i dont know how to continue. Im confused about my loop as well, how am i going to loop 1000000 times and get out of the loop when the throw is larger than 12?


Throw. Singular. Not "sum of dice rolls". You should have said that you are summing up the rolls.
We're happy to help you, but you need to give us the correct details for us to be able to help you.

By the way: Since the value of a die roll is at least one, you never need more than 12 rolls to get a sum of 12. There's no need to do it a million times.


I see but the thing is this is what the assignment is about. I have to print out how many times of these 1000000 throws we get 13,14,15,16,17 and 18.
Maybe this assignment is confusing, it certainly is for me.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:My interpretation is this:
process = Throw a dice any number of times and add the results until the sum on results is 13 or more
execute process one million times.

Well, it would be better if OP just stated clearly her requirements.


Im sorry if i didnt explain it all ok but i did write you the assigment so that you could understand what i mean since someone here stated my english wasnt really that good ;)
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, let's have a look at your actual problem here.

You need to have two loops, at least. One to count from one to one million, and one to roll the dice until the sum becomes >= 12. Do you know how to go from there?
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Okay, let's have a look at your actual problem here.

You need to have two loops, at least. One to count from one to one million, and one to roll the dice until the sum becomes >= 12. Do you know how to go from there?


thanks, this is what i suspected. But is it ok to write one loop after the other?
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Okay, let's have a look at your actual problem here.

You need to have two loops, at least. One to count from one to one million, and one to roll the dice until the sum becomes >= 12. Do you know how to go from there?


ps. do i need to change my arrays? Should i put values from 1-18? instead of 13-18?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One inside the other, actually (that means the inner loop inside the curly braces of the outer loop) . If you want to read up on this, try googling for nested loops in Java.

It would be easier if you had 18 fields in your array, then you could, once the sum exceeds 11, just say "array[sum-1]++". But it would waste some space, since you wouldn't need 0 through 10.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:One inside the other, actually (that means the inner loop inside the curly braces of the outer loop) . If you want to read up on this, try googling for nested loops in Java.

It would be easier if you had 18 fields in your array, then you could, once the sum exceeds 11, just say "array[sum-1]++". But it would waste some space, since you wouldn't need 0 through 10.


Ive heard about nestled loops, ill google it
ill give it a try. Thanks for pushing me into the right direction. I might come back here if i get stuck again if thats ok
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't finde the edit button in this layout, so sorry.
7 Values in your array would be sufficient, instead of result[sum-1], you can say [sum-11] as index. Hadn't thought of this while writing my last post.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Couldn't finde the edit button in this layout, so sorry.
7 Values in your array would be sufficient, instead of result[sum-1], you can say [sum-11] as index. Hadn't thought of this while writing my last post.


Ok guys i think i did it but im not sure that the results are really correct but on the other hand why not.
So here is the code :



So im sorry i have some swedish comments in the code but you know what its all about now so... anyway.. the results i get are following but with an error among the results. I tried to google the error but dont really know what it means anyway.



run:
12 = 16784
13 = 16549
14 = 16563
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
15 = 16704
16 = 16619
17 = 16868
18 = 18
at TärningsArray.main(TärningsArray.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:7 Values in your array would be sufficient, instead of result[sum-1], you can say [sum-11] as index. Hadn't thought of this while writing my last post.

Just as well, because it's wrong. The assignment was to throw the dice until the result is greater than 12, so there are only 6 possible results which can either, as you almost said first, be stored in an array of 19 (because the total may be up to 18 and indexes start at 0) - my slight preference because it's simple - or in an array of 6 using sum - 13.

@Patricia: Why are you initializing your array with the value of the total? It's supposed to contain counts of the number of times a particular total turns up, so they should all start at 0. Also:
  • the result of your dice throw is wrong.
  • you're adding to a count after every throw.

  • My advice: StopCoding (←click), and write out the process in English (or your native language). And don't write one more line of Java code until you have it described completely.

    Winston
     
    Patricia Andersen
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Jan Hoppmann wrote:7 Values in your array would be sufficient, instead of result[sum-1], you can say [sum-11] as index. Hadn't thought of this while writing my last post.

    Just as well, because it's wrong. The assignment was to throw the dice until the result is greater than 12, so there are only 6 possible results which can either, as you almost said first, be stored in an array of 19 (because the total may be up to 18 and indexes start at 0) - my slight preference because it's simple - or in an array of 6 using sum - 13.

    @Patricia: Why are you initializing your array with the value of the total? It's supposed to be a count of the number of times a particular total turns up, so they should all start at 0. Also:
  • the result of your dice throw is wrong.
  • you're adding to a count after every throw.

  • My advice: StopCoding (←click), and write out the process in English (or your native language). And don't write one more line of Java code until you have it described completely.

    Winston


    Yes thanks, its a good advisable idea, but this has to be finished tomorrow, so im quite stressed and nervous.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Patricia Andersen wrote:Yes thanks, its a good advisable idea, but this has to be finished tomorrow, so im quite stressed and nervous.

    Well I suspect that means that you've left things to the last minute, and we can't really help you out there, except to advise you to use your time better next time.

    However, what would you prefer? To do it properly, or hand in something that will either:
    1. Probably not work.
    2. Work just fine, but you won't be able to explain why, because you don't understand yourself.

    You cannot solve problems by coding; you can only solve them by understanding them.

    Winston
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You should also consider breaking the problem down into several components, rather than trying to do it all at once.

    For example, these things are all independent of each other:

    rolling a die to get a value from 1-6
    Looping 1,000,000 times and doing something
    keep rolling a die, totaling the values until it is greater than 12, and counting the number of rolls needed
    keeping track of how many times it took 'X' rolls

    so...you may want to write a method for each of these things. Write a method that rolls a die and returns the value 1-6. Once that works, you've compiled and test it THOROUGHLY, then write a method that call that method continuously, keeping track of the sum, until the sum is greater than 12. have it return how many times you had to roll the die.

    Then write a method that takes a value in, like 14, and increments how many times it gets that value.

    There are other pieces you need, like the computation of the percentages, the output, etc. But if you break it down into teeny, tiny pieces, it is easier to code, maintain, debug, and update.
     
    Patricia Andersen
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Patricia Andersen wrote:Yes thanks, its a good advisable idea, but this has to be finished tomorrow, so im quite stressed and nervous.

    Well I suspect that means that you've left things to the last minute, and we can't really help you out there, except to advise you to use your time better next time.

    However, what would you prefer? To do it properly, or hand in something that will either:
    1. Probably not work.
    2. Work just fine, but you won't be able to explain why, because you don't understand yourself.

    You cannot solve problems by coding; you can only solve them by understanding them.

    Winston


    Well i do understand but i dont understand why i got this error. Isnt this the way you learn as well?
    I didnt leave it to the last minute, my exam started today at 10 am and stops tomorrow at 10am. 24 hours.
    So believe it or not, im trying the whole day and i think i finally kind of made it, and i see there is an error. What you do is keep trying, and learn on the way. I never asked anyone to make my homework :-)
    But when one is stuck the only way to learn is to ask, fix in the code and understand the problem and then learn from it. Am i wrong? Thanks anyway
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Patricia Andersen wrote:But when one is stuck the only way to learn is to ask, fix in the code and understand the problem and then learn from it. Am i wrong?

    Yes. You don't (or shouldn't) "fix in the code". In fact, you shouldn't write any code until you understand what you're trying to do.

    Follow Fred's advice. He's laid out the tasks for you very nicely; and the whole business of programming is to break down a problem into small, simple tasks.

    I've already given you a few tips myself, so the only other thing I can add to Fred's advice is to remind you that Java indexes start at 0; so if you need to add to a count at index 18, then your array must have at least 19 elements. If you only have 6 elements, then they must be indexed from 0 to 5.

    However, none of this has anything to do with your problem: You need to keep throwing a dice until the total of the values you get is greater than 12, and record the result.
    Personally, I'd start out with that, and only worry about updating counts based on that result when you KNOW that portion of your program is working.

    Winston

    PS: I know it's frustrating, but believe me you'll be over the moon when you get it right.
     
    Jan Hoppmann
    Ranch Hand
    Posts: 147
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Just as well, because it's wrong. The assignment was to throw the dice until the result is greater than 12, so there are only 6 possible results which can either, as you almost said first, be stored in an array of 19 (because the total may be up to 18 and indexes start at 0) - my slight preference because it's simple - or in an array of 6 using sum - 13.


    You're right, of course (I remembered that the >= sign was used in the assignment, but it differs from what is stated in the text). My approach with 18 fields would have been right, because I use sum - 1 as index every time . But of course, 19 fields is easier. I should avoid posting while having the flu ;)
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jan Hoppmann wrote:I should avoid posting while having the flu ;)

    Sorry to hear. My cure-all: hot rum and grapefruit juice, with a big spoon of honey. It must be dark rum, and be sure not to boil it because you'll destroy the Vitamin C; but it's waaay better than Night Nurse.

    Get well soon.

    Winston
     
    Jan Hoppmann
    Ranch Hand
    Posts: 147
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Jan Hoppmann wrote:I should avoid posting while having the flu ;)

    Sorry to hear. My cure-all: hot rum and grapefruit juice, with a big spoon of honey. It must be dark rum, and be sure not to boil it because you'll destroy the Vitamin C; but it's waaay better than Night Nurse.

    Get well soon.

    Winston


    Thank you, and thanks for the tip. I'll try it tonight
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!