• 0

Question

I should have just added this in my topic yesterday :P 

 

I'm trying create a for loop that calculates a series of fractions, ranging from 1/30 to 30/1.

 

So far I have that I need to increment the numerator counter and decrement the denominator counter. I'm just confused about how to write the code that adds the fractions together!

 

Does this look right at all? This is my first programming class so it's still confusing to me! 

 

For num = 1 To 30 Step 1

     For den = 30 To 1 Step -1

          Set sum = sum + (num/den)

 

(Once again, I'm not learning a specific language at the moment, so I'm sorry if it seems so basic haha)

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

You'll need to initialize sum to zero before the loop:

Set sum = 0
For num = 1 To 30 Step 1
     For den = 30 To 1 Step -1
          sum = sum + (num/den)

After the outer loop exits, sum will contain the sum of all fractions 1/30, 1/29, 1/28... 2/30, 2/29, ... 30/1. Keep in mind that in most programming languages, number literals without a fractional part like "30" are integers, so you'll be performing integer division instead of real number division, which won't give you the result you want. Make sure you're using real (floating-point) numbers.

Link to comment
Share on other sites

  • 0

Keep in mind that in most programming languages, number literals without a fractional part like "30" are integers, so you'll be performing integer division instead of real number division, which won't give you the result you want. Make sure you're using real (floating-point) numbers.

This is good advice, but it's not relevant for pseudocode IMO as it's an implementation detail.

Link to comment
Share on other sites

This topic is now closed to further replies.