Page 1 of 1
Using Variance Formulas in QBasic Programs
Posted: Thu Dec 12, 2002 1:12 am
by mhcurl
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
Re: Using Variance Formulas in QBasic Programs
Posted: Thu Dec 12, 2002 1:59 am
by vbmike56
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
Re: Using Variance Formulas in QBasic Programs
Posted: Thu Dec 12, 2002 2:44 pm
by crossroads
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
Re: Using Variance Formulas in QBasic Programs
Posted: Fri Dec 13, 2002 12:56 am
by mhcurl
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
Re: Using Variance Formulas in QBasic Programs
Posted: Fri Dec 13, 2002 4:18 am
by frankiebaby
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
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
Re: Using Variance Formulas in QBasic Programs
Posted: Fri Dec 13, 2002 7:59 am
by crossroads
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