Showing posts with label if. Show all posts
Showing posts with label if. Show all posts

Saturday, November 6, 2021

TI-92 Plus and TI-Nspire CX II: List Filters

TI-92 Plus and TI-Nspire CX II:  List Filters




For this particular post, I also have a TI-Nspire CX II CAS document which can be downloaded here:  https://drive.google.com/file/d/1Rm5vIWdUMg5OEyXi4VecNfuIGdiGRtan/view?usp=sharing


The functions are public and are available in the catalog.  Examples and syntax are included.  


The Filters


filters(list, criteria): filters out a list 

sumfilt(list, criteria): sum of selected elements of a list

avgfilt(list, criteria): arithmetic average of elements of a list

prdlist(list, criteria): product of selected elements of a list

dimfilt(list, criteria): returns a count of list elements that fit a criteria


This is similar to Excel functions filter, sumifs, averagefits, and countifs; there is no productifs in Excel.  


The criteria is a string, using x as a variable.  Examples:

"x<10": select elements less than 10

"x≥20": select elements greater than or equal to 20

"10<x and x<20": select elements between 10 and 20, not including 10 or 20

"10≤x and x≤20": select elements between 10 and 20, including 10 or 20


TI-92 Plus Functions: Filters


Note:  I am using the symbol [| for comment; in the program editor, select F2, option 9.  


TI-92 Plus Function:  filters


filters(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-13

[| filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return list2

EndFunc


TI-92 Plus Function: sumfilt


sumfilt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-14

[| sum-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return sum(list2)

EndFunc


TI-92 Plus Function: avgfilt


avgfilt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-14

[| average-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return sum(list2)/(dim(list2))

EndFunc


TI-92 Plus Function:  prdfilt


prdfilt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-14

[| product-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return product(list2)

EndFunc


TI-92 Plus Function:  dimfilt


dimflt(list1,cr) 

Func

[| list, criteria string with x

[| EWS 2021-08-13

[| count-if filter function

Local list2,d,i,t,x

{}→list2

dim(list1)→d

For i,1,d

expr(cr)|x=list1[i]→t

If t Then

augment(list2,{list1[i]})→list2

EndIf

EndFor

Return dim(list2)

EndFunc


Examples:


list0 = {2,4,5,6,9,10,11,13,14,15,18}


filters(list0,"x≤10"):  {2,4,5,6,9}

filters(list0,"5≤x and x≤15"):  {5,6,9,10,11,13,14,15}


sumfilt(list0,"x≤10"):  36

sumfilt(list0,"5≤x and x≤15"):  83


avgfilt(list0,"x≤10"):  6

avgfilt(list0,"5≤x and x≤15"):  83/8


prdfilt(list0,"x≤10"):  21600

prdfilt(list0,"5≤x and x≤15"):  2700


dimfilt(list0,"x≤10"):  6

dimfilt(list0,"5≤x and x≤15"):  8


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Wednesday, June 21, 2017

HP Prime and Casio fx-CG50: Basic Spreadsheet Comparison

HP Prime and Casio fx-CG50:  Basic Spreadsheet Comparison






HP Prime
Casio fx-CG50
Maximum Spreadsheet Size
10,000 rows by 676 columns
999 rows by 26 columns
Accessing Spreadsheet mode
[Apps], Spreadsheet
[MODE], 4
Creating and saving files
The Spreadsheet app can be copied and renamed
Files can be created and saved.  The fx-CG50 stores spreadsheets as separate files.
Clearing the entire spreadsheet
[Shift], [Esc] (Clear), pressing OK
From the main menu: [ F3 ] (DELETE), [ F3 ] (ALL), [ F1 ] (OK)
Inserting text labels in cells
[ALPHA], [ 0 ] (“ “)
[ALPHA], [ *10^ ] (“).  Text cells are indicated by a solid black strip on the left side of the cell.
Inserting the colon character, for ranges (:)
[ALPHA], [ - ] ( : )
During cell entry/edit, [ F3 ] ( : )
Inserting the dollar sign character, for absolute cell references ($)
During cell entry/edit, the touch key ( $ )
During cell entry/edit, [ F2 ] ( $ )
Starting a formula with the equals sign (=)
[Shift], [ . ] ( = )
[Shift], [ . ] ( = )
The SUM function
During formula entry:  [Toolbox], (App), Spreadsheet, SUM
During formula entry:  [ F5 ] (CELL), [ F5 ] (Sum).  The function is named CellSum.
The AVERAGE function
During formula entry: [Toolbox], (App), Spreadsheet, AVERAGE
During formula entry: [ F5 ] (CELL), [ F3 ] (Mean). The function is named CellMean.
The IF function
During formula entry:  [Toolbox], (Ctlg), select when
Syntax:  when(condition, do if true, do if false)
During formula entry:  [ F5 ] (CELL), [ F4 ] (CellIf). 
Entering a random number
During formula entry:  [Toolbox], 5. Probability, 4.  Random:
1 for random number:  RANDOM()
2 for random integer:  RANDINT(a,b)
During formula entry:  [OPTN], [ F5 ] (PROF), [ F4 ] (RAND)
[ F1 ] for random number: Ran#
[ F2 ] for random integer: RandInt#(a,b)
Change text color
(Format), 4. Color, select color
[Shift], [ 5 ] (FORMAT), 1. Char Color, choose color, [EXIT]
Change cell color
(Format), 5. Fill, select color
[Shift], [ 5 ] (FORMAT), 2. Area Color, choose color, [EXIT]
Number of colors
16,777,216.  18 set colors, plus one master color selector
8 colors:  black, blue, red, magenta, green, cyan, yellow, white
Selecting a Group of Cells
Press the soft key (Select), choose cells, turn off by pressing (Sel *)
[Shift], [ 8 ] (CLIP), choose cells, turn of by pressing [EXIT]
Sizing columns
Go to top of the column, press (Format), 8, # of bytes
Use -1 for auto sizing
(not available)
Number format (number of decimal places)
Select cells, (Format), 1.  Number Format.  Select 8 to select the number of decimal places
(not available)
Conditional Formatting
(not available)
[ F6 ] ( > ), [ F5 ] (CONDIT), complete the prompt
Filling a formula
Select the source cell and copy it ([ Shift ], [ View ]).  Select target cells and paste the formula ( [ Shift ], [ Menu ], [ → ], select Formula)
Select the target cell range, [ F2 ]  (EDIT), [ F6 ] ( > ), [ F1 ] (FILL)
Plotting a scatter plot
(not available)
Select range, [ F6 ] ( > ), [ F1 ] (Graph), [ F1 ] or [ F2 ] or [ F3 ] for Graph1, Graph2, or Graph3, respectively
Trend line analysis – Linear Regression
Go to an output cell, [ Shift ], [ . ] (=),[Toolbox], (App), Spreadsheet, REGRS
Syntax: REGRS(range, 1)
Select range, [ F6 ] ( > ), [ F2 ] (CALC), [ F3 ] (REG), [ F1 ] (X), [ F1 ] (ax+b).  Results are shown.  Recall separate variables through VARS-STAT-GRAPH

Do you use the spreadsheet mode on your calculator?  Check it out if you have not done so. 

Happy First Days of Summer (Northern Hemisphere)/Winter (Southern Hemisphere)!

Eddie



This blog is property of Edward Shore, 2017

Thursday, October 6, 2011

RPL Programming Tutorial - Part 3 - HP 49g+/50g: If-Then-Else

If Only...

Welcome to Part 3 of the RPL Programming Tutorial with the HP 49g+ and HP 50g calculator. To recap, Part 1 discussed the basics of RPL programming and Part 2 introduced the concept of local variables.

In Part 3 we will dive into the IF-THEN and IF-THEN-ELSE structures. These structures test data against a condition, and then instructs the machine to execute designated program code based on the results.

In general, an IF-THEN-ELSE-END structure looks like this:

IF condition listed here (i.e. x = 5, y > 10, z ≤ 0, a ≠ b, etc.)
THEN commands to be done if the condition is true
ELSE commands to be done if the condition is false
END

How to find the IF menu

You can input the IF, THEN, ELSE, and END program commands separately. However, the HP 50g (and 49g+) gives the user another choice. With a keystroke sequence, you can enter the entire sequence as a template.

To insert the templates:

Start by pressing [LS] [EVAL] (PRG) [F3] (BRCH) .

To insert an IF-THEN-END structure, press [LS] [F1] (IF).

To insert an IF-THEN-ELSE-END structure, press [RS] [F1] (IF).

The next two programs will show an example of each of the two structures.

The Online Shipping Deal

A famous online store is running a promotion: Order at least $100.00 from our store, and we'll pay the $5.95 shipping fee! This program calculates the total amount of the order, with shipping if any. Assume the store charges 7.5% sales tax on all orders including shipping.

Notes:
1. The local variable A will be used to designate the amount of goods purchased.
2. Since A + y% = A + x * y /100 = A * (1 + y /100), we will multiply the amount plus shipping by 1.075. (Let y = 7.5%)
3. The $5.95 shipping charge applies only when the order amount is less than $100.00. Set up the test this way: If A < 100, add 5.95 to A, otherwise do nothing. Because we are only executing further instructions only when the condition is true, no ELSE command is necessary.

The Program ONSALE

Comments will be italicized, starting with an asterisk. This program assumes that the purchase amount is on Level 1 of the stack.

[RS] [ + ] (<< >>)
* Start of the program
[RS] [ 0 ] (&rarr)
[ALPHA] [F1] (A)

* Assign the amount to the local variable A
[RS] [ + ] (<< >>)
* Start the main program
[ALPHA] [F1] (A)
* Call A to the stack
[LS] [EVAL] (PRG)
[F3] (BRCH)
[LS] [F1] (IF)

* Insert the IF-THEN-END structure
[ALPHA] [F1] (A)
[SPC] 100 [RS] [ X ] (<)

* Insert the test condition A < 100
[ &darr ] (down arrow)
5.95 [ + ]

* Add shipping charge if A < 100
[ &darr ] 1.075 [ x ] [RS] [ENTER] (&rarr NUM) [ENTER]
* Finish the program

[ ' ] [ALPHA] [ALPHA] [ ' ] (O) [EVAL] (N) [SIN] (S) [F1] (A) [NXT] (L) [F5] (E) [ENTER] [STO>]

The completed program ONSALE:
<< &rarr A << A IF A 100 < THEN 5.95 + END 1.075 * &rarr NUM >> >>

Instructions:
1. Enter the amount on Level 1 of the stack.
2. Run ONSALE.

Test Data:

Input = 50, Result = 60.14625 (A $50.00 order results in a total bill of $60.15.)

Input = 99.99, Result = 113.8855 (A $99.99 order results in a total bill of $113.89)

Input = 100, Result = 107.5 (A $100.00 order results in a total bill of $107.50. Surprised? Remember all orders $100.00 or more result in the shipping charged being waived.)

Input = 149.99, Result = 161.23925 ($149.99 order yields a bill of $161.24)

f(x) = (sin x)/x

This program calculates the function f(x) = (sin x)/x for all real numbers. If we attempt to calculate f(0) directly, we would get a "division by zero" error. However, the calculus limit as f(x) approaches 0 is 1. Let's create a test condition that detects for an input of 0. If the input is anything else, f(x) computes normally.

Note: The test of equality requires two equal signs, ==. This can be typed directly from the keyboard via alpha or by the TEST submenu of the PROGRAM menu. This program uses TEST submenu method (4 keystrokes opposed to 6 keystrokes).

The Program SINX

The program takes X from Level 1 of the stack and stores it as the local variable X.

[RS] [ + ] (<< >>)
[RS] [ 0 ] (&rarr)
[ X ]

* Stores the contents of Level 1 in the local variable X
[RS] [ + ] (<< >>)
[LS] [EVAL] (PRG)
[F3] (BRCH)
[RS] [F1] (IF)

* Inserts the IF-THEN-ELSE-END structure
[ X ] [SPC] 0
[LS] [EVAL] (PRG) [F4] (TEST) [F1] (==)

* Inserts the double equals sign, ==, for the equality test. Test X = 0?
[ &darr ] [SPC] 1
* Enters the commands should X = 0 (THEN)
[ &darr ] [SPC]
[ X ] [SIN] [ X ] [ ÷ ]
[RS] [ENTER] (&rarr NUM)

* Enters the commands should X ≠ 0 (ELSE)
[ENTER]
* Terminates program entry

[ ' ] [ALPHA] [ALPHA] [SIN] (S) [TOOL] (I) [EVAL] (N) [ X ] (X) [ENTER] [STO>]

The complete program:
<< &rarr X << IF X 0 == THEN 1 ELSE X SIN X / &rarr NUM END >> >>

Source: HP 48SX Scientific Expandable Calculator: Owner's Manual Volume II. Hewlett Packard, 3rd Edition, 1990

Instructions for SINX:
1. Enter X on the stack.
2. Run SINX.

Test Data - Assume the calculator is in Radians mode:

Input = -1, Result = 0.841470984808

Input = 0, Result = 1 (If you get this, then the IF-THEN-ELSE-END structure worked)

Input = 1, Result = 0.841470984808

Coming up, we'll take a look at the FOR-NEXT structure. See you next time in Part 4!

Eddie


This tutorial is property of Edward Shore. Mass reproduction or distribution requires express permission of the author.

Earth's Radius by Latitude

Earth's Radius by Latitude Introduction: Calculating the Earth’s Radius In quick, general calculations, we assume that the...