A few things -- depending on your language you should have a "for()" construct. It's designed for iterative loops like the one you have -- you don't have to manually increment the value.
It'd be something like for (number =0; number < 10; number++). Everytime through the loop, the number would increment.
Using the for construct you can avoid the strange +1 math you're trying to do - because right now your cube has a serious problem.
Quote:
Cube
Printonethroughten()
number = 1
while number <= 10
print number
number = number + 1 * number * number
End while
Return
|
Trace your "number" value through the loop.
Number starts at 1, number is less than 10. 1 is printed. Number becomes 2. (Check your order of operations -- mult before add). Number is still less than 10. 2 is printed. Number becomes 2 + 1 * 2 * 2 = 6. Number is less than 10, and 6 is printed. In three iterations you've printed 1, 2, 6. That's hardly the cubes.
And assuming you also can't use the math libraries of your language, I'd still combine those into one function:
printNums()
{
// for the numbers 1 through 10, increment by 1
// Print the number
// Print number*number
// Print number*number*number
}
The biggest reason to go this way is that you're not printing it three seperate times. Unless the assignment explicitly requires it, you don't have to store the intermediate values. I've left it in psuedo-code because of my sneaking suspicion that this is a homework problem.