1. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0

## Question concerning loops

I have to write a program that generates 1000 random integers between 1 and 10 and finds how many are less than or equal to 5 and how many are more than five. Here is the code I have written, which is not right. The problem is that I cannot figure out what I am doing wrong. I think it might have to do with the i++ I have in the for statement, but I am not sure. I do not know what to replace it with and if I leave it blank the program becomes an infinite loop.

Java Code:
```public class PracticeExercise2A {

public static void main(String[] args){

//generate random number between one and ten one thousand times
//perform for loop
for(int i = (int)(Math.random()*(10) + 1); i <= 1000; i++){

if(i <= 5)

System.out.println("There are " + i + " numbers less than or equal to 5");

else

System.out.println("There are " + i + " numbers greater than 5");

}
}
}```

2. change your logic to something like this:

Java Code:
```//array to hold the random numbers
int[] randomNumbers = new int[1000];

//loop 1000 times
for (int i=0; i<1000; i++) {
//generate randomInt
//save to array
randomNumbers[i] = randomInt;
}```

then have your methods to get how many are less than 5:

Java Code:
```int countMore5;
for (int i=0; i<randomNumbers.length; i++) {
//if its greater than 5, increment our counter
if (randomNumbers[i] > 5) {
countMore5++;
}
}

//print result
System.out.println("Occurences greater than 5: " + countMore5);
System.out.println("Occurences less than or equal to 5: " + randomNumbers.length-countMore5);```

3. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by ozzyman
change your logic to something like this:

Java Code:
```//array to hold the random numbers
int[] randomNumbers = new int[1000];

//loop 1000 times
for (int i=0; i<1000; i++) {
//generate randomInt
//save to array
randomNumbers[i] = randomInt;
}```

then have your methods to get how many are less than 5:

Java Code:
```int countMore5;
for (int i=0; i<randomNumbers.length; i++) {
//if its greater than 5, increment our counter
if (randomNumbers[i] > 5) {
countMore5++;
}
}

//print result
System.out.println("Occurences greater than 5: " + countMore5);
System.out.println("Occurences less than or equal to 5: " + randomNumbers.length-countMore5);```
We haven't made it to arrays yet, but I will rearrange the random.math method in my for statement like you have it to see what it does.

4. Oh i see, in that case just move the counter into the first for loop that generates the numbers. So you don't save the numbers in an array, but you count the occurences of numbers greater than 5 while the numbers are being generated.

5. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by ozzyman
Oh i see, in that case just move the counter into the first for loop that generates the numbers. So you don't save the numbers in an array, but you count the occurences of numbers greater than 5 while the numbers are being generated.
By counter, I am assuming you mean i <= 5. I'm not sure where I put it. The way I have it now is:

Java Code:
`for(int i = (int)(Math.random()*(10) + 1); i <= 1000; i++)`
If I am understanding things correctly, expression one is " int i = (int)(Math.random()*(10) + 1)" and assigns a random number to i; expression two is "i <= 1000" and is a Boolean condition that executes the body of the loop 1000 times, and expression three is "i++" and modifies expression one, in this case incrementing i.

If I replace i <= 5 with expression one, it won't generate the random number. If I replace i <= 5 with expression two it will only repeat five times instead of 1000. if I replace it with expression three, the program won't compile.

6. i <= 5 is the test. A counter is a variable which you add 1 to every time the test is met. So if the number is less than or equal to 5 you add 1 to a variable declared before the loop. By the way
Java Code:
```i++

Is the same as

i = i + 1```

7. by counter, what i meant was:

int counter;

if (i<=5) counter++;

take that random number out of your for loop statement, it looks crazy!

Java Code:
```int randomInt,count;
//loop 1000 times
for (int i=0; i<1000; i++) {
//now make random int
randomInt = (int) (Math.random()*(10)+1);
//check what we generated
if (randomInt <=5) count++;
}```
after the loop is finished, your 'count' counter will hold the number of instances that the number generated was less than or equal to 5.

8. Member
Join Date
Sep 2010
Location
Southwest Missouri
Posts
97
Rep Power
0
Originally Posted by ozzyman
by counter, what i meant was:

int counter;

if (i<=5) counter++;

take that random number out of your for loop statement, it looks crazy!

Java Code:
```int randomInt,count;
//loop 1000 times
for (int i=0; i<1000; i++) {
//now make random int
randomInt = (int) (Math.random()*(10)+1);
//check what we generated
if (randomInt <=5) count++;
}```
after the loop is finished, your 'count' counter will hold the number of instances that the number generated was less than or equal to 5.
OK, I got it. Jeez, I'm not sure how I was supposed to figure that out with just the one simple example given to us by the professor. It's kind of like trying to figure out a calc problem with an algebra example.

So, the way this program is written now, it scrolls through and prints each random number one at a time and decides whether it is greater than or equal to or less than 5. Is it possible at this early stage of my programming class to make the program only print the total number of times it was less than or equal to five and the total number of times it is greater than five?

Thank you very much for your help. I have to run and go to school now, but will check back in later this afternoon.

9. Yes, you wasn't supposed to print the total in the for loop. you need the counter in the loop so that it checks & counts for each of the 1000 integers, but the print doesnt need to be done 1000 times. so Outside of the for loop, after the loop has finished, you should print the total.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•