Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   Solve this simple programming assignment (https://thetfp.com/tfp/tilted-technology/107790-solve-simple-programming-assignment.html)

Jove 08-22-2006 06:15 AM

Solve this simple programming assignment
 
Alright, this might be a simple programming solution for most, but I would like to know what you would do the following question:

Design the logic for a module that would print number 1 through 10 along with its square and cube.

Example:

Printonethroughten()
Number = 1
While number <= 10
Print number
Number = number + 1
End while
Return

This was the example given and I assumed, one would have to do this to get the square

Printonethroughten()
number = 1
while number <= 10
print number
number = number + 1 * number
End while
Return

Cube

Printonethroughten()
number = 1
while number <= 10
print number
number = number + 1 * number * number
End while
Return

---
Was the above solution the correct answers or incorrect answers?

Jinn 08-22-2006 06:46 AM

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. :)

Jove 08-22-2006 11:51 AM

You are correct, it is a homework problem. I was helping out a friend and came to the same conclusion as you did, but I thought it was incorrect, so I put down my first answer, which was incorrect. Thanks for the help.

mojodragon 09-19-2006 02:26 PM

in java...

for (int i = 1; i < 11; i++) {
System.out.println("Number: " + i + ",Square: " + (i * i) + ",Cube: " + (i*i*i));
}


All times are GMT -8. The time now is 10:37 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76