M09 Errors and Warnings Packet

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

21 Terms

1
New cards

T = input('Enter temperature in kelvin: ');

This line prompts the user to enter a temperature value in Kelvin and stores it in the variable T. The input() function reads the user's input from the command window.

2
New cards

if T < 0

This starts a conditional statement. It checks if the entered temperature T is less than zero.

3
New cards

error('Error: Absolute temperatures cannot be less than zero!')

If the condition in step 2 is true (i.e., the temperature is negative), this line executes. The error() function throws an error, displaying the message "Error: Absolute temperatures cannot be less than zero!" in the command window. Crucially, this stops the program's execution. Negative Kelvin temperatures are physically impossible.

4
New cards

end

This marks the end of the if statement. If the temperature T is not less than 0, the code proceeds to the next line.

5
New cards

T_F = (T - 273) * 1.8 + 32;

This line performs the Kelvin to Fahrenheit conversion using the standard formula. The result is stored in the variable T_F. This line only executes if the if condition (T<0) is false.

6
New cards

fprintf('Temperature of %0.0f K is converted to %0.0f deg F.', T, T_F)

This line uses the fprintf() function to display the results of the conversion to the command window. %0.0f is a format specifier that displays the numbers (temperature in Kelvin and Fahrenheit) without any decimal places.

7
New cards

T = input('Enter temperature in kelvin: ');

This line prompts the user to enter a temperature in Kelvin and stores the input in the variable T.

8
New cards

if T < 0

This conditional statement checks if the entered temperature T is less than zero. Negative Kelvin temperatures are physically impossible.

9
New cards

warning('Warning: Absolute temperatures cannot be less than zero! Using absolute value of T.')

If the condition T < 0 is true, this line executes. The warning() function displays a warning message in the command window, alerting the user that the input is invalid but doesn't halt the program.

10
New cards

T = abs(T);

This line takes the absolute value of T using the abs() function. This corrects the invalid negative input by using its positive equivalent. This is a choice made by the programmer—they're choosing to proceed with the calculation using the absolute value rather than stopping the program.

11
New cards

T_F = (T - 273) * 1.8 + 32;

This line performs the Kelvin to Fahrenheit conversion using the standard formula. It uses the value of T, which might be the original input or its absolute value (depending on whether the warning was triggered).

12
New cards

fprintf('The temperature of %0.0f K is converted to %0.0f deg F.', T, T_F)

This line prints the original (possibly corrected) Kelvin temperature and its Fahrenheit equivalent to the command window. %0.0f is a format specifier to display the numbers without decimal places.

13
New cards

MyList={'a','b','c','d','e'};

This line creates a cell array named MyList containing the letters 'a' through 'e'. This array provides the options for the user to choose from in the dialog box.

14
New cards

[Choice,tf] = listdlg('ListString',MyList, 'PromptString','Pick a letter', 'SelectionMode','single')

This is the core of the input process. listdlg creates a dialog box that presents the options in MyList.

  • 'ListString',MyList: Specifies the list of options to display.

  • 'PromptString','Pick a letter': Sets the text prompt that appears above the list of options.

  • 'SelectionMode','single': Restricts the user to selecting only one item from the list.

The function returns two values:

  • Choice: An integer representing the index of the selected item in MyList. If no item is selected, it returns an empty array [].

  • tf: A boolean value. It is 1 if the user made a selection and pressed "OK", and 0if the user pressed "Cancel" or closed the dialog without making a selection.

15
New cards

if tf == 0

This conditional statement checks the value of tf. If tf is 0, it means the user canceled the dialog without selecting an item.

16
New cards

warning('You didn''t make a choice.')

If the condition in step 3 is true, this line executes. The warning() function displays a warning message in the command window, informing the user that they didn't make a selection. The program continues executing, but the user is alerted to the lack of input.

17
New cards

ans_usr = questdlg('Is today Friday?')

This line is the core of the code. questdlg creates a simple dialog box that presents the question "Is today Friday?". The user can choose from "Yes," "No," or "Cancel."

The dialog box's response is stored in the variable ans_usr. ans_usr will contain a string: 'Yes', 'No', or 'Cancel'. Importantly, if the user closes the dialog box without selecting any option, ans_usr will be an empty string ('').

18
New cards

if isempty(ans_usr)

This conditional statement checks if ans_usr is empty. isempty() returns true if the variable is empty and false otherwise. An empty ans_usr indicates the user closed the dialog box without selecting an option.

19
New cards

warning('You didn''t make a choice.')

If the condition in step 2 is true (the dialog was closed without a selection), this line executes. The warning() function displays a warning message in the command window. The warning message informs the user that they didn't select an option, but it doesn't stop the execution of the program.

20
New cards

PRACTICE: The specific gravity of gold is 19.3. Write a MATLAB program that will ask
the user to input the mass of a cube of solid gold in units of kilograms [kg]
and display the length of a single side of the cube in units of inches [in].
The output should display a sentence like the one shown that follows, with
the length formatted to two decimal places.
If the user types a negative number or zero for the mass of the cube, your
program should display an error message and terminate.
Sample Input/Output (two examples):
Enter the mass of the cube [kilograms]: -3
Error: Mass must be greater than zero grams.
Enter the mass of the cube [kilograms]: 0.4
The length of one side of the cube is 1.08 inches.

% Constants
specific_gravity_gold = 19.3;  % Specific gravity of gold
density_water = 1000;        % Density of water in kg/m^3
kg_to_lbs = 2.20462;         % Conversion factor kg to lbs
lbs_to_oz = 16;             % Conversion factor lbs to oz
oz_to_grams = 28.3495;       % Conversion factor oz to grams
m_to_in = 39.3701;           % Conversion factor m to in

% Get mass input from the user
mass_kg = input('Enter the mass of the cube [kilograms]: ');

% Input validation: Check for non-positive mass
if mass_kg <= 0
    error('Error: Mass must be greater than zero kilograms.');
end

% Calculate density of gold in kg/m^3
density_gold_kg_m3 = specific_gravity_gold * density_water;

% Calculate the volume of the cube in cubic meters
volume_m3 = mass_kg / density_gold_kg_m3;

% Calculate the length of one side of the cube in meters
side_length_m = volume_m3^(1/3);

% Convert the side length to inches
side_length_in = side_length_m * m_to_in;

% Display the result, formatted to two decimal places
fprintf('The length of one side of the cube is %.2f inches.\n', side_length_in);

21
New cards

PRACTICE: Write a code segment to ask the user to enter the number of days in the
month of February for the current year.
If the user enters a value other than 28 days or 29 days, issue a warning to
the user that their entry is invalid and set the number of days to 28.
Based on the number of days, store the string value leap year in the
variable YEAR; otherwise, store the string value not a leap year in the
variable YEAR.

% Prompt the user to enter the number of days in February
days_in_feb = input('Enter the number of days in February for the current year: ');

% Check if the input is valid (28 or 29 days)
if days_in_feb ~= 28 && days_in_feb ~= 29
    warning('Invalid entry. The number of days in February must be 28 or 29. Setting days to 28.');
    days_in_feb = 28; % Set to 28 if invalid
end

% Determine and store the year type
if days_in_feb == 29
    YEAR = 'leap year';
else
    YEAR = 'not a leap year';
end

% Display the result (optional)
fprintf('The year is: %s\n', YEAR);