How write a calculate average monthly sales program

New to FreeBASIC? Post your questions here.
Posts: 1
Joined: May 18, 2018 16:59

How write a calculate average monthly sales program

Postby geraldinejns » May 18, 2018 17:05

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: 1147
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: How write a calculate average monthly sales program

Postby jevans4949 » May 20, 2018 15:57

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.
Posts: 3036
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How write a calculate average monthly sales program

Postby MrSwiss » May 20, 2018 16:53

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
    As Double   sum
    As ULongInt cnt
    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
End Sub

' test/demo code-start
Dim As Average  cAve                    ' use one (of above Type)
Dim As Double   cValue                  ' temporary variable

    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 ... ";
        Exit Do                         ' end of prog.
    End If
' test/demo code-end    ' -----EOF -----
Btw: I'm fully aware, that this is a beginners way, of doing it (done so, on purpose).
Posts: 5701
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How write a calculate average monthly sales program

Postby dodicat » May 20, 2018 20:57

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 ""   
#include ""

' for format
' for fileexists in loadfile
' =========== save and load text files ============
Sub savefile(filename As String,p As String)
    Dim As Integer n
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        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)
    If pst<>0 Then
    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
    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)"

' ====== files example =======
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
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"

Suitable for not too long a list of numbers.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests