Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can Someone Tell me what I\'m doing wrong?! The problem I\'m trying to solve: Th

ID: 3563519 • Letter: C

Question

Can Someone Tell me what I'm doing wrong?!

The problem I'm trying to solve: There is (1) counterfeit coin out of (n) but we dont know whether the counterfeit weights more or less than the real coins.

My code works sometimes but not every time!! Real coins weight 1f

Please compile and see the problem. Sometimes it detects the counterfeit but every 3 or 4 times it gives a false counterfeit

import java.util.Random;

import java.util.Scanner;

public class main {

  

   public static void main(String[] args) {

      

       float[] bag = getInput();

      

       float cWeight = bag[1];

      

       int size = (int)bag[0];

      

           bag = fillBag(bag, size, cWeight);

      

           dAndC(bag);

          

           //dAndC3Way(bag);

   }

  

   public static float[] getInput(){

      

       float[] input = new float[2];

      

       Scanner in = new Scanner(System.in);

      

       System.out.println("Enter a Number of Elements");

      

       input[0] = in.nextFloat();

      

       System.out.println("Enter a Counterfeit Coin Weight");

      

       input[1] = in.nextFloat();

  

      

return input;

      

   }

  

private static void display(float[] bag) {

       System.out.println("================");

      

       for(int i = 0; i<bag.length; i++){

          

           System.out.println(bag[i]);

          

       }

      

   }

public static float[] fillBag(float[] bag, int size, float cWeight){

      

       bag = new float[size];

      

       float weight = 1.0f;

      

       Random randy = new Random();

      

       for(int i = 0; i < bag.length; i++){

      

           bag[i] = weight;

       }

      

           bag[randy.nextInt(size)] = cWeight;

      

       return bag;

   }

public static int dAndC(float[] bag){

      

       int index = 0;

       float leftSum = 0;

       float rightSum = 0;

      

       float[] left = new float[bag.length/2];

       float[] right = new float[bag.length/2];

      

       if( bag.length < 2 ){

          

           System.out.println("Counterfeit Coin Found "

                   + "Counterfeit Coin Weighs: " + bag[0]);

          

           return 0;

      

       }

      

       for(int i = 0; i < (bag.length /2 ); i++){

          

           left[index] = bag[i];

           leftSum +=left[index];

          

           right[(right.length - index) -1 ] = bag[ (bag.length - index) - 1];

           rightSum += right[ ( right.length - index ) - 1];

           //System.out.println("Right: "+ ((right.length - index) -1) + "bag " + ((bag.length - index) - 1));

          

                   index++;

       }

      

   if( ( rightSum / right.length ) == 1 ){

          

           return dAndC( left );

          

       } else if ( ( leftSum / left.length) == 1 ){

           return dAndC( right );

          

       } else {

      

           System.out.println("Counterfeit Not Found");

      

return 0;

      

   }

  

   }

}

Explanation / Answer

import java.util.Random;

import java.util.Scanner;

public class main {

  

   public static void main(String[] args) {

      

       float[] bag = getInput();

      

       float cWeight = bag[1];

      

       int size = (int)bag[0];

      

           bag = fillBag(bag, size, cWeight);

      

           dAndC(bag);

          

           //dAndC3Way(bag);

   }

  

   public static float[] getInput(){

      

       float[] input = new float[2];

      

       Scanner in = new Scanner(System.in);

      

       System.out.println("Enter a Number of Elements");

      

       input[0] = in.nextFloat();

      

       System.out.println("Enter a Counterfeit Coin Weight");

      

       input[1] = in.nextFloat();

  

      

return input;

      

   }

  

private static void display(float[] bag) {

       System.out.println("================");

      

       for(int i = 0; i<bag.length; i++){

          

           System.out.println(bag[i]);

          

       }

      

   }

public static float[] fillBag(float[] bag, int size, float cWeight){

      

       bag = new float[size];

      

       float weight = 1.0f;

      

       Random randy = new Random();

      

       for(int i = 0; i < bag.length; i++){

      

           bag[i] = weight;

       }

      

           bag[randy.nextInt(size)] = cWeight;

      

       return bag;

   }

public static int dAndC(float[] bag){

      

       int index = 0;

       float leftSum = 0;

       float rightSum = 0;

      

       float[] left = new float[bag.length/2];

       float[] right = new float[bag.length/2];

      

       if( bag.length < 2 ){

          

           System.out.println("Counterfeit Coin Found "

                   + "Counterfeit Coin Weighs: " + bag[0]);

          

           return 0;

      

       }

      

       for(int i = 0; i < (bag.length /2 ); i++){

          

           left[index] = bag[i];

           leftSum +=left[index];

          

           right[(right.length - index) -1 ] = bag[ (bag.length - index) - 1];

           rightSum += right[ ( right.length - index ) - 1];

           //System.out.println("Right: "+ ((right.length - index) -1) + "bag " + ((bag.length - index) - 1));

          

                   index++;

       }

      

   if( ( rightSum / right.length ) == 1 ){

          

           return dAndC( left );

          

       } else if ( ( leftSum / left.length) == 1 ){

           return dAndC( right );

          

       } else {

      

           System.out.println("Counterfeit Not Found");

      

return 0;

      

   }

  

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote