CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2009
    Posts
    12

    Is this even possible? (don't need solution, just a yes or no)

    Whats up guys. One of my questions on homework is as follows:

    Write a value-returning function that contains a for loop that will find the sum of the integers from start to finish (start and finish are int variables passed to the function). (Example if start is 9 and finish is 12, then the function would return the sum of 9 + 10 + 11 + 12)

    I don't see how this is possible using a for loop. Don't for loops only deal with counts and can't the testExpression only be an inequlaity? Ex. for(i=1, i<=10, i++)

    Is it possible to do this with variables (start and finish) like the teacher said or should I just do a while loop? I'm a confused noob.

    Thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Is this even possible? (don't need solution, just a yes or no)

    What if i's initial value was start and looped until it was greater than finish?

    Just saying...

    The condition can be anything that evaluates to true or false.

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Is this even possible? (don't need solution, just a yes or no)

    There's nothing wrong with the question. It is achievable using a for-loop. You just need to count from start to end.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  4. #4
    Join Date
    Nov 2009
    Posts
    12

    Re: Is this even possible? (don't need solution, just a yes or no)

    like

    for (i = start, i <= end, i++)
    int total += i;

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Is this even possible? (don't need solution, just a yes or no)

    Quote Originally Posted by c++guy2 View Post
    like

    for (i = start, i <= end, i++)
    int total += i;
    Two things:
    1. OP wanted "just a yes or no".
    2. Your code repeatedly defines uninitialized int variable total that gets incremented by i and goes out of scope rigth away.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Is this even possible? (don't need solution, just a yes or no)

    Syntax in a code snippet you have provided is wrong. I do not know if it was intentional, but your code snippet will not even compile.
    If this was intended as a pseudo-code, you should indicate this.
    Since you have already revealed a code it should be written as something similar to this:
    Code:
     	//start and end are delivered as function srguments.  
    	int total = 0;
    
    	for(int iIndx = start; iIndx <= end; ++iIndx)
    	{
    		int total += iIndx; 
    	}
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Is this even possible? (don't need solution, just a yes or no)

    Similar, but not identical or it still won't work.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Is this even possible? (don't need solution, just a yes or no)

    Quote Originally Posted by JohnCz View Post
    Syntax in a code snippet you have provided is wrong. I do not know if it was intentional, but your code snippet will not even compile.
    If this was intended as a pseudo-code, you should indicate this.
    Since you have already revealed a code it should be written as something similar to this:]
    Why are you doing this guy's homework for him?

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Is this even possible? (don't need solution, just a yes or no)

    Quote Originally Posted by c++guy2 View Post
    like

    for (i = start, i <= end, i++)
    int total += i;
    That's pretty far off and even if it would work, the policy of this board is to not do people's homework for them.

  10. #10
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Is this even possible? (don't need solution, just a yes or no)

    Quote Originally Posted by GCDEF View Post
    Why are you doing this guy's homework for him?
    I did not.
    Quote Originally Posted by Me earlier View Post
    Since you have already revealed a code . . .
    Well, code was already posted by c++guy2. Since it is wrong, I decided to show the right way of doing it. Why confuse aea1189 even more?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  11. #11
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Is this even possible? (don't need solution, just a yes or no)

    Yes.

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