Conditional Statements and Algorithms Exercises
Conditional Statements and Algorithms Exercises
The algorithm checks the current day and time against the bank's schedule: open from 8:00 to 12:00 and 14:00 to 17:00, excluding Thursday afternoons and all of Friday. It uses a series of if-else statements to compare input time against this schedule and returns messages based on these comparisons. If closed, it generates alternative open times by determining the next valid time slot when the bank is open, helping guide users on when to return .
The algorithm uses age thresholds to classify a child's category. Ages 6-7 are classified as "Poussin," 8-9 as "Pupille," 10-11 as "Minime," and 12 and older as "Cadet." This categorization is implemented through a series of conditional statements, likely utilizing if-else constructs or switch cases to match specific age ranges to their respective categories and output the correct classification .
Error handling strategies include validating inputs through condition checks before processing, utilizing try-catch blocks where applicable, prompting user re-entry upon invalid input, and providing clear feedback on acceptable input formats. Additionally, implementing input sanitization, such as trimming whitespace and checking for required input types before conversion operations, enhances robustness against common input errors .
To determine the season, the algorithm assumes each season starts on the 21st of a specific month: March 21 for spring, June 21 for summer, September 21 for autumn, and December 21 for winter. It compares the entered day and month against these dates to assign the current season, adjusting for day inclusivity based on the specific start date of each season .
The algorithm involves initially assuming one of the three numbers as minimum and maximum, then iteratively comparing each number against these assumptions. It updates the minimum if any number is smaller than the current minimum, and similarly updates the maximum if any number is larger than the current maximum. These conditional comparisons are facilitated by if-else statements that ensure every number is checked against the others to derive both maximum and minimum values accurately .
The algorithm first checks if the year is divisible by 4. If true, it then checks if it is a century year (ending in 00); if so, it must also be divisible by 400 to be a leap year. This three-step check ensures that most years divisible by 4 but not century years are leap years, while century years are only leap years if divisible by 400. Example checks include years like 1980 and 2000, while disqualifying 2100 and 3000 .
The algorithm first requires input of the interval bounds from the user, followed by the target number. It then checks if the target number is greater than or equal to the lower bound and less than or equal to the upper bound using conditional statements. This ensures the number is within the closed interval defined by the user, accounting for boundary inclusivity .
Common errors include improper use of operators like '**et**' without ensuring proper syntax for both conditions, or ambiguous conditions such as 'N1 = 0, N2 = 0' without a clear conjunction or disjunction. Corrective actions involve ensuring each condition has explicit AND (&&) or OR (||) operators to clearly define relationships between conditions, thereby preventing logic faults and compilation errors .
The conditions evaluated for additional paid leave days include: being an executive, being at least 35 years old and having more than 3 years of seniority for 2 additional days, and being at least 45 years old with more than 5 years of seniority for a total of 4 additional days. The algorithm evaluates these conditions using conditional statements, likely nested if-else conditions, to check each criterion sequentially and accumulate additional leave days accordingly .
The algorithm requires the user to enter the day, month, and year of a date with the month given as a number. It checks validity by ensuring the day is within the valid range for the given month and adjusting for leap years if necessary. To transform the month's number into its name, it uses a 'case/switch' statement, mapping integer month values to their respective string names, such as 3 to March .