## How write a calculate average monthly sales program

New to FreeBASIC? Post your questions here.
geraldinejns
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.
jevans4949
Posts: 1147
Joined: May 08, 2006 21:58
Location: Crewe, England

### Re: How write a calculate average monthly sales program

Short answer is that you add up 12 monthly totals and divide by 12.
MrSwiss
Posts: 3036
Joined: Jun 02, 2013 9:27
Location: Switzerland

### 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 ...

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 TypeSub 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 counterEnd SubSub 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    PrintEnd Sub' test/demo code-startDim As Average  cAve                    ' use one (of above Type)Dim As Double   cValue                  ' temporary variableDo    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 -----`
Btw: I'm fully aware, that this is a beginners way, of doing it (done so, on purpose).
dodicat
Posts: 5701
Joined: Jan 10, 2006 20:30
Location: Scotland

### 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.

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 IfEnd SubFunction 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 textEnd 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 IfEnd SubFunction 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 dEnd 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 commaPrint "check average"Print (1+2+3+4+5-6.97+7+8+9+10+11+12+13+1234.08)/14Print "Press a key for a list of numbers in a file (could be sales)"Sleep'=====================' ====== files example =======RandomizeDim As String s ' a random list of salesDim As String tmpDim As Double dDim As Long number=20000For 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 checkNextd=d/number      'double average'save the list to a filesavefile ("numbers.dat",s)'load the file into wDim As String w=loadfile("numbers.dat")Print wDim As Double x=average(w,Chr(13,10)) 'Separator is a carriage return and new line  CHR(13,10)print "list length = ";numberPrint "List Average = ";Format(x,".00"), "Check: ";d;" versus ";xKill "numbers.dat"Print Iif(Fileexists("numbers.dat"),"delete file manually","File has been deleted")Print "Done"Sleep  `

Suitable for not too long a list of numbers.