locked
How to calculate the sum of any amount of data points, regardless of amount? RRS feed

  • Question

  • I am trying to write a program that will calculate standard deviation (it's a statistics thing). I realize that programs such as Excel can do this for me, but I am doing this as a personal project. But, is there a way to calculate the sum of any number of data points, without knowing how many there will be before the program starts?

    My code looks like this right now:

    -------------------------------------------------------------------------------------------------

    Sub DataAsk
      TextWindow.WriteLine("Enter data point # " + data + ":")
      datapoint[data] = TextWindow.ReadNumber()
    EndSub
    Sub DataSum
      For data = 1 To datas
        datasum = datapoint[data]
      EndFor
    EndSub
    TextWindow.WriteLine("Enter the amount of data points you have (in numeral form):")
    datas = TextWindow.Read()
    For data = 1 To datas
      For data = 1 To datas
        DataAsk()
      EndFor
      DataSum()
    EndFor
    TextWindow.WriteLine("This is your sum: " + datasum)

    --------------------------------------------------------------------------------------------------------

    That last line won't always be there, I wrote it for testing purposes.

    Any help at all will be appreciated.

    Tuesday, October 14, 2014 3:15 AM

Answers

  • You can use the following program (or similar)

    stop=0
    n=1
    TextWindow.WriteLine(" end of input by entering nothing")
    While stop=0
      TextWindow.Write("enter point "+n+" : ")
      data=TextWindow.ReadNumber()
      If data<>0 then
        sum=sum+data
        sum2=sum2+data*data
        n=n+1
      Else
        stop=1
        n=n-1
      EndIf
    EndWhile
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Number of points= "+n)
    TextWindow.WriteLine("Total sum       = "+sum)
    TextWindow.WriteLine("Sum of squares  = "+sum2)
    


    Jan [ WhTurner ] The Netherlands

    • Proposed as answer by litdev Tuesday, October 14, 2014 5:33 PM
    • Marked as answer by IshallLive Tuesday, October 14, 2014 9:10 PM
    Tuesday, October 14, 2014 3:14 PM
    Answerer

All replies

  • You can use the following program (or similar)

    stop=0
    n=1
    TextWindow.WriteLine(" end of input by entering nothing")
    While stop=0
      TextWindow.Write("enter point "+n+" : ")
      data=TextWindow.ReadNumber()
      If data<>0 then
        sum=sum+data
        sum2=sum2+data*data
        n=n+1
      Else
        stop=1
        n=n-1
      EndIf
    EndWhile
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Number of points= "+n)
    TextWindow.WriteLine("Total sum       = "+sum)
    TextWindow.WriteLine("Sum of squares  = "+sum2)
    


    Jan [ WhTurner ] The Netherlands

    • Proposed as answer by litdev Tuesday, October 14, 2014 5:33 PM
    • Marked as answer by IshallLive Tuesday, October 14, 2014 9:10 PM
    Tuesday, October 14, 2014 3:14 PM
    Answerer
  • Thank you very much, sir.

    Tuesday, October 14, 2014 9:10 PM