Program doy in Example \(4.3\) calculates the day of year associated with any given month, day, and year. As written, this program docs not check to sce if the data entered by the user is valid. It will accept nonsense values for months and days and do calculations with them to produce meaningless results. Modify the program so that it checks the input values for validity before using them. If the inputs are invalid, the program should tell the user what is wrong and quit. The year should be a number greater than zero, the month should be a number between 1 and 12 , and the day should be a number between 1 and a maximum that depends on the month. Use a switch construct to implement the bounds checking performed on the day.

Short Answer

Expert verified
To modify the program to check the validity of user inputs, follow these steps: Step 1: Check the year validity - Ensure the entered year is greater than zero. If not, display an error message and exit the program. Step 2: Check the month validity - Ensure the entered month is between 1 and 12. If not, display an error message and exit the program. Step 3: Check the day validity - Use a switch statement to set the maximum value for the day depending on the month (e.g., 31 for January, 28 or 29 for February depending on leap year, etc.). If the entered day is not within the valid range for that month, display an error message and exit the program. Step 4: Calculate the day of the year - If all input values are valid, use the given program logic to calculate the day of the year.

Step by step solution

01

Check the year validity

Check if the entered year is greater than zero. If it is not greater than zero, display an error message to the user and exit the program.
02

Check the month validity

Check if the entered month is a number between 1 and 12. If it is not within this range, display an error message to the user and exit the program.
03

Check the day validity

Use a switch statement to check the entered day's validity depending on the month. For each month, set the maximum value for the day (e.g., 31 for January, 28 or 29 for February, depending on whether it's a leap year or not, and so on). If the entered day is not within the valid range for that month, display an error message to the user and exit the program.
04

Calculate the day of the year

If all the input values are valid, proceed with the calculation of the day of the year using the given program logic. The modified program should now validate user inputs and provide an error message if any of the inputs are invalid before proceeding with the calculation.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Tension on a Cable. A 200-pound object is to be hung from the cnd of a rigid 8 -foot horizontal pole of negligible weight, as shown in Figure 4.5. The pole is attached to a wall by a pivor and is supported by an 8 -foot cable that is attached to the wall at a higher point. The tension os this cable is given by the equation $$ T=\frac{W+k c \cdot l p}{d \sqrt{l p^{2}-d^{2}}} $$ where \(T\) is the tension on the cable, \(W\) is the weight of the object. Ic is the length of the cable, Ip is the length of the pole, and \(d\) is the distance along the pole at which the cable is attached. Write a program to determine the distance \(d\) at which to attach the cable to the pole to minimize the tension on the cable. To do this, the program should calculate the tension on the cable at regular l-foot intervals from \(d=1\) foot to \(d=7\) fect. and should locate the position \(d\) that produces the minimum tens on. Also, the program should plot the tension on the cable as a function of \(d\), with appropriate tities and axis labels.

Write an M-file to calculate the factorial function N!, as defined in Example \(4.2\). Be sure to handle the special case of \(0 !\) Also, be sure to report an error if \(\mathrm{N}\) is negative or not an integer.

Write a single program that calculates the arithmetic mean (average), rms average, geometric mean, and harmonic mean for a set of positive numbers. Use any method that you desire to read in the input values. Compare these values for each of the following sets of aumbers. a. \(4,4,4,4,4,4,4\) b. \(4,3,4,5,4,3,5\) c. \(4,1,4,7,4,1,7\) d. \(1,2,3,4,5,6,7\)

Current Through a Diode. The current flowing through the semiconductor diode shown in Figure \(4.4\) is given by the equation $$ i_{\theta}=l_{e}\left(e^{\frac{w_{3}}{1 T}}-1\right) $$ where $$ \begin{aligned} &i_{D}=\text { the voltage across the diode, in volts } \\ &v_{n}=\text { the current flow through the diode, in amps } \\ &I_{e}=\text { the leakage current of the diode, in amps } \\ &q=\text { the charge on an electron, } 1.602 \times 10^{-19} \text { coulombs } \\ &k=\text { Boltzmann's constant, } 1.38 \times 10^{-23} \text { joule/K } \\ &T=\text { temperature, in kelvins }(\mathrm{K}) \end{aligned} $$ The leakage current \(I_{o}\) of the diode is \(2.0 \mu \mathrm{A}\). Write a program to calculate the current flowing through this diode for all voltages from \(-1.0 \mathrm{~V}\) to \(+0.6 \mathrm{~V}\), in \(0.1 \mathrm{~V}\) steps. Repeat this process for the following temperatures: \(75^{\circ} \mathrm{F}, 100^{\circ} \mathrm{F}\), and \(125^{\circ} \mathrm{F}\), Create a plot of the current as a function of applied voltage, with the curves for the three different temperatures appearing as different colors.

Write the MATLAB statements required to calculate and print out the squares of all the even integers between 0 and 50 . Create a table consisting of each integer and its square, with appropriate labels over each column.

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free