Lets see how to generate a series i.e 0,1,2,...100,99.....1,0,1,2...... with an additional condition, that is, using only one variable.
First of all we must check the possibility of such an algorithm. Lets make an analogy (refer to figure below). A person runs from a pillar marked 0 towards another pillar marked 100, the distance between them is 100 meters. We take a variable called currentPosition to denote its position at any time. The value of currentPosition will change like 0,1,2,3,4......99,100. Now the person returns back to pillar marked 0. We can use the same variable to denote current position.
Here the direction of movement is different. But, we need not add another variable to denote direction. If the value of currentPosition is positive the person is moving from 0 to 100, if its negative the person is moving from 100 to 0. By analogy we can assume that it is possible to generate a series i.e 0,1,2,...100,99.....1,0,1,2...... using a single variable.
To provide solutions is not my aim. I want to enhance readers problem solving skills. So, try this algorithm yourself.
Also, try to generate a series i.e 0,1,2,...100,99.....1,0,1,2......199,200,199,198............2,1,0 using only one variable? Is that possible?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment