PDA

View Full Version : vb.net for next nested loop question


konjammenson
03-22-2005, 11:17 PM
If anyone can help me out on this problem I would sure appreciate it.

The problem: Write a console application to determine which job pays more given the following information: Job A lasts thirty days and pays $25 a day, job B lasts thirty days and pays as follows: $1 first day, $2 second day, $3 third day, and so forth.

ok, so getting job a is real simple. I'm running into problems with finding job b, my code is as follows:

Sub Main()
dim a, b, c, d as integer

a = 0

For c = 1 to 30

a = a + 25
b = 0
for d = 1 To c
b = b + (b+1)
next d
next c

my logic here for b is that:

0 = 0 + (0 + 1) - b now equals 1
then 1 = 1 + (1 + 1) - b now equals 1 + 2 which is 3

for output, i am getting some incredibally high number, when i should be getting 465 (th number i got doing this calculation manually)

If anyone could help me with my logic here I would sure appreciate it.

Unit
03-22-2005, 11:38 PM
Why do you need this loop?

for d = 1 To c
b = b + (b+1)
next d

Wouldn't this do what you wanted?

a = 0
b = 0
For c = 1 to 30
a = a + 25 ' pay for method "a" - $25 per day
b = b + c ' pay for method "b" - $1 on first day, $1 increment for each day.
next c

the logic here is simple. you get pay equal to the day number (c). After each day, you add pay for that day. you do this for the 30 days.

konjammenson
03-23-2005, 03:05 AM
ah yes, that is simple. don't know why i was so intent on creating a nested loop when it is clearly not nescessary. thanks! -konjammenson

oracleguy
03-23-2005, 08:15 PM
Since this is VB7 you could make it even simpler:


For c = 1 to 30
a += 25 ' pay for method "a" - $25 per day
b += c ' pay for method "b" - $1 on first day, $1 increment for each day.
next c


Just thought I'd point that out.