views:

141

answers:

4

I am trying to do an accumulator in IBasic for a college assignment and I have the general stuff down but I cannot get it to accumulate. The code is below. My question is how do I get it to accumulate and pass to the different module.

I'm trying to calculate how many right answers the user gets. Also, i need to calculate the percentage of right answers. so if the user gets 9 out of 10 right theyed answer 90% right.

'October 15, 2009  '
'Lab 7.5 Programming Challenge 1 - Average Test Scores  '
'This is a dice game  '

declare main()  
declare inputName(name:string)  
declare getAnswer(num1:int, num2:int)  
declare getResult(num1:int, num2:int, answer:int)  
declare avgRight(getRight:int)  
declare printInfo(name:string, getRight:int, averege:float)  

openconsole  
main()  
do:until inkey$<>""  
closeconsole  
end  

sub main()  
    def name:string  
    def num1, num2, answer, total, getRight:int  
    def averege:float  
    inputName (name)  
    getRight = 0  
    For counter = 1 to 10  
        getRight = getAnswer(num1, num2)  
        getRight = getRight + 1  
    next counter  
        average = avgRight (getRight) 
    printInfo(Name, getRight, average)  
end  

sub inputName (name)  
    Input "Please enter your name: " ,name  
return  

sub getAnswer(num1, num2)  
    def answer, getRight:int    
    num1 = rnd (10) + 1  
    num2 = rnd (10) + 1  
    Print num1, "+ " ,num2  
    Input "What is the answer to the equation? " ,answer  
    getRight = getResult(num1, num2, answer)  
    return getRight

sub getResult(num1, num2, answer)   
    def getRight:int
    if answer = num1 + num2  
        getRight = 1 
    else  
        getRight = 0
    endif  
    return getRight  

sub avgRight(getRight)  
    def average:float  
    average = getRight / 10  
    return average  

sub printInfo(name, getRight, averege)  
    Print "The students name is: " ,name  
    Print "The number right is: " ,getRight   
    Print Using ("&##.#&", "The average right is " ,averege * 100, "%")  
return  
A: 

Hint: you can't take the average of three numbers until after you have three numbers (or the sum of them).

Jeanne Pindar
sorry i should have clarified its asking for the average of right answers...so once i get the accumulator working it would divide the number of right answers by 10. (it says 3 but ima change it to the original of 10)
Tara
A: 

Here's a (fairly big) hint.

You need to set an accumulator to zero before starting to ask questions (before the counter loop).

Your getAnswer function should just return 0 (if user was wrong) or 1 (if user was right).

You need to add that answer to your accumultor inside the loop.

When the loop is finished, your accumulator will have the number of questions that the user got right. The percentage is then that multiplied by 100 then divided by the number of questions asked.

In other words, don't worry about a running average, just a running count. You work out the average at the end when you have all the information required.

Update: Tara, in your latest code, you still have a few issues.

1/ The getRight variable belongs to the main function, I'm not sure you'll be able to add to it from within getResult.

2/ It would be better if getResult just returned 0 or 1 depending on whether you got it right or not. In other words, something like this (pseudo-code but easily translatable):

if answer = num1 + num2:
    print "Right"
    return 1
print "Wrong"
return 0

3/ Then you can rweturn that directly from the end of getAnswer rather than trying to use the getRight variable:

return getResult (num1, num2, answer)

4/ Then (I think you misunderstood what I said here), you add that to the running count inside the loop. This will increase the accumulator by one for each correct answer:

getRight = getRight + getAnswer (num1, num2)

5/ Again possibly a misunderstanding, you should move the average calculation (the call to avgRight) to after the loop. It doesn't cause any trouble being where it is but you're calculating after every question whereas you only need to calculate at the end.

6/ If you're going to repost the code, try to format it better - you can get code-like formatting by ensuring there are at least four spaces at the start of each line (and use more spaces for indentation). I don't really want to have to format it again :-)

Update 2: Since you're at least trying, Tara, I don't mind offering some more specific help. You're pretty close to a solution now.

If you want to pass the value into and out of the functions rather than just adding one or zero to it in the loop, here's what you do:

1/ Change getResult as follows. This accepts the current number of right answers and returns the updated one, based on the whether or not the answer was correct.

sub getResult(num1, num2, answer, currRight)   
    if answer = num1 + num2  
        Print "Right"  
        return currRight + 1  
    endif  
    Print "Wrong"  
    return currRight

2/ Change getAnswer the same way.

sub getAnswer(num1, num2, currRight)  
    def answer:int  
    num1 = rnd (10) + 1  
    num2 = rnd (10) + 1  
    Print num1, "+ " ,num2  
    Input "What is the answer to the equation? " ,answer  
    return getResult(num1, num2, answer, currResult)

3/ Change the getAnswer call to pass in the value and get back the updated value:

For counter = 1 to 10  
    getRight = getAnswer(num1, num2, getRight)

4/ Define num1 and num2 in getAnswer and remove both of them, answer and total from main - they're not needed there. Spell average consistently in all places :-)

5/ You may need to define name within sub inputName() and return it, rather than passing it in. I don't know IBasic well enough to know if it can change passed parameters directly.

Update 3:

This bit of code here is wrong:

For counter = 1 to 10
    getRight = getAnswer(num1, num2)
    getRight = getRight + 1
next counter

What you're doing is getting the return value (0 or 1) into getRight after every question, then adding one to it - that means you're losing the history from previous questions. This will end up giving you 1 if you got the last question wrong or 2 if you got it right. Try this instead:

For counter = 1 to 10
    getRight = getRight + getAnswer(num1, num2)
next counter

This will add the return value (0 or 1) to getRight for each question.

paxdiablo
that actually makes since...i'm going to attempt it, i reposted the code and now i am actually going to try to get it running on the IBasic program.
Tara
I really do appriciate you helping me. as you can tell i am still learning this site and the IBasic program.
Tara
Also for step 4 it wants the average right. not the average of right answers. im getting sleepy so its getting hard for me to work on this since im getting avragated (sp)
Tara
Not sure I understand, Tara. What is the average right if not the ratio of right answers to total answers? Actually, intentional or not "avragated" is actually a nice little pun (agravated, average-ated) - nice one.
paxdiablo
okay....ummmm we can nix the averaging part and i will figure that once i get this accumulating. i have been trying to figure out my wrong doings since thursday and i havnt gotten anywhere. LoL though i am one step closer.
Tara
I figured out some of my wrong doings but in my mind it should work but it still isnt accumulating. I have returned getRight as a 1 or 0 in my getResult function to getAnswers from their I returned it to main where i have my accumulator.
Tara
See update 3 for further details.
paxdiablo
A: 

You seem to have a typo in you posted source.

def averege:float

It's spelled average later.

You might try inserting print statements in your loop. These statements would let you observe how you variables are changing during each iteration of the loop.

Something like:

Print "Current counter value is", counter
Print "Current getRight value is", getRight

Similar statements in the subroutines may help you isolate where the problem is in your code.

Dave L Delaney
Thank you, i guess i missed that one when i fixed the others
Tara
A: 

Ya'll are amazing, expecially you pax!! The program finally works thank you SOOOOOO much.

'October 15, 2009  '
'Lab 7.5 Programming Challenge 1
'This is a dice game  '

declare main()  
declare inputName(name:string)  
declare getAnswer(num1:int, num2:int)  
declare getResult(num1:int, num2:int, answer:int)  
declare avgRight(getRight:int)  
declare printInfo(name:string, getRight:int, averege:float)  

openconsole  
main()  
do:until inkey$<>""  
closeconsole  
end  

sub main()  
def name:string  
def num1, num2, answer, total, getRight:int  
def average:float  
inputName (name) 
getRight = 0 
    For counter = 1 to 10  
        getRight = getRight + getAnswer(num1, num2)
    next counter  
        average = avgRight (getRight) 
    printInfo(Name, getRight, average)  
end  

sub inputName (name)  
    Input "Please enter your name: " ,name  
return  

sub getAnswer(num1, num2)  
def answer, getRight:int 
num1 = rnd (10) + 1  
num2 = rnd (10) + 1  
    Print num1, "+ " ,num2  
    Input "What is the answer to the equation? " ,answer 
    getRight = getResult(num1, num2, answer)  
return getRight

sub getResult(num1, num2, answer)   
def getRight:int
if answer = num1 + num2  
    print "Right"
    getRight = 1 
else
    print "wrong"  
    getRight = 0
endif  
return getRight  

sub avgRight(getRight)  
    def average:float  
    average = getRight / 10  
return average  

sub printInfo(name, getRight, averege)  
    Print "The students name is: " ,name  
    Print "The number right is: " ,getRight   
    Print Using ("&###&", "The average right is " ,averege * 100, "%")  
return  
Tara
@Tara, you should upvote the answers that helped and accept the one you consider the best.
paxdiablo
i tried....it didnt let me
Tara