1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
djverab [1.8K]
3 years ago
5

Write a Java program that creates a two-dimensional array of type integer of size x by y (x and y must be entered by the user).

The program must fill the array with random numbers from 1 to 100. The program must prompt the user to enter a search key between 1 and 100. Then, the program must check whether the search key is available in the array and print its location.
Computers and Technology
1 answer:
spayn [35]3 years ago
4 0

Answer:

The program in Java is as follows:

import java.util.*;

public class Main{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 int x, y;

 System.out.print("x and y: ");

 x = input.nextInt(); y = input.nextInt();

 int[][] twoD_arr = new int[x][y];

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

     for(int j =0;j<y;j++){

         twoD_arr[i][j] = (int)(Math.random() * 100);      }  }

 System.out.print("Search: ");

 int numSearch = input.nextInt();

 int count = 0;

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

     for(int j =0;j<y;j++){

         if(twoD_arr[i][j] == numSearch){

             count++;

             System.out.println("Row "+(1+i)+" Column "+(j+1));          }          }    }

 if(count == 0){ System.out.print("Key does not exist");  }

}

}

Explanation:

Declare x and y

 int x, y;

Prompt user for x and y

 System.out.print("x and y: ");

Get input for x and y

 x = input.nextInt(); y = input.nextInt();

Declare the two dimensional array

 int[][] twoD_arr = new int[x][y];

Iterate through the rows

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

Iterate through the columns

     for(int j =0;j<y;j++){

Get input for each cell

         twoD_arr[i][j] = (int)(Math.random() * 100);      }  }

Prompt the user for search key

 System.out.print("Search: ");

Get input search key

 int numSearch = input.nextInt();

Initialize count to 0

 int count = 0;

Iterate through the rows

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

Iterate through the columns

     for(int j =0;j<y;j++){

Check if current element and the search key are the same.

         if(twoD_arr[i][j] == numSearch){

Increment count by 1, if they are the same

             count++;

Print the position

             System.out.println("Row "+(1+i)+" Column "+(j+1));          }          }    }

If count is 0, print key does not exist

 if(count == 0){ System.out.print("Key does not exist");  }

You might be interested in
Which task might be suitable for moving into a separate function so you can re-use it from multiple places in your code?
Fudgin [204]

Answer:

The answer is "All of these could make good functions ".

Explanation:

In the given question some information is missing, that is options which can be described as follows:

a. Ask the user to confirm an input with a yes/no answer

b. Sort some input data into an ordered list

c. All of these could make good functions

d. Calculate a complex mathematical expression

A method is a collection of ordered code and provides a generic code, that is used to execute a single, connected operation.

  • A good function is a function, which takes values from the user and it will sort all the data and store in memory, and whenever we call the function, it will give the values.
  • It is also used to calculate some complex values,
8 0
3 years ago
This operating system was used by individual computers and required users to type commands.
In-s [12.5K]

Answer:

MS-DOS is your answer

Explanation:

6 0
3 years ago
How an operating system allocates system resources in a computer​
Whitepunk [10]

Answer:

The Operating System allocates resources when a program need them. When the program terminates, the resources are de-allocated, and allocated to other programs that need them

8 0
3 years ago
Which of the following savings vehicles usually requires a high minimum balance
GuDViN [60]
A Savings Vehicle is an effective way to hold your savings. It could be a savings account. But some requires a high minimum balance such as Certificate of Deposit (CD). It is generally issued by commercial banks.  It is a time deposit too and restricts you from withdrawing funds.
7 0
3 years ago
What does the word tolerance mean in textiles?
Firdavs [7]
It means the willingness to respect or except thecustoms, beliefs, or opinions of others
4 0
3 years ago
Other questions:
  • Give two examples of desktop publishing software
    7·1 answer
  • WILL DO A BRAINLY! help pls.
    15·1 answer
  • Catherine wants to search online for fruit juices. She is fine with aerated or fresh fruit juices. Which Boolean operator will e
    12·2 answers
  • What kind of server connects a remote system through the internet to local serial ports using tcp/ip?
    9·1 answer
  • create a boolean variable called sucess that will be true if a number is between -10 and 10 inclusively python
    11·1 answer
  • I need help!
    14·1 answer
  • Write a function to output an array of ints on a single line. Funtion Should take an array and an array length and return a void
    9·1 answer
  • What is used to connect computers to the internet*Audio Ports
    15·2 answers
  • Write the difference between left-sentential form and <br> right-sentential form
    13·1 answer
  • Write any one method to delete a table in writer​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!