Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program help please!
#11
I can't figure it out. The answers come back wrong but only off by a few.

Here is the contents of the dat file:

"Coke Classic",19.8
"Pepsi",18.8
"Diet Coke",7.7
"Diet Pepsi",4.8
"Dr. Pepper",4.3
"Sprite",3.5
"7-Up",3.4
"Mountain Dew",2.9

Here is the prog. It is running in QBasic 1.1


Code:
CLS
PRINT "Soft Drinks      Market Share      Gross Sales"
PRINT TAB(20); "Percent"
PRINT
OPEN "asgn5b2a.dat" FOR INPUT AS #1                                      
DO WHILE NOT EOF(1)                                            
INPUT #1, softDrink$, percentMarketShare                                  
LET grossSales = 40000000000#
LET grossSales = (grossSales * percentMarketShare) / 100                  
LET totalPercentMarketShare = totalPercentMarketShare + percentMarketShare
LET totalGrossSales = totalGrossSales + grossSales                        
LET a$ = "\          \       ##.##%       $$##,###,###,###"
PRINT USING a$; softDrink$; percentMarketShare; grossSales
LOOP
LET B$ = "\   \              ##.##%       $$##,###,###,###"
PRINT
PRINT TAB(20); "______        _______________"
PRINT USING B$; "Total"; totalPercentMarketShare; totalGrossSales
CLOSE #1                                                                   'Close file
END
Reply
#12
it's because gross sales is a single precision variable. so qb, as your computer in general, will round off on calculations with numbers as large as the ones you have. at least i think. try this:

dim grossSales as double

at the top of your program and see if it makes any more mistakes, or even fewer.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#13
Forgive my ignorance. I understand some of this statement.

DIM grossSales as double

What does "as double" mean?
Reply
#14
I did try this and it's comming back different and still off by a few. The QBasic book I have to not explain this, so I' trying to figure out what it means at google, if I can find it.
Reply
#15
AS DOUBLE:

Double is a data type, like Single, Integer, and Long.

check it out here:

http://qbasicnews.com/qboho/qckdouble.shtml
size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Reply
#16
I found this on the web and I assume if this is correct then it is inherent to QBasic. So, QBasic automatically adds the pound sign for double precision.

SeeSpot


I found this bug in Microsoft QBASIC which comes with MSDOS 5.0-6.22 about two years ago and my letter to PC Magazine about it was published but I thought I would post this on the Internet since I have not heard of any other things with it. When you try to add three certain numbers there is an incorrect answer put in:

PRINT 3.26 + 3.25 + 2.55

Run this and it will print out 9.059999 when it should be 9.06 for an
answer. I don't know if anyone else actually knows about this and
since QBASIC is pretty insignificant most people might not care about this but I figured it's interesting to post.

This kind of rounding error is a well known phenomenon in dialects of BASIC that use true-binary storage of numbers. The problem is that most numbers that can be written in simple, terminating *decimal* form become repeating fractions (with infinite numbers of digits to the right of the point) when written in binary. Since infinite numbers of digits cannot be stored, rounding off is necessary. If several such numbers are added, the rounding errors can become sufficient to show up when the sum is converted back into decimal form for display to the user.

You can improve things by putting "#" signs after the numbers:

PRINT 3.26# + 3.25# + 2.55#

This will force BASIC to represent the numbers to double precision, and will therefore reduce the rounding errors. However, perfect precision still cannot be achieved.

BASICs that use Binary-Coded Decimal for their internal representations of numbers do not suffer from this problem, since any number that can be represented exactly in decimal can also be represented exactly in BCD. I know people who have had to switch from true-binary to BCD versions of BASIC in order to avoid this difficulty.
Reply
#17
and quite a few people know about it. Smile It has nothing to do with Qbasic, QB, or any particular dialect of any particular programming language. It is caused by the fact that you are using a digital computer and digital computers can't store floating point numbers with infinite precision.



"PRINT 3.26 + 3.25 + 2.55

Run this and it will print out 9.059999 when it should be 9.06 for an answer. I don't know if anyone else actually knows about this and since QBASIC is pretty insignificant most people might not care about this but I figured it's interesting to post. "
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Reply
#18
I'm still stuck on this program. As it is now, it works but there has to be a better way. The part I'm referring to is:

Code:
REM Calculate total cost over twelve days
RESTORE
FOR i = 1 TO 12
READ gift$(i), cost(i)
LET n = n + 1
LET price = price + cost(i) * n
LET totalCost = totalCost + price
NEXT i

Is this the way it is or is thier a way to make this more simple becasue it just doesn't look right? I've tried probably 30 different ways but I cannot get it. Can I not get the same result somehow using this section?

Code:
REM Calculate daily cost over days
FOR i = 1 TO num
READ gift$(i), cost(i)
LET day = day + 1
LET dailyCost = dailyCost + cost(i) * day
REM LET totalCost = totalCost + dailyCost
PRINT day; gift$(i)
NEXT i

If I remove the REM from the statement above, it calculates the totalCost by the "num" that the user inputs. I've been trying to find a way for this to give me the whole 12 days no matter what "num" is entered. Is there a way to do this? I would appreciate any assistance. TIA

SeeSpot

Code:
REM The Twelve Days of Christmas
CLS
DIM gift$(1 TO 12), cost(1 TO 12)

DO
INPUT "Enter a day from 1 to 12: ", num$
LET num = VAL(num$)
IF num > 0 OR num <= 12 THEN CLS
LOOP UNTIL num > 0 AND num <= 12

LET day = 0
LET dailyCost = 0
LET totalCost = 0

PRINT "The gifts for day"; num; "are"

REM Calculate daily cost over days
FOR i = 1 TO num
READ gift$(i), cost(i)
LET day = day + 1
LET dailyCost = dailyCost + cost(i) * day
REM LET totalCost = totalCost + dailyCost
PRINT day; gift$(i)
NEXT i

REM Calculate total cost over twelve days
RESTORE
FOR i = 1 TO 12
READ gift$(i), cost(i)
LET n = n + 1
LET price = price + cost(i) * n
LET totalCost = totalCost + price
NEXT i


PRINT USING "Cost:$$##,###.##"; dailyCost
PRINT USING "Total cost for the twelve days:$$##,###.##"; totalCost


REM ---Data: gifts, costs
DATA Partridge in a Pear Tree, 27.50, Turtledoves, 25.00
DATA French Hens, 5.00, Calling Birds, 70.00
DATA Golden Rings, 60.00, Geese-A-Laying, 25.00
DATA Swans-A-Swimming, 1000.00, Maids-A-Milking, 4.25
DATA Ladies dancing, 289.50, Lords-A-Leaping, 292.50
DATA Pipers Piping, 95.75, Drummers Drumming, 95.00
END
Reply
#19
yeah, i did the calculations out, glenn's bug is right. i remember some people "discovering" it awhile ago but i never figured it was big enough to make a difference. i'll play with some "alternative" methods i've been thinking about later.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)