Question

In: Computer Science

Randomly generate 0, 1, or 2 in a 100 by 100 2d array. For each entry...

Randomly generate 0, 1, or 2 in a 100 by 100 2d array.
For each entry in the 2d array, count the number of 0’s and 1’s in the surrounding entries.
Save the result in two 2d arrays: one for the number of 0’s, one for the number of 1’s.
For example:
0   0   1
2   1   0
0   0   0

The number of 0’s for a[0][0] is 1; the number of 1’s for a[0][0] is 1;
The number of 0’s for a[0][1] is 2; the number of 1’s for a[0][1] is 2;
The number of 0’s for a[0][2] is 2; the number of 1’s for a[0][2] is 1;
The number of 0’s for a[1][0] is 4; the number of 1’s for a[1][0] is 1;
The number of 0’s for a[1][1] is 6; the number of 1’s for a[1][1] is 1;
The number of 0’s for a[1][2] is 3; the number of 1’s for a[1][2] is 2;
The number of 0’s for a[2][0] is 1; the number of 1’s for a[2][0] is 1;
The number of 0’s for a[2][1] is 3; the number of 1’s for a[2][1] is 1;
The number of 0’s for a[2][2] is 2; the number of 1’s for a[2][2] is 1;

Solutions

Expert Solution

NOTE: Since no language was mentioned I have made this program in Java. To make the program execution easy and output understandable I have taken a 3 by 3 marix. For 100 by 100 matrix just replace the initial values of n1 and n2 by 100.

import java.util.Random;
class Randomly
{
    public static void main(String[]args)
    {

        int a[]={0,1,2};// array for selecting random numbers for our matrix
        int n1, n2;// variables for number of rows and columns of our matrix
        n1=3;
        n2=3;
        int b[][]=new int[n1][n2];// initializing our matrix
        int i, k, j, c1, c2;
        Random r=new Random();
        for(i=0; i<n1; i++)
        {
            for(j=0; j<n2; j++)
            {
                k=r.nextInt(a.length);// assigning random numbers in the matrix
                b[i][j]=k;
            }
        }
        for(i=0; i<n1; i++)// nested loops to print the matrix
        {
            for(j=0; j<n2; j++)
            {
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }
        for(i=0; i<n1; i++)// nested loops to count the number of 1's and 0's surrounding an element
        {// first we are checking whether an index is available or not before checking for 1's and 0's
            for(j=0; j<n2; j++)
            {c1=0; c2=0;
                if(j-1>=0)
                {
                    if(b[i][j-1]==1)
                        c1++;
                    if(b[i][j-1]==0)
                        c2++;
                }
                if(i-1>=0&&j-1>=0)
                {
                    if(b[i-1][j-1]==1)
                        c1++;
                    if(b[i-1][j-1]==0)
                        c2++;
                }
                if(i-1>=0)
                {
                    if(b[i-1][j]==1)
                        c1++;
                    if(b[i-1][j]==0)
                        c2++;
                }
                if(i-1>=0&&j+1<n2)
                {
                    if(b[i-1][j+1]==1)
                        c1++;
                    if(b[i-1][j+1]==0)
                        c2++;
                }
                if(j+1<n2)
                {
                    if(b[i][j+1]==1)
                        c1++;
                    if(b[i][j+1]==0)
                        c2++;
                }
                if(i+1<n1&&j+1<n2)
                {
                    if(b[i+1][j+1]==1)
                        c1++;
                    if(b[i+1][j+1]==0)
                        c2++;
                }
                if(i+1<n1)
                {
                    if(b[i+1][j]==1)
                        c1++;
                    if(b[i+1][j]==0)
                        c2++;
                }
                if(i+1<n1&&j-1>=0)
                {    
                    if(b[i+1][j-1]==1)
                        c1++;
                    if(b[i+1][j-1]==0)
                        c2++;
                }

                System.out.println("The number of 0's for a["+i+"]"+"["+j+"]"+" is "+c2+" ; the number of 1's for a["+i+"]"+"["+j+"]"+" is "+c1+" ;");
            }
        }
    }//end of main method
}// end of class

Related Solutions

1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
C# - count =0; - Create a 2d array "items". string[,] items = new string[100, 4];...
C# - count =0; - Create a 2d array "items". string[,] items = new string[100, 4]; - Create a do while loop, when the user enters "0", stop the loop. - Users enter the item name, price, quantity, save this info and subtotal to array. - increase "count++" -convert price(decimal, Convert.ToDecimal() ) and quantity(int) to do multiplication for subtotal - create a for loop (with count ) to cycle through the "items", display item name, price, quantity, and subtotal. accumulate...
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Given an array A[0 … n-1], where each element of the array represent a vote in...
Given an array A[0 … n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Matrix: Ax b [2 1 0 0 0 | 100] [1 1 -1 0 -1 |...
Matrix: Ax b [2 1 0 0 0 | 100] [1 1 -1 0 -1 | 0] [-1 0 1 0 1 | 50] [0 -1 0 1 1 | 120] [0 1 1 -1 1 | 0] Problem 5 Compute the solution to the original system of equations by transforming y into x, i.e., compute x = inv(U)y. Solution: %code I have not Idea how to do this. Please HELP!
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. Need it in 10 minutes, please.
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write...
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write a program that finds the average value of the rows and the average value of the columns. Display the averages. 2. Create an array of randomly generated numbers in any range. Write a function that takes the array as an argument and returns an array that consists of only the even numbers in the original array. Use the function in a program. 3. Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT