You are here

Case Selection

26 July, 2019 - 09:47
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/5b6e61df-b830-48cb-9764-94696cb47c80@1.3

This program determines if a year is a leap year or not. A leap year is divisible by 4 but not by 100, except when it is divisible by 400. A number x is divisible by a number y if the remainder of x/y is identical to zero, i.e. Rem(x/y}=O is true therefore

Leap Year = {Rem (Year/4) = 0 And Not (Rem (Year/100) = 0)} Or Rem (Year/400) = 0        (3.1)

where And, Or and Not are Boolean operators.

For example:

1900 is not a leap year because it is divisible by 100

1970 is not a leap year because it is not divisible by 4

1980 is a leap year because it is divisible by 4 but not by 100

2000 is a leap year because it is divisible by 400

Start a new G program and right click on the Block Diagram window. Go to the Functions » Programming » Numeric menu in the Block Diagram window.

media/image27.png
Figure 1.26 Quotient & Remainder Function 

Select three copies of the Quotient & Remainder function and three numeric constants. Type in 4, 100 and 400 for the numeric constants and wire these constants to the lower input terminal (corresponding to the dividend) of the Quotient & Remainder function.

media/image28.png
Figure 1.27 Leap Year Numeric Constants 

From the Functions » Programming » Comparison menu, select 2 copies of the Equal to Zero function and one copy of the Not Equal to Zero function.

media/image29.png
Figure 1.28 Comparison Functions 

Organize the comparison operations as show in the diagram.

media/image30.png
Figure 1.29 Diagram 

From the Functions » Programming » Boolean menu select the AND and OR operators

media/image31.png
Figure 1.30 Boolean Operators 

Place the Boolean operators as shown in Q&R, Comparison & Boolean Functions.

media/image32.png
Figure 1.31 Q&R, Comparison & Boolean Functions 

From the Functions » Programming » Structures menu, click on the Case Structure.

media/image33.png
Figure 1.32 Case Structure 

Click and drag on the Block Diagram window to create the Case Structure.

media/image34.png
Figure 1.33 Creating a Case Structure 

The True diagra m option is indicated at the top of the case structure.

media/image35.png
Figure 1.34 Created Case Structure 

Drop a string constant and type "Is a Leap Year".

media/image36.png
Figure 1.35 True Case Editing 

Click on the down arrowhead next to the True label and select the False option .

media/image37.png
Figure 1.36 Selecting the False Case  

Drop another string constant and type "Is not a Leap Year".

media/image38.png
Figure 1.37 False Case Editing 

Go to the Front Panel window and place a numeric input and an output string. Re-label the numeric input to Year and the output string to Message.

Figure 1.38 Leap Year GUI 

Right click on Year and select Representation » I32 from the numeric pop-up menu.

media/image40.png
Figure 1.39 32-Bit Integer Numeric 

Arrange the Year and Message terminals in the Block Diagram window as shown in the figure.

media/image41.png
Figure 1.40 Unwired Leap Year Diagram  

Wire the OR operator is to the "7" in the case structure and the string constant "Is not a Leap Year" is wired to Message.

media/image42.png
Figure 1.41 Leap Year False Case 

Select the True option and Wire the "Is a Leap Year" string constant to the output terminal of the Case Structure.

media/image43.png
Figure 1.42 Leap Year True Case 

Save the program as Leap Year.vi, enterYear values and run the program to determine whether the value of Year is that of a leap year or not.

media/image44.png
Figure 1.43 Leap Year Program