Exchange the contents: Given tow glasses marked A and B. Glass A is full of apple drink and glass B is full of grape drink. For exchanging the contents of glasses A and B represent the sate by suitable variables, and write the specification of the algorithm
Is there a diagram for this question that you can upload?
No there isn't a diagram :(
You can have 2 variables \(a\) and \(b\) to represent the overall state of the system. The variable \(a\) represents the value of the contents inside Glass A, similarly variable \(b\) represents the value of contents inside Glass B. so \(a ~=~apple\) and \(b ~=~grape\) so what you essentially need to do is swap the values of variables \(a\) and \(b\). To do this you can introduce a third variable \(t\) which could represent the contents inside a an Extra glass whcih we can use. So your algorithm could be- 1) transfer the contents of Glass A to Extra glass \(c ~=~ a\) 2) transfer the contents from Glass B to Glass A \(a ~=~b\) 3) transfer the contents from Extra glass to Glass B \(b ~=~c\)
Join our real-time social learning platform and learn together with your friends!