Question

Below is an example of an algorithm that determines whether a number is even or odd....

Below is an example of an algorithm that determines whether a number is even or odd.

function is_even(number)
if number % 2 == 0 then
return True
otherwise
return False

Write a program that generates 100 random numbers (between 1 and 1000), and keeps a count of how many of those random numbers are even and how many are odd.
Implement the algorithm above (or your improved version) as a function, which is used to work out whether each number is odd or even.
When it’s done, your program must display how many numbers of each type were generated.

N.B the algorithm given is not particularly efficient. How could we simplify this to reduce the complexity? Hint: the decision uses Boolean values (the result of the comparison) to work out what Boolean value (True/False) to return. Can you reduce the number of Booleans?

Homework Answers

Answer #1


import java.util.*;
public class Main
{
   public static void main(String[] args) {
   // create instance of Random class
Random rand = new Random();
int rand_int1,i,even,odd,r;
even=odd=0;
// Generate random integers in range 0 to 999
for(i=0;i<100;i++)
{
rand_int1 = rand.nextInt(1000);
r=is_even(rand_int1);//sending the random variable to function
if(r==1)//if it returns 1 it is even
even++;
else //else it is odd
odd++;
  
}
System.out.println("Total no.of even numbers are "+even); //printing the even count
System.out.println("Total no.of odd numbers are "+odd); //printing the odd count
      
   }
   public static int is_even(int n) //function to determine even or odd
   {
if(n%2 == 0) //even
return 1; //returns 1
else //odd
return 0; //returns 0
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a function that returns a boolean value if all-elements are not odd or even. Using...
Create a function that returns a boolean value if all-elements are not odd or even. Using Dr. Racket and the How to Design a Function (HtDF ) recipe, create a function that will return the following check-expect: (check-expect (all-elements? even? (list 1 2 3) false) (check-expect (all-elements? even? (list 2 4 6) true) (check-expect (all-elements? odd? (list 1 3 5) true) Can you find an element in the list where the predicate fails (return false)?
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100 is surjective. That is, supply a “Method” to go with Input: A function (array) f with f(i) ∈ Z100 for i = 0, 1, . . . , 99. Output: Boolean B. B=‘true’ if f is surjective, ‘false’ otherwise. Try to make your algorithm as efficient as possible. Do NOT include an implementation of your algorithm in a programming language. (b) How many comparisons...
# Define a function that prints the word hello # Define a function that takes a...
# Define a function that prints the word hello # Define a function that takes a name as input argument # Then prints Hello and the name # Define a fucntion called my_function # That takes a boolean value (True or False) as an input argument # If the input is True print Hello # if the Input is False print Goodbye # Define a function called multiply # that takes two numbers as inputs # The funciton should return...
Write a program in python 3 that uses a custom function to generate a specified number...
Write a program in python 3 that uses a custom function to generate a specified number of random integers in a specified range. This custom function should take three arguments; the number of integers to generate, the lower limit for the range, and the upper limit for the range. Values for these arguments should be entered by the user in main. The custom function should display the random integers on one line separated by single spaces. The function should also...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Write a program in python that prints the count of all prime numbers between A and...
Write a program in python that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2,...
2. Using a “for loop” print all even numbers in rage from 1 to 1000. Also,...
2. Using a “for loop” print all even numbers in rage from 1 to 1000. Also, please count and print how many even numbers you have found. 3. Using the following list and a “for” loop, display differences of all consecutive pairs of numbers in the list. our_list = [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12] The output should look like this: 1 3 1 … 4. Using a “while loop” ask the user for a number and add it to a list if number is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT