Algorithm: adding fractions using 'for' loop
-
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: 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: using namespace std; int main() { double answer = 0; for(int numerator = 1; numerator } 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.
Miningco.com Visit the source
Related Q & A:
- Do you think fractions are useful?Best solution by mathworksheetscenter.com
- Is there a way where u can send someone an email using hotmail and adding a small picture in the?Best solution by Yahoo! Answers
- Diving hard fractions?Best solution by Yahoo! Answers
- What is the difference between a for loop, a do while loop and a do loop?Best solution by wiki.answers.com
- How do you multiply fractions by whole numbers?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.