-
November 23rd, 2009, 07:33 PM
#1
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
-
November 23rd, 2009, 07:55 PM
#2
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.
-
November 23rd, 2009, 08:23 PM
#3
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.
-
November 23rd, 2009, 10:13 PM
#4
Re: Is this even possible? (don't need solution, just a yes or no)
like
for (i = start, i <= end, i++)
int total += i;
-
November 23rd, 2009, 11:50 PM
#5
Re: Is this even possible? (don't need solution, just a yes or no)
Originally Posted by c++guy2
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...
-
November 24th, 2009, 12:16 AM
#6
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.
-
November 24th, 2009, 08:05 AM
#7
Re: Is this even possible? (don't need solution, just a yes or no)
Similar, but not identical or it still won't work.
-
November 24th, 2009, 08:10 AM
#8
Re: Is this even possible? (don't need solution, just a yes or no)
Originally Posted by JohnCz
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?
-
November 24th, 2009, 08:11 AM
#9
Re: Is this even possible? (don't need solution, just a yes or no)
Originally Posted by c++guy2
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.
-
November 24th, 2009, 10:06 AM
#10
Re: Is this even possible? (don't need solution, just a yes or no)
Originally Posted by GCDEF
Why are you doing this guy's homework for him?
I did not.
Originally Posted by Me earlier
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.
-
November 24th, 2009, 11:00 AM
#11
Re: Is this even possible? (don't need solution, just a yes or no)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|