Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

8.11 (Game: nine heads and tails) Nine coins are placed in a 3-by-3 matrix with some face up and some face down. You can represent

8.11 (Game: nine heads and tails) Nine coins are placed in a 3-by-3 matrix with some face up and some face down. You can represent the state of the coins using a 3-by-3 matrix with values 0 (heads) and 1 (tails). Here are some examples: 0 0 0 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 0 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 0 0 1 1 0 0 1 1 0 Each state can also be represented using a binary number. For example, the preceding matrices correspond to the numbers 000010000 101001100 110100001 101110100 100111110 There are a total of 512 possibilities, so you can use decimal numbers 0, 1, 2, 3, . . . , and 511 to represent all states of the matrix. Write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding matrix with the characters H and T. Here is a sample run: Enter a number between 0 and 511: 7 H H H H H H T T T The user entered 7, which corresponds to 000000111. Since 0 stands for H and 1 for T, the output is correct.

This is what require from me to do

PLEASE FIX MY PROGRAM.

THIS IS THE PROGRAM:

import java.util.Scanner;

public class ss {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a number between 0 and 511: ");

int number = input.nextInt();

int matrix[][] = new int[3][3];

int index = 3;

int raw = 3;

while (number > 0) {

matrix[raw][index] = number % 2;

number = number / 2;

index--;

if (index == 0) {

raw--;

index += 3;

}

}

displayArray(matrix);

}

public static void displayArray(int array[][]) {

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

for (int j = 0; j < array[0].length; j++) {

if (array[i][j] == 0) {

System.out.print("H ");

} else {

System.out.print("T ");

}

}

System.out.println("");

}

}

}

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

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

How should you consider taxes in your financial planning?

Answered: 1 week ago