help!!!!!!!

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

Moderators:Administrator, Global Moderator

Post Reply
User avatar
yacinator
Full Member
Posts:33
Joined:Sun Dec 21, 2003 12:02 am
help!!!!!!!

Post by yacinator » Fri Jan 16, 2004 4:57 am

I'm learning QBasic and I'm not very good at it yet. I wrote a program that adds ,subtracts,multiplies or divides fractions. The output is decimal. For some reason it doesn't work. I have no idea why plese help me !!!!!!!  ???

this is the program:

DECLARE SUB enter (numerator!(), denumerator!(), op$(), count!)
DECLARE SUB process (numerator!(), denumerator!(), op$(), count!, answer!)
DECLARE FUNCTION plus! (numerator!(), denumerator!(), answer!, i!)
DECLARE FUNCTION minus! (numerator!(), denumerator!(), answer!, i!)
DECLARE FUNCTION times! (numerator!(), denumerator!(), answer!, i!)
DECLARE FUNCTION divide! (numerator!(), denumerator!(), answer!, i!)
DECLARE SUB display (answer!)
CLS
DIM numerator(1 TO 150)
DIM denumerator(1 TO 150)
DIM op$(1 TO 150)
CALL enter(numerator(), denumerator(), op$(), count)
CALL process(numerator(), denumerator(), op$(), count, answer)
PRINT
CALL display(answer)
END

SUB display (answer)
PRINT answer * 2
END SUB

FUNCTION divide (numerator(), denumerator(), answer, i)
LET divide = answer / (1 / denumerator(i) * numerator(i))
END FUNCTION

SUB enter (numerator(), denumerator(), op$(), count)
LET count = 1
DO
INPUT "numerator", numerator(count)
INPUT "denumerator", denumerator(count)
INPUT "operation", op$(count)
LOOP UNTIL op$(count) = "="
END SUB

FUNCTION minus (numerator(), denumerator(), answer, i)
LET minus = answer - (1 / denumerator(i) * numerator(i))
END FUNCTION

FUNCTION plus (numerator(), denumerator(), answer, i)
LET plus = answer + (1 / denumerator(i) * numerator(i))
END FUNCTION

SUB process (numerator(), denumerator(), op$(), count, answer)
LET answer = (1 / denumerator(1)) * numerator(1)
FOR i = 2 TO count
SELECT CASE op$(i)
CASE IS = "+"
LET answer = plus(numerator(), denumerator(), answer, i)
CASE IS = "-"
LET answer = minus(numerator(), denumerator(), answer, i)
CASE IS = "*"
LET answer = times(numerator(), denumerator(), answer, i)
CASE IS = "/"
LET answer = times(numerator(), denumerator(), answer, i)
END SELECT
NEXT i
END SUB

FUNCTION times (numerator(), denumerator(), answer, i)
LET times = answer * (1 / denumerator(i) * numerator(i))
END FUNCTION

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

Re: help!!!!!!!

Post by frankiebaby » Fri Jan 16, 2004 3:09 pm

Try this out, u made a few simple mistakes, and i made some changes to simplify the 'fraction making' process

DECLARE SUB enter (numerator!(), denumerator!(), op$(), count!)
DECLARE SUB process (numerator!(), denumerator!(), op$(), count!, answer!)
DECLARE FUNCTION plus! (numerator!(), denumerator!(), answer!, i!)
DECLARE FUNCTION minus! (numerator!(), denumerator!(), answer!, i!)
DECLARE FUNCTION times! (numerator!(), denumerator!(), answer!, i!)
DECLARE FUNCTION divide! (numerator!(), denumerator!(), answer!, i!)
DECLARE SUB display (answer!)
CLS
DIM numerator(1 TO 150)
DIM denumerator(1 TO 150)
DIM op$(1 TO 150)
CALL enter(numerator(), denumerator(), op$(), count)
CALL process(numerator(), denumerator(), op$(), count, answer)
PRINT
CALL display(answer)
END


SUB display (answer)
PRINT answer '* 2 not entirely sure why this *2 is here, so i removed it

END SUB

FUNCTION divide (numerator(), denumerator(), answer, i)
LET divide = answer / (1 / denumerator(i) * numerator(i))

END FUNCTION

SUB enter (numerator(), denumerator(), op$(), count)
LET count = 1
DO
INPUT "numerator", numerator(count)
INPUT "denumerator", denumerator(count)
INPUT "operation", op$(count)
count = count + 1   ' you were missing this!
LOOP UNTIL op$(count - 1) = "=" 'to adjust to adding one, subtract one

END SUB

FUNCTION minus (numerator(), denumerator(), answer, i)
LET minus = answer - (1 / denumerator(i) * numerator(i))


END FUNCTION

FUNCTION plus (numerator(), denumerator(), answer, i)
LET plus = answer + (numerator(i) / denumerator(i))


END FUNCTION

SUB process (numerator(), denumerator(), op$(), count, answer)
'LET answer = (1 / denumerator(1)) * numerator(1)
LET answer = numerator(1) / denumerator(1)
FOR i = 2 TO count
SELECT CASE op$(i - 1)  'changed this, you want the last one's op, not this new one's
CASE IS = "+"
LET answer = plus(numerator(), denumerator(), answer, i)
CASE IS = "-"
LET answer = minus(numerator(), denumerator(), answer, i)
CASE IS = "*"
LET answer = times(numerator(), denumerator(), answer, i)
CASE IS = "/"
LET answer = times(numerator(), denumerator(), answer, i)
END SELECT
NEXT i


END SUB

FUNCTION times (numerator(), denumerator(), answer, i)
LET times = answer * (1 / denumerator(i) * numerator(i))
END FUNCTION

User avatar
yacinator
Full Member
Posts:33
Joined:Sun Dec 21, 2003 12:02 am

Re: help!!!!!!!

Post by yacinator » Sat Jan 17, 2004 3:24 am

Thanks a lot for helping me  :). I tried it and it works perfectly now. I'm just wondering why after :

LET answer=[1/denumerator[1]] * numerator[1]

you put :

LET answer=denumerator[1] / numerator[1]

I'm very grateful for you fixing my program, but I would like to know why you put in that line , so in the future I won't make any more mistakes.

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

Re: help!!!!!!!

Post by frankiebaby » Sun Jan 18, 2004 6:41 pm

well, i wasnt too sure about this one:
LET answer=(1/denumerator(1)) * numerator(1)
b/c it just seems to be more work than neccesary to get the decimal form of the fraction.

this will return the same answer, only it uses fewer operations and no parenthesis. This makes it easier to read and faster when executing. Not that speed matters much in this application, but in another it might.
LET answer=denumerator(1) / numerator(1)

User avatar
yacinator
Full Member
Posts:33
Joined:Sun Dec 21, 2003 12:02 am

Re: help!!!!!!!

Post by yacinator » Mon Jan 19, 2004 3:39 am

Thanks a lot for explaining this to me :)

User avatar
yacinator
Full Member
Posts:33
Joined:Sun Dec 21, 2003 12:02 am

Re: help!!!!!!!

Post by yacinator » Mon Jan 19, 2004 10:38 am

This is a program that finds the mode of an array. nathanj posted a question on how to do it. I wrote the program that finds the mode but it doesn't quite work.
See what it does it displays the total numeber of numbers not the number of times the mode ocurrs.
Here's the code:
DECLARE SUB mode (m!(), check!(), num!)
DECLARE SUB bubble (check!(), num!)
CLS
DIM m(1 TO 5)
DIM check(1 TO 5)
CALL mode(m(), check(), num)
CALL bubble(check(), num)
PRINT check(1)
DATA 40,50,40,30,40
END

SUB bubble (check(), num)
FOR i = 1 TO num - 1
FOR n = 1 TO n - num
IF check(n) < check(n + 1) THEN
SWAP check(n), check(n + 1)
END IF
NEXT n
NEXT i
END SUB

SUB mode (m(), check(), num)
FOR i = 1 TO 5
DO WHILE num < 5
LET num = num + 1
IF m(i) = m(num) THEN
LET check(i) = check(i) + 1
END IF
LOOP
LET num = 0
NEXT i
END SUB

What it does is it checks if the first number equals any other numbers and if it does it adds one to count.
Then it orders the count array and displays the first of the array. I worked on it for two days and I don't know what the hell is wrong with this program &nbsp;:o.
Can you please help me [and nathanj too].

Also if anybody wants help with anything they can post it here.

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

Re: help!!!!!!!

Post by frankiebaby » Mon Jan 19, 2004 6:17 pm

ok, heres a way to find the mode, but there is some redundancy, so its not extremely efficient, oh well:
(note, does not ned sorted data)

CONST numdata = 5' how many number to have
DIM array(numdata) &nbsp;AS INTEGER
'fill the array with numbers first, im not going to show you that &nbsp;:P

MAX = 0
NUM = 0

FOR count = 0 to numdata - 1 'pick which number to look for
&nbsp; &nbsp;CURRENT = array(count)
&nbsp; &nbsp;MANY = 0 'how many times it shows up
&nbsp; &nbsp; FOR count2 = count to numdata - 1 &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;IF array (count2) = CURRENT THEN MANY = MANY + 1 ' count how many times it shows up
&nbsp; &nbsp;NEXT
&nbsp; &nbsp;IF MANY > MAX THEN
&nbsp; &nbsp; &nbsp; &nbsp; NUM = CURRENT &nbsp; &nbsp; &nbsp; 'if it beats the record, mark it
&nbsp; &nbsp; &nbsp; &nbsp; MAX = MANY
&nbsp; &nbsp;END IF
NEXT


PRINT "THE MODE IS "; NUM; ", OCCURING "; MAX; " TIMES!"

User avatar
yacinator
Full Member
Posts:33
Joined:Sun Dec 21, 2003 12:02 am

Re: help!!!!!!!

Post by yacinator » Fri Jan 23, 2004 11:59 am

thanks for the help
:) ;) :D ;D >:( :( :o 8) ??? ::) :P :-[ :-X :-/ :-* :'(

Post Reply