Question

MATLAB QUESTION

8) Create a program that first prompts the user to enter any two numbers. Then prompt the user again to input as many numbers

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In the question, whether to take the -1 as a element of array or not is not mentioned. So I consider the both cases.

Not considering the -1 as an element in the array x:

x=[];
fprintf('First enter two numbers x(1) and x(2)\n');

x(1) =input('');

x(2) = input('');

fprintf('Now enter as many numbers as you''d like, enter -1 to kill program.\n');

i=2;
while ~(x(i)==-1)
    i=i+1;
    x(i) = input('');
end

len = length(x)-1;
x=x(1:len);

for i=1:len
    for j=1:len-i
        if x(j)>x(j+1)
            dup=x(j);
            x(j)=x(j+1);
            x(j+1)=dup;
        end
    end
end

fprintf('The largest number is: %d\n',x(end));
fprintf('The second largest number is: %d\n',x(end-1));

We make use of bubble sorting operation to sort the array. So the largest and second largest number lies at the end , and second last of the array.

Considering -1 as a input:

x=[];
fprintf('First enter two numbers x(1) and x(2)\n');

x(1) =input('');

x(2) = input('');

fprintf('Now enter as many numbers as you''d like, enter -1 to kill program.\n');

i=2;
while ~(x(i)==-1)
    i=i+1;
    x(i) = input('');
end
len=length(x);

for i=1:len
    for j=1:len-i
        if x(j)>x(j+1)
            dup=x(j);
            x(j)=x(j+1);
            x(j+1)=dup;
        end
    end
end

fprintf('The largest number is: %d\n',x(end));
fprintf('The second largest number is: %d\n',x(end-1));

Although output is not effected by this. Output is common for both the cases.

Some results of the above code.

Hope you got the answer. If you have any doubts regarding this answer please try to comment, I will help you with that.

Thank you and don't forget to upvote the answer.

Add a comment
Know the answer?
Add Answer to:
MATLAB QUESTION 8) Create a program that first prompts the user to enter any two numbers....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • write a java program that prompts a user to enter two different numbers, divide the first...

    write a java program that prompts a user to enter two different numbers, divide the first number by second and prints the result

  • 1) Write a MATLAB script to prompt a user to input two numbers, then assess whether...

    1) Write a MATLAB script to prompt a user to input two numbers, then assess whether the two numbers are equal or not. If the two numbers are equal, display that they are equal in the Command Window. If they are not equal, display that they are not equal in the Command Window. 2) Write a MATLAB script that prompts the user to enter the day of the week using the input function. If the information the user enters is...

  • How do I solve using Matlab? Use MATLAB to create a pattern such as the one...

    How do I solve using Matlab? Use MATLAB to create a pattern such as the one below in the command window. Prompt the user to enter the max number of columns and number of intervals ran. In the left figure, the user selected 10 max columns and one interval. In the right figure, the user selected 5 max columns and two intervals. The program should run for n max columns and n number of intervals. **** ***** ***** *****

  • 7) Use MATLAB to create a pattern such as the one below in the command window....

    7) Use MATLAB to create a pattern such as the one below in the command window. Prompt the user to enter the max number of columns and number of intervals ran. In the left figure, the user selected 10 max columns and one interval. In the right figure, the user selected 5 max columns and two intervals. The program should run for n max columns and n number of intervals. ******* ** **** *** ***** ******* ***** ***

  • Write a C++ program that prompts the user to enter two numbers. Then prompt the user...

    Write a C++ program that prompts the user to enter two numbers. Then prompt the user to select from the following options: a. Add the numbers. b. Multiply the numbers. c. Terminate the program Use the appropriate control structures (loops, if else, switch-case, etc) to make your program as short and efficient as possible. Minor syntax errors are forgiven in this exercise; just focus on the correct structure, declarations and sequence of instructions

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • please help me by matlab 5.2) Develop a program that prompts the user for three numbers....

    please help me by matlab 5.2) Develop a program that prompts the user for three numbers. Once received, print the multiplication of the first two numbers. Use the third number as the quantity of decimal places to print. 5.3) Develop a program that prompts the user for two numbers and a calculation operator (+,-/or *). Once received, use appropriate IF STATEMENTS to complete the calculation guided by the following rules: Addition Subtraction Division Multiplication Print to output to three decimal...

  • Write a program that prompts the user to enter a length in feet and inches and...

    Write a program that prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number or a nondigit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Using Python

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT