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
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.
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.
Re: Is this even possible? (don't need solution, just a yes or no)
like
for (i = start, i <= end, i++)
int total += i;
Re: Is this even possible? (don't need solution, just a yes or no)
Quote:
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.
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;
}
Re: Is this even possible? (don't need solution, just a yes or no)
Similar, but not identical or it still won't work.
Re: Is this even possible? (don't need solution, just a yes or no)
Quote:
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?
Re: Is this even possible? (don't need solution, just a yes or no)
Quote:
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.
Re: Is this even possible? (don't need solution, just a yes or no)
Quote:
Originally Posted by
GCDEF
Why are you doing this guy's homework for him?
I did not.
Quote:
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?
Re: Is this even possible? (don't need solution, just a yes or no)