Gambas/Maths
Back to Gambas
The Cross Sum Program
[edit | edit source]This miniprogram calculates the cross sum of an integer. You need 2 textboxes and 1 commandbutton to get the program going.
Please beware of the signs / and \ . In this program the sign \ is used for a division without a remainder.
The Code
PUBLIC SUB Button1_Click()
DIM z AS Integer
DIM crosssum AS Integer
z = Val(TextBox1.Text)
DO WHILE z <> 0
crosssum = crosssum + z MOD 10
z= z \ 10
LOOP
TextBox2.Text = Str$(crosssum)
END
Addy The Sum Program
[edit | edit source]This miniprogram calculates the sum of a row of integers. You need 1 textarea, 1 textbox and 1 commandbutton to get the program going. You can also add negative integers. Very helpful is the Split command and the String array String[]
The Code
PUBLIC SUB Button1_Click()
DIM text AS String
DIM summe AS Float
DIM Elt AS String[]
DIM Sb AS String
text = textarea1.Text
Elt = Split(text,Chr(10))
FOR EACH Sb IN Elt
summe = summe + Val(sb)
NEXT
textbox1.Text = summe
END
The Average Program
[edit | edit source]This miniprogram calculates the average of a row of integers. You need 1.textarea, 2 textboxes and 1 commandbutton to get the program going.
The Code
PUBLIC SUB Button1_Click()
DIM text AS String
DIM summe AS Float
DIM mw AS Float
DIM varianz AS Float
DIM sigma AS Float
DIM Liste AS String[]
DIM Einzelwert AS String
text = textarea1.Text
Liste = Split(text,Chr(10))
FOR EACH Einzelwert IN Liste
summe = summe + Val(Einzelwert)
NEXT
mw = summe / Liste.Length
textbox1.Text = mw
varianz = 0
FOR EACH Einzelwert IN Liste
varianz = varianz + ((Val(Einzelwert) - mw)^2)
PRINT Einzelwert,mw,((Val(Einzelwert) - mw)^2)
PRINT varianz
NEXT
varianz = varianz / (Liste.Length - 1)
PRINT varianz
sigma = Sqr(varianz)
textbox2.Text = sigma
END
The Median
[edit | edit source]The Median of a list of values lies between two equal parts of the list. 50% of the values ar greater or equal to the median. 50% are smaller or equal to the median. In contrary to the average the median is not influenced that much by extreme values.
If you want to program the median, you have to
- 1.sort your list of values
- 2.check if the list has an even or an odd number of values.
- 3.choose the right formula for the median.
- for an odd number of values
xmedian = x[(n+1)/2]
- for an even number of values
xmedian = 1/2*(x[n/2] + x[n/2+1])
example 1:
sorted list of values:
11 12 13 14 15
The number of values is odd. n = 5
(n+1)/2 = 6/2 = 3
xmedian = x[3] = 13
example 2.
sorted list of values:
11 12 13 14 15 16
The number of values is even , n = 6
n/2 = 6/2 = 3 x[3] = 13 x[4] = 14 xmedian = 1/2*(13+14) = 13,5
You can take the list from above and copy it into the Gambas median program. Use the clipboard of your computer for that. Ctrl+C = Copy , Ctrl + V = Paste
example program
You need 2 textareas , 3 commandbuttons and 1 textbox, to get the program going:
' Gambas CLASS file
PUBLIC liste AS String[]
PUBLIC SUB Form_Open()
ME.Text = "Computate the Median "
END
PUBLIC SUB Button1_Click()
'sort
Dim c AS Integer
Dim j AS Integer
Dim n AS Integer
Dim y AS Variant
Dim liste AS String[]
Dim element AS String
Dim txt AS String
Dim text AS String
text = Textarea1.Text
liste = Split(text,Chr(10))
y = 0
n = liste.length
REPEAT
c = 0
FOR j = 0 TO n - 2
'PRINT j,y,liste[0],ar[1],ar[2],ar[3],ar[4]
IF Val(liste[j]) > Val(liste[j + 1]) THEN
y = Val(liste[j])
liste[j] = liste[j + 1]
liste[j + 1] = Str(y)
c = 1
ENDIF
NEXT
UNTIL c = 0
FOR EACH element IN liste
txt = txt & Str(element) & Chr(10)
NEXT
PRINT txt
textarea2.Text = ""
txt = Mid$(txt,1,-1)
'the last CR has to be killed chr(10)
textarea2.Text = txt
END
PUBLIC SUB Button2_Click()
'computate median, but sort first !!
Dim text AS String
Dim median AS Float
Dim liste AS String[]
Dim posten AS String
text = Textarea2.Text
liste = Split(text,Chr(10))
'count the length of your list and choose the right formula
IF liste.Length MOD 2 THEN
'PRINT liste.Length MOD 2 & " odd"
'PRINT (liste.length + 1)/2
'PRINT liste[(liste.length + 1)/2 - 1]
median = Val(liste[(liste.length + 1)/2 - 1])
'the array starts with the element 0 not with the element 1 !
ELSE
'PRINT liste.Length MOD 2 & " even"
median = (Val(liste[liste.length/2 - 1]) + Val(liste[liste.length/2]))/2
ENDIF
textbox1.Text = Str(median)
END
PUBLIC SUB Button3_Click()
'list of values as an example
textarea1.Text = "114,3"
textarea1.Text = textarea1.Text & Chr(10) & "135,7"
textarea1.Text = textarea1.Text & Chr(10) & "104,8"
textarea1.Text = textarea1.Text & Chr(10) & "118,5"
textarea1.Text = textarea1.Text & Chr(10) & "125,7"
textarea1.Text = textarea1.Text & Chr(10) & "121,4"
textarea1.Text = textarea1.Text & Chr(10) & "122,4"
textarea1.Text = textarea1.Text & Chr(10) & "96,8"
textarea1.Text = textarea1.Text & Chr(10) & "118,9"
textarea1.Text = textarea1.Text & Chr(10) & "120"
textarea1.Text = textarea1.Text & Chr(10) & "112,2"
textarea1.Text = textarea1.Text & Chr(10) & "127,9"
textarea1.Text = textarea1.Text & Chr(10) & "122,8"
textarea1.Text = textarea1.Text & Chr(10) & "128,9"
textarea1.Text = textarea1.Text & Chr(10) & "120,3"
'median = 120,3
END