# Question concerning loops

• 04-13-2011, 12:28 PM
jim01
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.

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");               }   } }```
• 04-13-2011, 12:39 PM
ozzyman
change your logic to something like this:

Code:

```//array to hold the random numbers int[] randomNumbers = new int[1000]; //loop 1000 times for (int i=0; i<1000; i++) {     //generate randomInt     //(your code)     //save to array     randomNumbers[i] = randomInt; }```

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

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);```
• 04-13-2011, 12:43 PM
jim01
Quote:

Originally Posted by ozzyman
change your logic to something like this:

Code:

```//array to hold the random numbers int[] randomNumbers = new int[1000]; //loop 1000 times for (int i=0; i<1000; i++) {     //generate randomInt     //(your code)     //save to array     randomNumbers[i] = randomInt; }```

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

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.
• 04-13-2011, 12:49 PM
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.
• 04-13-2011, 01:09 PM
jim01
Quote:

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:

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.
• 04-13-2011, 01:17 PM
sunde887
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
Code:

```i++ Is the same as i = i + 1```
• 04-13-2011, 01:19 PM
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!

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.
• 04-13-2011, 01:57 PM
jim01
Quote:

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!

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.
• 04-13-2011, 03:17 PM
ozzyman
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.