Question

It is often important to validate if the user entered an acceptable input to a program. For this problem, you may safely assu

PROBLEM 2 MATLAB

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

CODE:

%taking input
days = input('Enter the number of days: ');
hours = input('Enter the number of hours: ');
minutes = input('Enter the number of minutes: ');
seconds = input('Enter the number of seconds: ');

%printing the user input
disp(newline + "The user entered: " + printString(days, hours, minutes, seconds));

totalSec = 0;
%if user input needs correction,
%then convert the days, hours, mmiutes to seconds and
%convert the total seconds value to days, hours, minutes and seconds
if (hours > 23) || (minutes > 59) || (seconds > 59)
totalSec = (days * 86400) + (hours * 3600) + (minutes * 60) + seconds;
disp(newline + "The user-entered values required correction.");
days = fix(totalSec / 86400);
totalSec = mod(totalSec, 86400);
  
hours = fix(totalSec / 3600);
totalSec = mod(totalSec, 3600);
  
minutes = fix(totalSec / 60);
totalSec = mod(totalSec, 60);
  
seconds = totalSec;
str = printString(days, hours, minutes, seconds);
disp("The new values are: " + str);
else
disp(newline + "The user-entered values required no correction.");
end

%printing function
function str = printString(days, hours, minutes, seconds)
str = "";
str = str + num2str(days);
if days == 1
str = str + " day, ";
else
str = str + " days, ";
end
  
str = str + num2str(hours);
if hours == 1
str = str + " hour, ";
else
str = str + " hours, ";
end
  
str = str + num2str(minutes);
if minutes == 1
str = str + " minute, ";
else
str = str + " minutes, ";
end
  
str = str + num2str(seconds);
if seconds == 1
str = str + " second, ";
else
str = str + " seconds.";
end
end

OUTPUT:

MATLAB R2018a HOME PLOTS APPS EDITOR PUBLISH VIEW fxFA Insert Find Files Run Section 1 Comment % EGo To Compare EAdvance New

PROBLEM 2 EXTRA CREDIT IS ALSO DONE IN THIS SOLUTION.

PLEASE MAKE SURE TO LIKE AND COMMENT IN THE ANSWER. THANKS SO MUCH IN ADVANCE :)

Add a comment
Know the answer?
Add Answer to:
PROBLEM 2 MATLAB It is often important to validate if the user entered an acceptable input...
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
  • CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing...

    CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing a program that asks the user to input a time period as a number of seconds and in turn calculates the whole number of days, hours and minutes that are contained within the entered time period. Refer to the "Output Format" below for an example of what your program must do. The objectives of this lab are to reinforce and help teach: proper Java...

  • How do I program this problem using c++. Create a process to prompt the user to...

    How do I program this problem using c++. Create a process to prompt the user to enter the number of seconds and determine the whole number of days, hours, minutes, and seconds corresponding to the entered number of seconds. For example if the number of seconds entered was 183945, then 2 days, 3 hours, 5 minutes and 45 seconds should be displayed. There are 86400 seconds in a day, 3600 seconds in an hour, and 60 seconds in a minute....

  • Write a program which asks the user to enter an integer. Use switch statement to write...

    Write a program which asks the user to enter an integer. Use switch statement to write out the numeric word (such as ONE) if the user's input is 1; (see the sample run output for the usr input 2 or 3); otherwise, write OUT OF RANGE. Below are few sample runs: If the user enters a 1, the program will print: ONE TWO THREE Or, if the user enters a 2, the program will print: TWO THREE Or, if the...

  • CHALLENGE ACTIVITY CCY7.10.5: Read user input and print to output. Assign user_str with a string from...

    CHALLENGE ACTIVITY CCY7.10.5: Read user input and print to output. Assign user_str with a string from user input, with the prompt: 'Enter a string:" Hint- Replace the ? in the following code: user_str?('Enter a string: ') Note: These activities may test code with different pre-entered test values, thus blank print statements are placed after the prompt so that output will appear on the following line. This activity will perform two tests: the first with user input of "Hello", the second...

  • Write a C++ program that repeatedly collects positive integers from the user, stopping when the user...

    Write a C++ program that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the largest positive number entered. A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -213 Output: The largest positive number you entered was 10.

  • ANS (2) 0R hat output is produced by the following code if user enters these values...

    ANS (2) 0R hat output is produced by the following code if user enters these values for a question asked by the progran .User entered 3 Enter a positive integer value! (enter -1 to end) :3 Enter a positive integer value! (enter -1 to end) five Enter a positive integer value! (enter -1 to end) :7 Enter a positive integer value! (enter -1 to end) :-1 User entered five -User entered 7 ...User entered -1 import java.util.Scanner: class Verify input...

  • Write a C program that counts the number of each digit entered by the user (Input...

    Write a C program that counts the number of each digit entered by the user (Input process should be ended using EOF character). You should use switch to compute the number of each digits. For each digit your program should print a line containing that number of adjacent asterisks. Sample Input1: 18218342850056D855599 Sample Outputi: 0** 3° 5*** 6° 7 g*** Sample Input: 77777445521056 Sample Output2: 0* 1 2° 3 45 S. 6° 8 9 Sample Output 3: Sample Input3: 782222231567799988001332092555...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

  • In Matlab Create a single script (.m file) to solve these problems. Unless directed otherwise, use...

    In Matlab Create a single script (.m file) to solve these problems. Unless directed otherwise, use meaningful variable names for each variable; do not use the default variable ans to store your results. For this project, suppress your output with semi-colons (;). Each problem should be in a separate cell, using the cell mode feature of MATLAB. Problem 4 Video games are rather complicated to program, not least of which because of the graphics work that needs to be completed...

  • MUST BE WRITTEN IN C++ All user input values will be entered by the user from...

    MUST BE WRITTEN IN C++ All user input values will be entered by the user from prompts in the main program. YOU MAY NOT USE GLOBAL VARIABLES in place of sending and returning data to and from the functions. Create a main program to call these 3 functions (5) first function will be a void function it will called from main and will display your name and the assignment, declare constants and use them inside the function; no variables will...

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