This site requires JavaScript, please enable it in your browser!
Greenfoot back
Miguel.Valdez
Miguel.Valdez wrote ...

2018/4/10

loops

1
2
Miguel.Valdez Miguel.Valdez

2018/4/10

#
Im having a struggling time trying to get with some loops and quite understand how i could possibly make this happen. I believe i am on the right track. Calculate the average of 1000 random numbers from 0 to 999. Use variables to define these the 2 bolded numbers. Print out the result as: “The average of <insert value> random numbers from 0 to <insert value> is: <insert value>”
1
2
3
4
5
6
7
8
9
10
11
12
private int average;
private int maxNumber;
{
    average = 1000;
    maxNumber = 999;
    for (i=0; i<=maxNumber; i++)
    {
        System.out.print();
    }
    System.out.print("the average of //variable average// random numbers from 0 to //variable maxNumber// is ________");
    System.out.println();
}
danpost danpost

2018/4/10

#
First, I see a block of code with no declaration line (the block from line 3 through to line 12). Then, I see average as being a misnamed field. The 1000 is the number of random choices to be made -- not the average of anything. Next, I see a print line inside the loop -- nothing says to print anything other than the final result. Now, for what I do not see -- no random values being chosen; no variable to hold the running sum of chosen numbers (to get the average, we add all chosen numbers and divide by how many were chosen). While making any adjustments you can make, you might rethink the condition in the for statement.
Miguel.Valdez Miguel.Valdez

2018/4/10

#
am i going to have to use "Greenfoot.getRandomNumber();"?
danpost danpost

2018/4/10

#
Miguel.Valdez wrote...
am i going to have to use "Greenfoot.getRandomNumber();"?
You will have to use something to generate a random value. That would be a perfectly good option.
Miguel.Valdez Miguel.Valdez

2018/4/10

#
now im guessing something along this line?
1
2
3
4
5
6
7
8
9
10
private void averageOf1000()
{
    int total = 0;
    int randomNumber = Greenfoot.getRandomNumber(1000);
    for (i=0; i<=randomNumber; i++)
    {
        //not sure what to insert
    }
    System.out.print("the average of <insert value> random numbers from 0 to <insert value> is <insert value>");
    System.out.println();
Im barely getting ahold of loops with plain addition and slight multiplication. But now trying to do an average (which would include division). Im not really sure how to approach this loop.
danpost danpost

2018/4/10

#
Miguel.Valdez wrote...
now im guessing something along this line? << Code Omitted >> Im barely getting ahold of loops with plain addition and slight multiplication. But now trying to do an average (which would include division). Im not really sure how to approach this loop.
Well, the number of times the loop should iterate is how many random numbers you want to generate and the random number should be generated inside the loop and added to the total.
Miguel.Valdez Miguel.Valdez

2018/4/10

#
is this correct or along the lines of: Calculate the average of 1000 random numbers from 0 to 999. Use variables to define these the 2 bolded numbers. Print out the result as: “The average of <insert value> random numbers from 0 to <insert value> is: <insert value>”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private int totalNumbers;
private int maxNumber;
 
private void averageOf1000()
{
    int total = 0;
    int totalNumbers = 1000;
    int maxNumber = 999;
    for (int i=Greenfoot.getRandomNumber(totalNumbers); i<=maxNumber; i++)
    {
        total %= i;
    }
    System.out.print("the average of " +totalNumbers+ " random numbers from 0 to " +maxNumber+ " is " +total+ ".");
    System.out.println();
danpost danpost

2018/4/10

#
Miguel.Valdez wrote...
is this correct or along the lines of: Calculate the average of 1000 random numbers from 0 to 999. Use variables to define these the 2 bolded numbers. Print out the result as: “The average of <insert value> random numbers from 0 to <insert value> is: <insert value>” << Code Omitted >>
There is nothing random about how many times to iterate the loop or what values to iterate through. The thing that is random are the chosen numbers that are to be added to the total. The average can only be acquired after all the chosen numbers are added together. Remove lines 7 and 8 and assign values to the fields declared on lines 1 and 2.
Miguel.Valdez Miguel.Valdez

2018/4/10

#
1
2
3
4
5
6
7
8
9
10
11
12
13
static int totalNumbers = 1000;
static int maxNumber = 999;
 
private void averageOf1000()
{
    int total = 0;
    for (int i=0; i<=maxNumber; i++)
    {
        total = total + i;
    }
    System.out.print("the average of " +totalNumbers+ " random numbers from 0 to " +maxNumber+ " is " +total+ ".");
    System.out.println();
}
When running this I get this "the average of 1000 random numbers from 0 to 999 is 499500." I would then somehow need to divide to get the average, no?
danpost danpost

2018/4/10

#
Miguel.Valdez wrote...
When running this I get this "the average of 1000 random numbers from 0 to 999 is 499500." I would then somehow need to divide to get the average, no?
Why did you make the fields static? Also, line 7 is using the wrong field and you seem to have lost your random number chooser (Greenfoot.getRandomNumber), which was previously misplaced and given an incorrect argument (the value may have ended up right, but how you got it was not). And yes, you will need to divide the final total by the number of random values that were added together.
Miguel.Valdez Miguel.Valdez

2018/4/10

#
im really starting to draw blanks... this is what im thinking to do now. once all the random numbers have been added, then the total is divided by the totalNumbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
private void averageOf1000()
{
    int total = 0;
    int totalNumbers = 1000;
    int maxNumber = 999;
    for (int i=Greenfoot.getRandomNumber(totalNumbers); i<=maxNumber; i++)
    {
        total = total + i;
    }
    total = total/totalNumbers;
    System.out.print("the average of " +totalNumbers+ " random numbers from 0 to " +maxNumber+ " is " +total+ ".");
    System.out.println();
}
"the average of 1000 random numbers from 0 to 999 is 466."
danpost danpost

2018/4/10

#
Miguel.Valdez wrote...
im really starting to draw blanks... this is what im thinking to do now. << Code Omitted >>
Your loop counter does not start at some random number -- it starts at zero and iterates totalNumbers times. The random numbers need to be added to your running total (the total variable).
once all the random numbers have been added, then the total is divided by the totalNumbers.
You got that part right (line 10). However, you may want to include some fractional part of the average:
1
double average =(double)(total*100/totalNumbers)/100d;
Miguel.Valdez Miguel.Valdez

2018/4/10

#
I just realized that it said 0 to 99: "Calculate the average of 1000 random numbers from 0 to 99. Use variables to define these the 2 bolded numbers. Print out the result as: “The average of <insert value> random numbers from 0 to <insert value> is: <insert value>”" This should really change anything besides the maxNumber, no? this is what i come to think of now then. Not sure if it is just getting worse though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void averageOf1000()
{
    int total = 0;
    int totalNumbers = 1000;
    int maxNumber = 99;
    for (int i=0; i<=maxNumber; i++)
    {
        total = total + i;
        //random number of totalNumbers 0 to 99 is then added onto the total.
        total = total + Greenfoot.getRandomNumber(totalNumbers);
    }
    double average =(double)(total*100/totalNumbers)/100d;
    System.out.print("the average of " +totalNumbers+ " random numbers from 0 to " +maxNumber+ " is " +total+ ".");
    System.out.println();
As for "double" that is something that we have not been taught in class & when i looked up "double" in the index of the greenfoot book. Nothing appeared. Where could i go to get a better knowledge of how "double" works?
danpost danpost

2018/4/11

#
Remove line 8 -- the loop counter is not to be averaged in. You can get a lot of information about a lot of things in the java tutorials. See this page to find out about primitive types..
Miguel.Valdez Miguel.Valdez

2018/4/12

#
hey Danpost, so my code looks like this now.
1
2
3
4
5
6
7
8
9
10
11
12
13
private void averageOf1000()
{
    int total = 0;
    int numbers = 99;
    int maxNumber = 1000;
    for (int i=0; i<=maxNumber; i++)
    {
        total += Greenfoot.getRandomNumber(numbers+1);
    }
    int average = total / maxNumber;
    System.out.print("the average of " +maxNumber+ " random numbers from 0 to " +numbers+ " is " +average+ ".");
    System.out.println();
}
so my int numbers is 99 and being called into the random number parameter. that would mean that only a random number between 0-98 are being used.... should I do
1
Greenfoot.getRandomNumber(numbers +1);
my theory is that it would be 99+1 = 100, so then numbers 0-99 are being used?
There are more replies on the next page.
1
2