PDA

View Full Version : QBasic help


ladijock
Dec 14, 2009, 03:11 PM
How about this one.. can anyone help?

How about this one?

You agree to work for a certain employer, but ask to be paid in a peculiar way. After the first day you are paid one penny. On each sucsessive day your pay is doubled. Write a program that determines your pay after 30 days. Your program should display your pay for each day of the 30 day period and the total pay earned after each day.

Sample Display:

Day Daily Pay Total Pay

1 0.01 0.01
2 0.02 0.03
3 0.04 0.07

Perito
Dec 14, 2009, 08:09 PM
How about this one..can anyone help?

How about this one?

You agree to work for a certain employer, but ask to be payed in a peculiar way. After the first day you are paid one penny. On each sucsessive day your pay is doubled. Write a program that determines your pay after 30 days. Your program should display your pay for each day of the 30 day period and the total pay earned after each day.

Sample Display:

Day Daily Pay Total Pay

1 0.01 0.01
2 0.02 0.03
3 0.04 0.07


I'm not sure this is the proper place to put your question. It's really a mathematics question, not a programming one.

Nonetheless, the first day your pay is 0.01. The second day, 0.02, the third day 0.04. In general, the day's pay is

0.01 \,\times\, 2^{(n-1)}

where n is the day number. Note that 2^0 = 1

So, you perform the summation

\sum_{i=1}^{30} \left( 0.01\,\times\, 2^{(n-1)} \right)

On a computer, in any basic language, this is totally trivial

Dim sum As Double
Dim onedays_pay As Double
Dim i As Integer
sum = 0

For i = 1 To 30
onedays_pay = 0.01 * 2 ^ (i - 1)
sum = sum + onedays_pay
Print "On day " & CStr(i) & " the payment is " & CStr(onedays_pay) & ", and the total so far is " & CStr(sum)
Next i

Print "The total pay is: " & sum