How write a calculate average monthly sales program
-
- Posts: 1
- Joined: May 18, 2018 16:59
How write a calculate average monthly sales program
I would like to no if someone could send me in the direction of how to write a calculate average monthly sales program. I've search the internet and have not found one using free Basic.
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Re: How write a calculate average monthly sales program
You need to provide more information. What is the format of your input data?
Short answer is that you add up 12 monthly totals and divide by 12.
Short answer is that you add up 12 monthly totals and divide by 12.
Re: How write a calculate average monthly sales program
Here is a very simple average calculator implementation.
You'll have to enter values one by one. Hit [Enter] after each value.
No value and [Enter] displays result, then waits and thereafter exits ...Btw: I'm fully aware, that this is a beginners way, of doing it (done so, on purpose).
You'll have to enter values one by one. Hit [Enter] after each value.
No value and [Enter] displays result, then waits and thereafter exits ...
Code: Select all
' Average.bas -- 2018-05-20, MrSwiss
'
' compile: -s console
'
Type Average
Private:
As Double sum
As ULongInt cnt
Public:
Declare Sub AddOne(ByVal value As Double, ByRef ave As Average)
Declare Sub ShowAv(ByRef ave As Const Average)
End Type
Sub Average.AddOne(ByVal value As Double, ByRef ave As Average)
' here we modify the type to latest: sum & count
ave.sum += value ' add latest 'value'
ave.cnt += 1 ' increment counter
End Sub
Sub Average.ShowAv(ByRef ave As Const Average)
' here we use the type in 'read only' mode
Print ' get values & do the math., then _
Print "Average is: "; ave.sum / ave.cnt ' display it
Print
End Sub
' test/demo code-start
Dim As Average cAve ' use one (of above Type)
Dim As Double cValue ' temporary variable
Do
Cls : Locate 2, 1 ' clear screen | (below) get user input
Input "enter a amount, then press [Enter] (empty = show/quit) "; cValue
If cValue <> 0.0 Then ' if: new value received
cAve.AddOne(cValue, cAve) ' add freshly entered value
Else ' no new value: show result, then exit
cAve.ShowAv(cAve) ' show result
Print "press a key to exit ... ";
Sleep()
Exit Do ' end of prog.
End If
Sleep(100,1)
Loop
' test/demo code-end ' -----EOF -----
Re: How write a calculate average monthly sales program
Can only guess the format.
My guess, a list of numbers held in a string and separated by a known separator.
Suitable for not too long a list of numbers.
My guess, a list of numbers held in a string and separated by a known separator.
Code: Select all
#Include "file.bi"
#include "string.bi"
'file.bi for format
'string.bi for fileexists in loadfile
' =========== save and load text files ============
Sub savefile(filename As String,p As String)
Dim As Integer n
n=Freefile
If Open (filename For Binary Access Write As #n)=0 Then
Put #n,,p
Close
Else
Print "Unable to load " + filename
End If
End Sub
Function loadfile(file As String) As String
If Fileexists(file)=0 Then Print file;" not found":Sleep:End
Var f=Freefile
Open file For Binary Access Read As #f
Dim As String text
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
Return text
End Function
'==========================================
' ===== average and split together for average ========
Sub split(DataString As String,DataSeparator As String,var1 As String,var2 As String)
Dim As Long pst=Instr(DataString,DataSeparator),LD=Len(DataSeparator)
var1="":var2=""
If pst<>0 Then
var1=Mid(DataString,1,pst-1)
var2=Mid(DataString,pst+LD)
Else
var1=DataString
End If
End Sub
Function average(DataString As String,DataSeparator As String) As Double
Dim As String s=DataString,var1,var2
Dim As Long counter
Dim As Double d
Do
counter+=1
split(s,DataSeparator,var1,var2)
s=var2
d+=Val(var1)
Loop Until s=""
d= d/counter
Return d
End Function
'==============================================
'==== examples ========
Print "average"
Print average("1,2,3,4,5,-6.97,7,8,9,10,11,12,13,1234.08",",") 'Separator is a comma
Print "check average"
Print (1+2+3+4+5-6.97+7+8+9+10+11+12+13+1234.08)/14
Print "Press a key for a list of numbers in a file (could be sales)"
Sleep
'=====================
' ====== files example =======
Randomize
Dim As String s ' a random list of sales
Dim As String tmp
Dim As Double d
Dim As Long number=20000
For n As Long=1 To number
tmp=Format((Rnd*500),".00")+Chr(13,10) ' formatted number with carriage return and new line separator
s+=tmp 'string list for file
d+=Val(tmp) 'keep a double tally to check
Next
d=d/number 'double average
'save the list to a file
savefile ("numbers.dat",s)
'load the file into w
Dim As String w=loadfile("numbers.dat")
Print w
Dim As Double x=average(w,Chr(13,10)) 'Separator is a carriage return and new line CHR(13,10)
print "list length = ";number
Print "List Average = ";Format(x,".00"), "Check: ";d;" versus ";x
Kill "numbers.dat"
Print Iif(Fileexists("numbers.dat"),"delete file manually","File has been deleted")
Print "Done"
Sleep