Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 08-22-2006, 06:15 AM   #1 (permalink)
Groovy Hipster Nerd
 
Jove's Avatar
 
Location: Michigan
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?
Jove is offline  
Old 08-22-2006, 06:46 AM   #2 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Location: Seattle, WA
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.
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel

Last edited by Jinn; 08-22-2006 at 06:51 AM..
Jinn is offline  
Old 08-22-2006, 11:51 AM   #3 (permalink)
Groovy Hipster Nerd
 
Jove's Avatar
 
Location: Michigan
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.
Jove is offline  
Old 09-19-2006, 02:26 PM   #4 (permalink)
Darth Mojo
 
mojodragon's Avatar
 
Location: Right behind you...
in java...

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

Tags
assignment, programming, simple, solve


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:08 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, 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