Question

Write a script that will generate seven random integers in the range [-100:200] inclusive and then...

Write a script that will generate seven random integers in the range [-100:200] inclusive and then print the value of each integer and whether it is positive or negative and whether it is even or odd.

An output from this program might be:

-57 is negative and odd.

26 is positive and even.

If zero is one of the random integers generated, remember that zero is even and neither negative or positive.

Hint: even numbers are divisible by 2 with no remainder.  

Solve it in MATLAB

Homework Answers

Answer #1

Answer

Here is your answer, if you have any doubt please comment, i am here to help you. Here is the matlab code for finding the above problem. Here we can use randi() built in method for creating the random numbers. And we can use mod() to find the reminder. So here is the code

%creating 7 random numbers including -100 and 200 , which 1x7 vector.
%we can use randi() method to generate random numbers
%Uniformly distributed pseudorandom integers
r = randi([-100,200],1,7);
%iterate through each values in r and check weather it is +ve or -ve and
%even or odd
for i=1:numel(r)
    if r(i) <0
        %use modulo operator to find the reminder
        if mod(r(i),2)==0
            fprintf(" %d is negative and even\n ",r(i));
        else
             fprintf(" %d is negative and odd\n ",r(i));
        end
    elseif r(i) > 0
        if mod(r(i),2)==0
            fprintf(" %d is postive and even\n ",r(i));
        else
             fprintf(" %d is positive and odd\n ",r(i));
        end
    else
        fprintf("zero is even and neither negative or positive");
    end
end

output

i put comment in the code, any doubt please comment

Thanks in advance

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
Using the Java language program Demonstrate multiway if statement 1.1 Generate an integer random number from...
Using the Java language program Demonstrate multiway if statement 1.1 Generate an integer random number from -5 to 5.(Hint: Use Math.random()) 1.2 Write a multiway if statement that will display whether the number is positive, negative or zero. 1.3 Be sure to test your code with all three cases
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
1. A random number generator is used to select a number from 1 to 500 ?(inclusively)....
1. A random number generator is used to select a number from 1 to 500 ?(inclusively). What is the probability of selecting the number 595 ? What is the probability? 2.Identify the sample space of the probability experiment and determine the number of outcomes in the sample space. -Randomly choosing an even number between 10 and 20, inclusive The sample space is? (Use a comma to separate answers) There are _____ outcomes in the sample space 3. Determine the number...