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
Write a complete program that will generate equally likely random shoe sizes in the inclusive range...
Write a complete program that will generate equally likely random shoe sizes in the inclusive range [5, 13.5], with whole or half sizes allowed. The number of values to generate will be input by the program user. It will also count and display the total number of times small (5, 5.5, 6, 6.5) and large (12, 12.5, 13, 13.5) sizes are generated. We have provided skeleton code for you to use. You must implement the helper method as given in...
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
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...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT