CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: loop problem?

  1. #1
    Join Date
    Aug 2007
    Posts
    23

    loop problem?

    A program will calculate and display the sum of all even even numbers from 2 to n, where n is a positive number inputted by the user. What should I do and what is the expression of that???
    www.coderewind.com
    Best Place to hunt for Code

  2. #2
    Join Date
    Dec 2006
    Posts
    203

    Re: loop problem?

    Sounds a lot like homework, so a hint is looking at modulus (%).
    Sincerely,

    Martin Svendsen

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: loop problem?

    You should write code for it.

    Take a pen and paper, write down the steps that need to be performed, then write the same steps in C# code.

  4. #4
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: loop problem?

    Interesting homework. You can solve it without using a loop. It is just a mathematical problem. The solution is quite simple so I believe you can solve it alone.
    Useful or not? Rate my posting. Thanks.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: loop problem?

    Quote Originally Posted by klamath23
    try this thing in your program
    It works.
    It doesn't work. Secondly, why are you trying to do this guys homework for him? Anyone with a week of programming experience in any language should be able to knock that out within 30 minutes in c#.

    Let him at least put up his own attempt.

    Also, don't fix your solution, at least make him do that.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by Mutant_Fruit
    It doesn't work. Secondly, why are you trying to do this guys homework for him? Anyone with a week of programming experience in any language should be able to knock that out within 30 minutes in c#.

    Let him at least put up his own attempt.

    Also, don't fix your solution, at least make him do that.
    How can you say it does'nt work. I have compiled it and seen the result. I should not do his homework, right i will delete for now. But if you say it does'nt work prove it to me , show me in private message. It very easy to just say it does'nt work.
    Last edited by klamath23; August 22nd, 2007 at 01:30 PM.

  7. #7
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by codeexpert123
    A program will calculate and display the sum of all even even numbers from 2 to n, where n is a positive number inputted by the user. What should I do and what is the expression of that???
    This is one way you can do it .
    Code:
    int sum = 0;
    for ( int i = 2; i <= n; i += 2 ) {
    sum += i;
    }

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: loop problem?

    Well, the problem with your original code (from what i remember, i only glanced at it when i noticed the problem) was that you coded the loop like this:

    Code:
    for(int i=0; i <= n; n++)
        DoStuff();
    So unless i misread it, it couldn't have worked. Maybe you transcribed it wrong, or maybe i misread. Either way, if that's what you did write, it was a mistake.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by Mutant_Fruit
    Well, the problem with your original code (from what i remember, i only glanced at it when i noticed the problem) was that you coded the loop like this:

    Code:
    for(int i=0; i <= n; n++)
        DoStuff();
    So unless i misread it, it couldn't have worked. Maybe you transcribed it wrong, or maybe i misread. Either way, if that's what you did write, it was a mistake.
    i didnot write the full code because it was his homework problem , i just wanted to give him hint. I will send the complete code to by private message,
    This problem is not new this is asked many times in c++, java etc.
    klamath23

  10. #10
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: loop problem?

    Because of post #7 from klamath23 the solution is out now. Therefore I can post my solution too:

    Code:
    public uint Calculate(uint n)
    {
        //ensure the number is an even number
        if (n % 2 == 1)
           n -= 1;
        //calculate sum without a loop because it is a arithmetic progression
        return Convert.ToUInt32(n*(n/4 + 0.5));
    }
    The complexness is reduced to O(1) from O(n/2) when using a loop.
    Useful or not? Rate my posting. Thanks.

  11. #11
    Join Date
    May 2007
    Posts
    1,546

    Re: loop problem?

    Quote Originally Posted by torrud
    Code:
    public uint Calculate(uint n)
    {
        if (n % 2 == 1)
           n -= 1;
    
        return Convert.ToUInt32(n*(n/4 + 0.5));
    }
    Suppose n=6. The final answer should be 6 + 4 + 2 = 12.

    In your code, if n=6, the final answer is 9. That's a bit of a problem
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  12. #12
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: loop problem?

    6*(6/4+0.5) = 6 * 2 = 12
    My hobby projects:
    www.rclsoftware.org.uk

  13. #13
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by Mutant_Fruit
    Suppose n=6. The final answer should be 6 + 4 + 2 = 12.

    In your code, if n=6, the final answer is 9. That's a bit of a problem
    Try something like this ((n *n)/4 + (n* 0.5))
    if n = 6
    then ((6*6)/4 + (6 *0.5)) = 12

  14. #14
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: loop problem?

    Yes, but isn't n*(n/4 + 0.5) exactly the same as (n *n)/4 + (n* 0.5) ?
    My hobby projects:
    www.rclsoftware.org.uk

  15. #15
    Join Date
    Aug 2007
    Posts
    39

    Re: loop problem?

    Quote Originally Posted by Zaccheus
    6*(6/4+0.5) = 6 * 2 = 12
    This works for human, this does'nt work in c# , so if you try something like

    ((n *n)/4 + ( n * 0.5)) = 12 in c#.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured