<< Chapter < Page Chapter >> Page >
A screen capture of a window with a grey grid enclosing two input or output boxes, labeled year, and message. A pop-out menu from the year box is visible, with the selection, representation, then word, then an object labeled I32 highlighted.
32-Bit Integer Numeric

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

A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled False. Inside the box is a pink rectangle containing the text, Is a Leap Year. To the far left of the diagrams is an object titled year, and to the far right is an object titled message.
Unwired Leap Year Diagram

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

A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled False. Inside the box is a pink rectangle containing the text, Is a Leap Year. To the far left of the diagrams is an object titled year, and to the far right is an object titled message. There are blue lines connecting year to each quotient and remainder operator, green lines connecting the operators together, then converging on a connector from the or operator to the large rectangle. Finally, there is a pink line connecting the large rectangle to an icon labeled, message.
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 .

A three-part diagram connecting numbers to operations. The first is the number 4 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. The second is the number 100 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, not equal to 0. The third is the number 400 inside a blue box that is connected with a line segment to an operation named quotient and remainder. To the right of the object is the operator, equal to 0. In between the first and second parts is the, and, operator. In between the second and third parts is the, or, operator. To the right of these diagrams is a large rectangle, containing a drop-down menu labeled True. Inside the box is a pink rectangle containing the text, Is a Leap Year. To the far left of the diagrams is an object titled year, and to the far right is an object titled message. There are blue lines connecting year to each quotient and remainder operator, green lines connecting the operators together, then converging on a connector from the or operator to the large rectangle. Finally, there is a pink line connecting the large rectangle to an icon labeled, message.
Leap Year True Case

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

A screen capture of a grey grid containing an input and output box. The input box is labeled, year, and contains the value 2000. The output box is labeled, Message, and contains the text, is a leap year.
Leap Year Program

Arrays

Right click on the Front Panel window and select Array from the Controls>>Modern>>Arrays, Matrix&Cluster menu, and drop an array onto the Front Panel window. The array structure consists of an index or element offset (left portion of the structure) and the array elements (right portion of the structure). When the array structure is placed on the Front Panel window, the data type of the array is undefined as indicated by the grayed out portion of the array.

A two-part screenshot. The left side is a grey grid containing one white box, containing the value 0, and one larger grey box, labeled Array. The right side is a window titled, controls, containing a hierarchical list, beginning with Modern, and continuing with Array, matrix, and cluster. Below the list are two objects, labeled Array and Cluster.
Arrays

To define the array data type, drag and drop a data type onto the array structure. For instance, to create an input array of numbers, place Numeric Control into the array structure.

A two-part screenshot. The left side is a grey grid containing one white box, containing the value 0, and one larger grey box, labeled Array. Inside the grey box are dashed rectangles of different heights and widths. The right side is a window titled, controls, containing a hierarchical list, beginning with Modern, and continuing with Array, matrix, and cluster. Below the list are two objects, labeled Array and Cluster.
Creating a Numeric Array

At this point, the numeric array is an Empty or Null array because no elements of the array have been defined. This is indicated by the grayed out numeric control within the array structure.

A screen capture of a grey grid containing an input and output box. Both boxes contain values of 0, and the box is titled, Numeric Input Array.
Empty Numeric Array

Define elements of an input array by selecting the offset and entering its value. For instance, at offset = 4, enter the value 0.0. This defines Numeric Input Array as {0, 0, 0, 0, 0} .

A screen capture of a grey grid containing an input and output box. The box is titled, Numeric Input Array. The input box contains the value, 4, and the output box contains the value, 0.
Defining Numeric Array Elements

An output array is created similarly to an input array with the exception that an output data type needs to be dropped into the array structure.

A screen capture of a grey grid containing four boxes. The left two boxes are titled, Numeric Input Array. The right two boxes are titled, Numeric Output Array. All four boxes contain the value, 0.
Creating Output Numeric Arrays

For loop

This program converts an array of Fahrenheit values to Celsius. Create numeric input and output arrays and label them Fahrenheit and Celsius respectively. In the Fahrenheit array enter the values 0, 20, 40, 60, 80, 100, 120, 140, 160, 180 and 200 at offsets 0 through 10 as shown in [link] .

A screen capture of a grey grid containing four boxes. The left two boxes are titled, Fahrenheit, and the right two boxes are titled, Celsius. The leftmost box under fahrenheit contains the value, 10, and the right box under fahrenheit contains the value, 200. Both boxes under celsius contain the value, 0.
Numeric Input and Output Arrays

Right click in the Block Diagram window, navigate to Programming>>Structures and click on For Loop .

A screen cap of a window containing a directory hierarchy 'Programming' above 'Structures'. Contained in 'Structures' are two icons. On the left of the window there is an orange icon labeled 'Fahrenheit'.
For Loop Structure

Click and drag to create the For Loop as shown in [link] and [link] .

A dashed square is in the middle of this image. There is a smal dashed square containing an 'N'in the lower right corner of the square. On the left side of the square is an icon labeled 'Fahrenheit' and on the right side there is a similar icon labeled Celsius.
Creating For Loops
A dashed square is in the middle of this image. There is a smal dashed square containing an 'N'in the lower right corner of the square. On the left side of the square is an icon labeled 'Fahrenheit' and on the right side there is a similar icon labeled 'Celsius'.
For Loop

Right click inside the For Loop and select Select a VI… from the pop-up menu. Find the Fahrenheit to Celsius.vi and click OK . Drop the function inside the For Loop .

From left to right there is an orange square labeled 'Fahrenheit' and then a big box with a blue box 'N' on the upper left corner and a blue box 'i' on the lower left corner. In the middle of the big box there is a small box containing 'Fahr to Celsius'. On the right side of the big box there is another icon labeled 'celsius'.
Function in Diagram

To complete the program, wire the Fahrenheit input array to the input terminal of the Fahrenheit to Celsius function and wire the output terminal of the Fahrenheit to Celsius function to the Celsius output array.

From left to right there is an orange square labeled 'Fahrenheit' and then a big box with a blue box 'N' on the upper left corner and a blue box 'i' on the lower left corner. In the middle of the big box there is a small box containing 'Fahr to Celsius'. On the right side of the big box there is another icon labeled 'celsius'. An orange line runs from the right edge of the left icon through the center of the big box and connects to the left side of the right icon.
Wired Function in Diagram

This program uses the For Loop to select each element in the Fahrenheit input array, converts that value to Celsius and saves the results in the Celsius output array. Save the program as Fahrenheit to Celsius For Loop.vi and run the program.

Two parallel forms. On the left there is form labeled 'Fahrenheit' with the values '7' and '140'. On the right is another form labeled 'Celsius' with the values '7' and '60'.
Fahrenheit to Celsius Arrays

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to g programming. OpenStax CNX. Mar 15, 2010 Download for free at http://cnx.org/content/col11192/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to g programming' conversation and receive update notifications?

Ask