Algorithm: adding fractions using 'for' loop

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:50 PM GMT

Question

QUESTION: Hello and thank you so much for taking the time to view my question and answer according to your expertise. I am trying to design (write a program in C++ or pseudo code) for a 'For' loop that will calculate a series of numbers. The problem is: 1/30 + 2/29 + 3/28 + ... 30/1

From my understanding, using nested loops would be the best way to approach this problem. The outer loop counting from 30 to 1 while the inner loop adds the fractions. Am I correct? I really need help. I am not sure how to begin writing this program for I am a beginner. I would greatly appreciate your expertise. And, thank you in advance.


ANSWER: Hello India

Nested loops are not the way to approach this problem. With a nested loop, an inner loop runs completely for each iteration of the outer loop, so if your outer loop is 30 iterations, and your inner loop is 30 iterations, you end up with 30x30 = 900 iterations. Clearly you don't need that many iterations to add 30 fractions. What you need is a single loop that runs from 1 up to and including 30 because there are 30 fractions to add. If you look at the pattern of the fractions they are i/(30-i+1) where i goes from 1 up to and including 30. The i is your loop counter.

When you write your program, make sure you cast your i as a double, like this: (double)i/(30-i+1) Otherwise, the whole expression will be evaluated with integer arithmetic, and any fraction less than 1 will evaluate to 0. Try it with the (double) cast, and without and see the difference.

Also, make sure that your sum is a double.

I hope that helps you start your program. If you would like me to check your work, feel free to ask a follow up question.


---------- FOLLOW-UP ----------

QUESTION: Thank you so much for a speedy and thorough response. I initialized the variable on the outside of the loop and set it to 0. I added the loop counter (i) to the variable's sum that's declared outside. Here is what I have:

Int sum=0; For (double)i/(30-i+1) Sum +=I;

Now, how would I pseudo code this?

ANSWER: Hello India

To write pseudo code means to write instructions in a natural language, like English so that the instructions can be easily translated into a programming language like C. Each pseudo-code line becomes one or more C lines

What you have shown me would be an example of pseudo code although it needs a little more detail to translate it into C. Knowing how much detail to add is a matter of practice. Once you have enough practice, you won't need pseudo code at all.

Here is how I would write the pseudo code.

set the sum to 0 for each I from 1 through 30 do

  create the fraction
  add fraction to the sum

end of loop

From the pseudo code, you should be able to create C code. In this case, the program is quite simple, and each line of pseudo code translates to about one line of C code.

If your instructor has presented the C language properly to you, then you should be able to create the C program now. Look into your class notes or textbook about how to make a "for" loop. Write a main program, with a simple "for" loop and nothing else and show that to me.


---------- FOLLOW-UP ----------

QUESTION: Ok, thank you. My instructor isn't that clear in his instructions. However, I did try looking back at my textbook as you advised. This is the C language I came up with for this program:

  1. include <iostream>

using namespace std;

int main() { double answer = 0;

int numerator;

for (int denominator = 1; denominator <= 30; ++denominator) { numerator = 30 - denominator + 1;

cout << numerator << "/" << denominator;

if (denominator == 30) cout << " = " << endl; else cout << " + "; answer += ( (double)numerator ) / ( (double)denominator ); }

cout << answer << endl; return 0; }

Where I have "double answer" should I use "For (double)i/(30-i+1)" instead? I should arrive to the same result. If I still don't understand after your next response, I suppose I will try another expert online and see if a different way of explaining will help me.

Answer

Edit: Hi Again Thank you for the kind review. Please understand that the back and forth you experienced was my attempt to help you discover the answer for yourself. You will find that most people who volunteer help are not interested in just cranking out homework for others, and it has been my experience that many students don't want a complete answer handed to them. It was a pleasure to help you. Good luck with your studies.


Hi.

Your program is fine the way it is. It works and it is clearly written. If you did not need to see the printout of all the fractions, then you could have written:

  1. include <iostream>

using namespace std;

int main() {

   double answer = 0;
   for(int numerator = 1; numerator <= 30; ++numerator)
   {
       answer += (double)numerator / (30-numerator+1);
   }
   cout << answer << endl;
   return 0;

}

which uses the idea of "(double)i/(30-i+1)". The result is still the same (93.8446).

Programming is like writing. There are many ways to say the same thing. As long as your program works, and is easy for a reader to understand, then it is good. Feel free to ask others for their opinions. The more you ask, the more you learn.

Advertisement

©2024 eLuminary LLC. All rights reserved.