Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using System; namespace Coins { class Program { static void Main(string[] args) { // all coins are initialised as 9973 int c1 = 9973; int

image text in transcribed

using System; namespace Coins { class Program { static void Main(string[] args) { // all coins are initialised as 9973 int c1 = 9973; int c2 = 9973; int c3 = 9973; int c4 = 9973; int c5 = 9973; int c6 = 9973; int c7 = 9973; int c8 = 9973; int c9 = 9973; int c10 = 9973; int c11 = 9973; int c12 = 9973; // default out of range initialisation of the index of cfCoin int cfCoin = -1; // the index randIndex of a random coin (one out of twelve) gets picked Random rnd = new Random(); int randIndex = rnd.Next(12)+1; // decide whether the coin with the index randIndex gets lighter (9967) or heavier (10007) int heavier = rnd.Next(2); // setting weight of the counterfeit coin if (randIndex == 1) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c1 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c1 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 2) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c2 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c2 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 3) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c3 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c3 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 4) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c4 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c4 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 5) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c5 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c5 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 6) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c6 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c6 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 7) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c7 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c7 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 8) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c8 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c8 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 9) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c9 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c9 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 10) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c10 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c10 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 11) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c11 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c11 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } if (randIndex == 12) //if the index of the counterfeit coin is 0 { if (heavier > 0) //if the counterfeit coin is heavier { c12 = 10007; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is heavier"); } else { c12 = 9967; Console.WriteLine($"The index of the counterfeit coin is: {randIndex} and the coin is lighter"); } } // do not change the code above this line // // Report the counterfeit coin on variable cfCoin. // If the counterfeit coin is c7 and the coin is heavier, your code should return cfCoin =7, // and cfCoin =-7 if this coin is lighter than all other coins. // // The only operations you are allowed to use are nested if-else-statements which use // - operators ,== // - sums of coin variables on both sides of theses operators // // The code below refers to the leftmost branch of the tree on the diagram // // if (c1+c2+c3+c4>c5+c6+c7+c8) // { // if (c1+c2+c3+c5>c4+c9+c9+c9) // { // if (c1>c2) // { // cfCoin = 1; // } // } // } // // You can also adopt the the following format // // if ((c1+c2+c3+c4>c5+c6+c7+c8) && (c1+c2+c3+c5>c4+c9+c9+c9) && (c1>c2)) // { // cfCoin = 1; // } // // Make sure you cover all branches (root to leaves) of the tree and you report the correct value! // // YOU CANNOT make any use of variables: randIndex and heavier in your code! // you need to report the index of the counterfeit coin on cfCoin // // your part of the code goes below this line // your part of the code goes above this line // do not change the code below this line if (cfCoin > 0) { Console.WriteLine($"You reported that the coin with index {cfCoin} is heavier."); } else { Console.WriteLine($"You reported that the coin with index {-cfCoin} is lighter."); } int answer=randIndex; if (heavier == 0) { answer = -randIndex; } if (answer == cfCoin) { Console.WriteLine($"Your answer is correct."); } else { Console.WriteLine($"Your answer is NOT correct"); } } } }
In this assignment, you will create a C# application that solves the 12 coins puzzle. In this challenge one is asked to find a counterfeit coin, either lighter or heavier, using a two side scales. The task is to find a coin (one in 12) using at most three comparisons. During each comparison you can compare sums of coin weights Co,c1,...,c12 using nested if-statements. Please note that the solution to the problem is non-trivial. To counterpart this a diagram suggesting a possible solution is given below. Please note that letter G refers to any coin which is already identified as a good (different to counterfeit) one. 1234 5678 12-coin 1234 5678 tree 13 test nodes 3 NPS 1235 4GGG 91011 GGG 1235 4GGG 1267 9 10 12 G 9 10 4G 67 12 SA 1H 3H2H 7L BL 6L 4H 5L NP 9H 11H 10H 12H NP 12L 10L 11L 9L NP 5H 4L 6H8H7H 2L 3L 1L the counterteit coin via an integer variable cjcoin. in particular, it the number of the counterteit coin is 7, your code should return 7, if this coin is heavier and -7 if this coin is lighter than all other coins. I recommend you spend time thinking (or reading about Bubble Sort) about the proper strategy before you begin coding your solution. The more thought you put into your work before you code, the better In this assignment, you will create a C# application that solves the 12 coins puzzle. In this challenge one is asked to find a counterfeit coin, either lighter or heavier, using a two side scales. The task is to find a coin (one in 12) using at most three comparisons. During each comparison you can compare sums of coin weights Co,c1,...,c12 using nested if-statements. Please note that the solution to the problem is non-trivial. To counterpart this a diagram suggesting a possible solution is given below. Please note that letter G refers to any coin which is already identified as a good (different to counterfeit) one. 1234 5678 12-coin 1234 5678 tree 13 test nodes 3 NPS 1235 4GGG 91011 GGG 1235 4GGG 1267 9 10 12 G 9 10 4G 67 12 SA 1H 3H2H 7L BL 6L 4H 5L NP 9H 11H 10H 12H NP 12L 10L 11L 9L NP 5H 4L 6H8H7H 2L 3L 1L the counterteit coin via an integer variable cjcoin. in particular, it the number of the counterteit coin is 7, your code should return 7, if this coin is heavier and -7 if this coin is lighter than all other coins. I recommend you spend time thinking (or reading about Bubble Sort) about the proper strategy before you begin coding your solution. The more thought you put into your work before you code, the better

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions