I need help with for loops
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: I need help with for loops

  1. #1
    Join Date
    Feb 2010
    Posts
    5

    I need help with for loops

    Ok here is the assignment my instructor gave me:

    A cancellation error occurs when you are manipulating a very large number with
    a very small number. The large number may cancel out the smaller number. For example, the
    result of 100000000.0 + 0.000000001 is equal to 100000000.0. To avoid cancellation errors and
    obtain more accurate results, carefully select the order of computation. For example, in
    computing the following series, you will obtain more accurate results by computing from right to
    left rather than from left to right:

    1+1/2+1/3+...+1/n.

    Write a C++ program that compares the results of the summation of the preceding series,
    computing from left to right and from right to left with n = 10, 100, 1000, and 10000.


    Instructions:
    Use a long double variable and accumulator for the summation.
    Avoid mixed mode expressions.
    May use a while or for loop (or both), but will need to nest the loops.
    There is no input from the user.
    Output the two summation values and the absolute value of their difference in a table
    with the value of n.

    n Left to right Right to left Abs(diff)
    10
    100
    1000
    10000

    I wrote something that worked but it is so ugly and inefficient im scared to show it to my instructor.

    I'm not asking anyone to write this for me, but i would appreciate some ideas about how you might approach this particular program. Thanks.

  2. #2
    Join Date
    Feb 2010
    Posts
    4

    Re: I need help with for loops

    Quote Originally Posted by jpetrilli View Post
    I wrote something that worked but it is so ugly and inefficient im scared to show it to my instructor.

    I'm not asking anyone to write this for me, but i would appreciate some ideas about how you might approach this particular program. Thanks.
    Hi. Most likely, we are not your instructor, so feel free to show it to us and we can help you improve your program.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: I need help with for loops

    Quote Originally Posted by jpetrilli View Post
    1+1/2+1/3+...+1/n.
    Here's a left to right summation,

    Code:
    int N=.........;
    double sum = 0.0;
    for (int i=1; i<=N; ++i) {
       sum = sum + 1.0/double(i);
    }

  4. #4
    Join Date
    Feb 2010
    Posts
    5

    Re: I need help with for loops

    I got my program to do the summations, but I cannot figure out how to do the absolute value of the two differences of the sums. Any ideas?
    Attached Files Attached Files

  5. #5
    Join Date
    Feb 2010
    Posts
    6

    Re: I need help with for loops

    Not entirely sure if this is correct, but can't you simply use the cmath library and use the abs() function?

  6. #6
    Join Date
    Feb 2010
    Posts
    5

    Re: I need help with for loops

    Yes, I used that function I just had to declare 2 accumulators I guess that was my problem.

    Does anyone know how to get the zeros on those answers to show up to 20 decimal places?

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: I need help with for loops

    That's just a matter of output formatting. Read up on the setprecision() manipulator if using cout, or the detailed format string documentation if using printf().

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center