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.
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); }
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?
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...
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
Suppose we have N = 6 values from a population. These values are 4, 8, 0,...
Suppose we have N = 6 values from a population. These values are 4, 8, 0, 10, 14 and 6. Let μ and σ denote the population mean and population standard deviation of these six values, respectively. (a) What are the values of μ and σ, respectively? μ = 4.8580; σ = 7 μ = 7; σ = 4.8580 μ = 7; σ = 4.4347 μ = 7; σ = 19.6667 μ = 7; σ = 23.6 (b) What percentage...
I have the problem in bold below. I am completely confused. I am hoping that I...
I have the problem in bold below. I am completely confused. I am hoping that I will see how you did this and will be able to follow it. Please remember that I have just started this so please don't use any complex features that an absolute beginner (like me) wouldn't know. Additional information:   /*This is what everyone owes before tax and tip:     Person 1: $10     Person 2: $12     Person 3: $9     Person 4: $8     Person 5: $7     Person 6:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT