Using Variance Formulas in QBasic Programs

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
mhcurl
Newbie
Posts:2
Joined:Thu Dec 12, 2002 12:54 am
Using Variance Formulas in QBasic Programs

Post by mhcurl » Thu Dec 12, 2002 1:12 am

Hello everyone,

   I am trying to write QBasic code to find the variance of the following situation.  I am a newbie when it comes to QBasic.  Any help received would be appreciated.  Thank you in advance.  You may also send replies to mikecurl74@aol.com.

***Find the mean (M), the variance (V), and the Highest Number entered of a variable number of student grades (n).  The formulas for the mean and variance are as follows:

M= (G1+G2+G3)/n
(I have this working.)

V= [(G1-M)2+(g2-m)2+...(Gn-M)2]/(n-1)

( I can't get that to come out right.  Any suggestions?  Or is the code completely wrong for the variance?  Is there a way to write the variance formula so that it makes since in QBasic?)

Mike
mikecurl74@aol.com

vbmike56
Newbie
Posts:5
Joined:Sat Nov 30, 2002 7:51 am

Re: Using Variance Formulas in QBasic Programs

Post by vbmike56 » Thu Dec 12, 2002 1:59 am

You might want to try declaring you variable out front.
Intergers will only goto 32767. Try using
DIM G as single
DIM M as single
etc.
if not try
DIM G as DOUBLE
etc.
You must leave enough room for large numbers

good luck

User avatar
crossroads
Administrator
Posts:34
Joined:Wed Feb 13, 2002 10:15 pm
Location:Germany
Contact:

Re: Using Variance Formulas in QBasic Programs

Post by crossroads » Thu Dec 12, 2002 2:44 pm

Try this:

DEFDBL A-Z
CLS

M = 0: V = 0
n = 3

DIM G(1 TO n)
DATA 7,8,34

FOR i = 1 TO n
READ G(i)
NEXT i

'M = (G1+G2+G3)/n
FOR i = 1 TO n
M = M + G(i)
NEXT i
M = M / n

'V = [(G1-M)2+(g2-m)2+...(Gn-M)2]/(n-1)
FOR i = 1 TO n
V = V + (G(i) - M) * 2
NEXT i
V = V / (n - 1)

PRINT "M ="; M
PRINT "V ="; V
crossroads (QBCafe Forum Admin)

mhcurl
Newbie
Posts:2
Joined:Thu Dec 12, 2002 12:54 am

Re: Using Variance Formulas in QBasic Programs

Post by mhcurl » Fri Dec 13, 2002 12:56 am

Thanks for the help guys.  It does work that way.  But what if I don't know how many grades will be entered?  Is there a way I can set it up to use an unknown variable instead of entering the grades themselves in the program?

Mike

User avatar
frankiebaby
Global Moderator
Posts:95
Joined:Tue Apr 30, 2002 1:38 am
Location:Pennsylvania
Contact:

Re: Using Variance Formulas in QBasic Programs

Post by frankiebaby » Fri Dec 13, 2002 4:18 am

Well, you need to use a loop like

DO
INPUT "GRADE" G(i)
i = i + 1
LOOP


instead of READ G(i)

also, you need to output what the sentinel, or terminating input is

ie:
LIMIT = 100   ' max number of grades to be entered
DIM g(LIMIT - 1)

print "ENTER "101" to end.
i = 0
DO
INPUT "GRADE", grade

if grade >= 0 and grade <= 100 then
G(i) = grade
&nbsp;i = i + 1
end if

LOOP UNTIL grade = 101 or i = LIMIT


there must be some limit, the limit i think is 32767 for array spaces, but thats probly more than you need, just be sure to DIM the array of grades high enough

User avatar
crossroads
Administrator
Posts:34
Joined:Wed Feb 13, 2002 10:15 pm
Location:Germany
Contact:

Re: Using Variance Formulas in QBasic Programs

Post by crossroads » Fri Dec 13, 2002 7:59 am

This is the complete code including Frankybaby's input routine:

DEFDBL A-Z
CLS

M = 0: V = 0
LIMIT = 100   ' max number of grades to be entered
MAXGRADE = 100
DIM G(1 TO LIMIT)

PRINT "ENTER "; MAXGRADE + 1; " to end. "
n = 0
DO
   PRINT n + 1;
   INPUT ". GRADE: ", grade
   
   IF grade >= 0 AND grade <= MAXGRADE THEN
    n = n + 1
    G(n) = grade
   END IF
LOOP UNTIL grade = MAXGRADE + 1 OR n = LIMIT
IF n < 2 THEN END


'M = (G1+G2+G3)/n
FOR i = 1 TO n
M = M + G(i)
NEXT i
M = M / n

'V = [(G1-M)2+(g2-m)2+...(Gn-M)2]/(n-1)
FOR i = 1 TO n
V = V + (G(i) - M) * 2
NEXT i
V = V / (n - 1)

PRINT
PRINT "M ="; M
PRINT "V ="; V
crossroads (QBCafe Forum Admin)

Post Reply