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
Five uses of the start button
Damm [24]

Starting something

Possibly turning it off

Making something work

Fixing the Start Button

Beginning something

5 0
3 years ago
8.5 Edhesive Code Practice
Lynna [10]

Answer:

Check explanation

Explanation:

#Print original quote

twainQuotes = ["I have never let my schooling interfere with my education.",

"Get your facts first, and then you can distort them as much as you please.",

"If you tell the truth, you don't have to remember anything.",

"The secret of getting ahead is getting started.",

"Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]

print(twainQuotes)

#Sort and print

twainQuotes.sort()

print(twainQuotes)

#Insertion of quote, sort, then print

twainQuotes.extend(["Courage is resistance to fear, mastery of fear, not absence of fear."])

twainQuotes.sort()

print(twainQuotes)

mark as brainliest pls hehe

8 0
3 years ago
What is the use of an NDP?
Makovka662 [10]

Answer:

It defines five ICMPv6 packet types for router solicitation,router advertisement,neighbor solicitation,neighbor advertisement,and network redirects.

Explanation:

8 0
3 years ago
Can anyone help me out with my photography questions?
Oliga [24]
Verifying, The answer above me is correct
6 0
2 years ago
An attacker has hidden an NFC reader behind an NFC-based kiosk in an airport. The attacker uses the device to capture NFC data i
tatiyna

Answer:

Nfc relay attack

Explanation:

The NFC relay attack occurs when data is extracted by an attacker who puts himself in the middle of two parties having a communication with each other. As the message gets to a party, the attacker gets it and modifies it to their taste then passes it on.

This attacker in this question is extracting data by establishing communication between end user devices and reader in the kiosk

5 0
3 years ago
Other questions:
  • How can we get the number of records or rows in a table using MySQL? *
    8·1 answer
  • Michael is trying to remember his friend’s new cell phone number. He repeats it in his mind until he gets the new number entered
    8·2 answers
  • Keith is researching a controversial topic and wants to use a website he found as an information source. Which of the following
    11·2 answers
  • What is not true of credit scores?
    11·1 answer
  • Why is it important to have at least one backup stored off-site?
    8·1 answer
  • Write an algorithm that receives a number from the user (you can store the number in a variable called N). Then the algorithm sh
    8·1 answer
  • You have three users who travel to four branch offices often and need to log on to the RODCs at these offices. The branch office
    10·1 answer
  • Write a program that prompts the user to enter a Social Security number in the format ddd-dd-dddd, where d is a digit. The progr
    12·1 answer
  • The Monte Carlo method is commonly used to approximate areas or volumes of shapes. In this problem, we will use Monte Carlo to f
    12·1 answer
  • Before radio, many minority musicians were severely limited by the prejudice of the era. true o rfalse
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!