Qbasicnews.com
Program help please! - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: Program help please! (/thread-305.html)

Pages: 1 2


Program help please! - SeeSpot - 03-01-2003

I'm very new to programming and I'm taking a distance course for programming in QBaisc. I've been working on a program for school and I'm just having problems with it. I'm not asking for someone to write it but rather some direction. Below is the problem and a sample output. What I have done so far is under that and I'm just confused on many things and need some help understanding. I'm asking for help now because I've been stuck for awhile. The text is informative but maybe I'm missing something. I think I'm having trouble more or less with the formula. TIA!

SeeSpot


The Twelve Days of Christmas. Each year the Provident National Bank of Philadelphia publishes a Christmas price list (see table 4). Write a program that requests an integer from 1 to 12 and then lists the gifts for that day along with that day’s costs. On the nth day, the n gifts are 1 partridge in a pear tree, 2 turtledoves…n of the nth item. The program should also give the total cost of all twelve days. A sample output is as follows:

[run]
Enter a day from 1 to 12: 3
The gifts for day 3 are
1 partridge in a pear tree
2 turtledoves
3 French hens
Costs: $92.50
Total cost for the twelve days: $71,618.50

Table 4: Christmas Price Index (1992)

Item Cost (in dollars)
Partridge in a pear tree 27.50
Turtledove 25.00
French hen 15.00
Calling bird 70.00
Gold ring 60.00
Geese-a-laying 25.00
Swan-a-swimming 1000.00
Maid-a-milking 4.25
Lady dancing 289.50
Lord-a-leaping 292.50
Piper piping 95.75
Drummer drumming 95.00


Here is what I have so far:

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

FOR i = 1 TO 12
READ gift$(i), cost(i)
NEXT i

DO
INPUT "Enter a day from 1 to 12: ", num
SELECT CASE num
CASE 1
PRINT gift$(1)
CASE 2
CASE 3
CASE 4
CASE 5
CASE 6
CASE 7
CASE 8
CASE 9
CASE 10
CASE 11
CASE 12
CASE ELSE
PRINT "Please try again!"
END SELECT
LOOP UNTIL num <= 12

REM ---Data: gifts, costs
DATA Partridge in a pear tree, 27.50, Turtledove, 25.00
DATA French hen, 5.00, Calling bird, 70.00
DATA Gold ring, 60.00, Geese-a-laying, 25.00
DATA Swan-a-swimming, 1000.00, Maid-a-milking, 4.25
DATA Lady dancing, 289.50, Lord-a-leaping, 292.50
DATA Piper piping, 95.75, Drummer drumming, 95.00
END



Program help please! - pr0gger - 03-01-2003

You're actually doing quite well. There is an easier way to do this than SELECT CASE, however. A hint:

Code:
FOR i = 1 to num
'
'Put code here
'
NEXT
'The below will print the total cost added up.
PRINT TotalCost

Inside the loop, put codes to print all the gifts up to that day, add up the cost, and put it in the TotalCost varible.


Program help please! - SeeSpot - 03-01-2003

pr0gger,

This make more since. I'll work on this approach to see if I can get it working. You make it sound easy!

Thanks.


Program help please! - pr0gger - 03-01-2003

Smile you're welcome. once you figure it out it will seem easy, trust me. good luck.


Program help please! - SeeSpot - 03-01-2003

pr0gger or anyone else,

Okay,

I tried your suggestion, which took me a while but I have it working for the most part. Thanks again! However, I cannot for the life of me figure out totalCost. Based on the sample output I was given, the totalCost for 12 days should be 71618.50 but I think this is a misprint because I cannot in any way come up with that number. The way I have it figured is below: I been trying to figure this part out for about 4 hours and I have not even come close. I would appreciate any guidance. Below is what I have completed. TIA!

SeeSpot
  • day cost total day total
    1 x 27.50 27.50 x 12 330.00
    2 x 25.00 50.00 x 11 550.00
    3 x 5.00 15.00 x 10 150.00
    4 x 70.00 280.00 x 9 2520.00
    5 x 60.00 300.00 x 8 2400.00
    6 x 25.00 150.00 x 7 1050.00
    7 x 1000.00 7000.00 x 6 42000.00
    8 x 4.25 34.00 x 5 170.00
    9 x 289.50 2605.50 x 4 10422.00
    10 x 292.50 2925.00 x 3 8775.00
    11 x 95.75 1053.25 x 2 2106.50
    12 x 95.00 1140.00 x 1 1140.00
    total 15580.25 total 71613.50

Code:
REM The Twelve Days of Christmas
CLS
DIM gift$(1 TO 12), cost(1 TO 12)
INPUT "Enter a day from 1 to 12: ", num
PRINT "The gifts for day"; num; "are"

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

FOR i = 1 TO num
READ gift$(i), cost(i)
LET day = day + 1
LET dailyCost = dailyCost + cost(i) * day
PRINT day; gift$(i)
NEXT i

PRINT USING "Cost:$$##,###.##"; dailyCost
PRINT 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 Gold 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



Program help please! - toonski84 - 03-01-2003

totalcost = totalcost + dailycost works if i put it in the loop.

tip from program to programmer: LET and REM are archaic throwbacks to older basic compilers. you dont need the word let at all, you can just say something like "x =5" and it'll register. as for REM, it can be replaced by: "x = 5 ' comment". just a little easier on your typing fingers Smile


Program help please! - SeeSpot - 03-01-2003

toonski84,

Thanks! Your are correct and I had this working. The sample output shows that the user entered 3 and it came back with a cost of (92.50), which is correct. It also shows that the totalcost for twelve days is (71,613.50) even though the user entered 3. If I use this method and enter 3 it comes back with a cost of (197.50). This is why I'm lost. Somehow, I have to be able to keep the cost of the twelve days the same no matter what day is entered. This is what is causing me to go bald tonight. Saturday, it will be the relatives coming in. Any sugesting? TIA!

SeeSpot


Program help please! - SeeSpot - 03-01-2003

toonski84,

By the way, I agree with you on the tips because I've seen this done and I personally think it's a better way of doing things. However, you know how those professors are? Thanks again!

SeeSpot


Program help please! - SeeSpot - 03-03-2003

Can someone sheld some light on this for me.

Why when I enter

Let = 40000000000

and press enter it comes back as

Let = 40000000000#

I'm trying some calculations and the answer are coming back wrong. Would appreciate any help. TIA


Program help please! - toonski84 - 03-03-2003

could you post those calculations? doesnt do that for me...

heh, i just found out playing with it that qb has scientific notation for numbers with 16 and over digits:

type in this:
x = 4d+15
x = 4d+16