Question: How do I change this into a 2D Array. Objectives for this practice lab: Populating a single array Checking for bounds of an array Using

How do I change this into a 2D Array.

Objectives for this practice lab:

Populating a single array

Checking for bounds of an array

Using a multi dimensional array

Create a program (CoinFlip.java) that:

Creates 2 int arrays to store coin flip data from two different coins

Simulate a coin flip (0= heads, 1 = tails) 1000 times.( you can use Math.Random() or a Random generator)

gen.nextInt(2);

(int)(Math.random()*2);

Evaluate if the first flip of each coin through the 1000th flip to determine if:

Coin1 flip 1 vs Coin2 flip 1

Coin1 flip 2 vs Coin2 flip 2

Coin1 flip 1000 vs Coin2 flip 1000

Both equals heads

Both equals tails

Have different values

Print out the total counts of the above three

THEN

Evaluate the first flip of the first coin to the last flip of the second and repeat until all flips have been covered, i.e.:

Coin1 flip 1 vs Coin2 flip 1000

Coin1 flip 2 vs Coin2 flip 999

Coin1 flip 1000 vs Coin2 flip 1

Both equals heads

Both equals tails

Have different values

Print out the total counts of the above three

You're not done yet!

Now repeat what you did above but use a 2-D array instead of 2 two separate arrays!

This is what I have so far. Thanks for your help.

import java.util.*;

public class CoinFlip {

public static void main(String[] args) {

/*

* Creating integer array of size 1000 to

* store random flips of coin1

*/

int coin1[]=new int[1000];

for(int i=0;i<1000;i++){

/*

* In each loop random number i.e 0 or 1 is created and stored

* in array coin1[i] at i.

*/

Random rn1 = new Random();

coin1[i] = rn1.nextInt(2);

}

/*

* Creating integer array of size 1000 to

* store random flips of coin2*

*/

int coin2[]=new int[1000];

for(int i=0;i<1000;i++){

/*

* In each loop random number i.e 0 or 1 is created and stored

* in array coin2[i] at i.

*/

Random rn1 = new Random();

coin2[i] = rn1.nextInt(2);

}

/*

* Now as per the problem statement, both arrays i.e

* coin1 and coin2 are compared at each index starting

* from i=0 to i=999(1 to 1000 flips of the coin).

*

* Three integer variables are created to store total

* count of both heads, both tails and both different.

*/

// Note heads==0 and tails==1

int bothHeads=0,bothTails=0,bothDifferent=0;

/*First comparing in same order*/

for(int i=0;i<1000;i++){

if(coin1[i]!=coin2[i]){

/*If both are different, then increment bothDifferent*/

bothDifferent++;

}else{

/*Both same then check if current is heads or tails*/

if(coin1[i]==0){

/*If both are heads*/

bothHeads++;

}else{

/*If both are heads*/

bothTails++;

}

}

}

System.out.println("===============Using Two Arrays==============");

System.out.println();

System.out.println("Results of comparing coins in same order:");

System.out.println("Total number of times both coins are heads on same flip: "+bothHeads);

System.out.println("Total number of times both coins are tails on same flip: "+bothTails);

System.out.println("Total number of times both coins are differant on same flip: "+bothDifferent);

/*

* Now comparing in opposite direction as given in question.

* Update all the three variables as setting them to again 0.

*/

bothHeads=0;

bothTails=0;

bothDifferent=0;

int j=999;

for(int i=0;i<1000;i++){

/*

* Here I am using j as index for array coin2 to access

* values in reverse order. j is initialized to j=999,

* i.e the index at which last element of the array coin2 is stored.

*/

if(coin1[i]!=coin2[j]){

/*If both are different, then increment bothDifferent*/

bothDifferent++;

}else{

/*Both same then check if current is heads or tails*/

if(coin1[i]==0){

/*If both are heads*/

bothHeads++;

}else{

/*If both are heads*/

bothTails++;

}

}

/*

* In every iteration j is decremented so that we get pairing of

* i=0 vs j=999, i=1 vs j=998 and so on...

*/

j--;

}

System.out.println();

System.out.println("Results of comparing coins in reverse order::");

System.out.println("Total number of both equal Heads at same position is "+bothHeads);

System.out.println("Total number of both equal Tails at same position is "+bothTails);

System.out.println("Total number of both Different at same position is "+bothDifferent);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!