Question

In: Computer Science

I have an 8 by 8 array that has values ranging from 1-64. I am wanting...

I have an 8 by 8 array that has values ranging from 1-64. I am wanting to display the array evenly but due to some digits being single and others double, it displays lopsided. What would be the best way to get everything in the array to display in a nice even square?

Solutions

Expert Solution

CODE IN JAVA:

Square.java file:

public class Square {
   public static int digitCount(int n) {
       int count = 0 ;
       while(n!=0) {
           n = n/10 ;
           count += 1 ;
       }
       return count ;
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int arr[][] = new int[8][8];
       int count = 1 ;
       for(int i=0;i<8;i++) {
           for(int j=0;j<8;j++) {
               arr[i][j] = count ;
               count +=1 ;
           }
       }
       for(int i=0;i<8;i++) {
           for(int j=0;j<8;j++) {
               System.out.print(" "+arr[i][j]+" ");
               if(digitCount(arr[i][j])==1)
                   System.out.print(" ");
           }
           System.out.println("");
       }
   }

}
OUTPUT:


Related Solutions

(BASH) Say I have an array of strings with about 100 numbers ranging in length from...
(BASH) Say I have an array of strings with about 100 numbers ranging in length from 3-6 that represent some sort of ID. Within the array of strings, there are some duplicates numbers. What I am trying to do is represent only the top 12 numbers in the form (ID, count occurence) where its ranked by the count occurrences. So the first number would not be the ID with the most digits but it would be the one with the...
Suppose we have a die that has face values ranging from 2 to 12. This can...
Suppose we have a die that has face values ranging from 2 to 12. This can be represented as a uniform random variable. (a) Find the probability of rolling a 4. (b) Find the expected value of any roll. (c) Find the standard deviation of the roll.
Question 1 i) A binary system uses 8-bits to represent an analog value ranging from 120...
Question 1 i) A binary system uses 8-bits to represent an analog value ranging from 120 ounces to 700 ounces, determine the resolution of the system and interprete your result. ii) Determine the number of bits that would be needed for the above resolution to improve to better than 0.01 ounces per increment. Interpret your results. iii) Use the binary coded decimal (BCD) representation of integers to represent each of the following integers. 2194 4576 7865 3947 3782
I am creating a crop watering "simulator" in Python. I have the user input an array...
I am creating a crop watering "simulator" in Python. I have the user input an array and then must compare the placement of water and crops to determine if all the crops in the array are watered. The user will either input a "w" for water or "c" for crop when creating the array. A w cell can water 8 cells around it, including itself. My end result must determine if all the crops will be watered or not. How...
Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I...
JAVA JAVA JAVA JAVA, My array has 1000 int variables with random values from 1-100, I want to be able to scan and output which number appears the most and the least. int x =1000 int[] array = new array[x] for(int i = 0 ; i < x; i++){ array[i] = random.nextInt(101); }
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
1) Define and create an array of integers with the values 5, 7, 8, 9 10,...
1) Define and create an array of integers with the values 5, 7, 8, 9 10, 12 using an initializer list. Print the array. 5. 2) Given the Array below, replace the element at position 3 with the value, 99, using an assignment statement. int [] list = {88, 0, 11, 22, 55, 77}; What is the value of list.length?
1)As an employee list 8 expectations you have from your employer 2)As am employer list 8...
1)As an employee list 8 expectations you have from your employer 2)As am employer list 8 expectation you would have of your employee 3)What is considered Full Time Hours in Canada and Japan? 4)What is considered Part Time Hours in Canada and Japan? 5)What would you do if you felt something was unsafe at work? 150 words
I need to access the values in the pizzaLocations array when main.cpp is run. The values...
I need to access the values in the pizzaLocations array when main.cpp is run. The values include name, address, city, postalCode, province, latitude, longitude, priceRangeMax, priceRangeMin. I tried declaring getter functions, but that does not work. How do I access the values? Do I need to declare a function to return the values? operator[](size_t) - This should return the location with the matching index. For example if given an index of 3, you should return the location at index 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT