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
Technician A says that it's a good idea to perform a test drive before attempting repairs. Technician B says that it's a good id
Alex Ar [27]

Answer:

Technician A.

Explanation:

Technician A is correct because a technician can detect the fault more accurately as compared to the customer.

3 1
2 years ago
Read 2 more answers
Why did Hunter gatherers moved into the<br>America's?​
GrogVix [38]

Answer:

Explanation:

Hunter-gatherers are humans that get the food and living collecting wild plants and pursuing wild animals, this was the first human's adaptation, this people arrive to America from the Bering Strait from Asia Eurasia, they were following some prehistoric mammoths, and using primitive boats arriving at Pacific coast to South America.

8 0
3 years ago
-The following type of Access form allows to see multiple records in an Excel-like arrangement of data fields:
Damm [24]
Data sheet

Data sheet is what an excel sheet is so it’s the same thing
4 0
3 years ago
Define Class in C++. Briefly discuss.
lesya [120]

Answer:

Class are the collection of variable and member function.

Class are the blueprint of an object.

Explanation:

Following are the points regarding class in c++

1.In C++ class is an user defined datatype..

2.Classes are the collection of variable and function in c++.

3.To access the property of class we can create object of that class

4.We can use following syntax to declared any class in c++

class classname

{

accessmodifier:

//statement and function

};

main()

{

classname objectname;

}

implementation of class in c++

#include<iostream>

class test // class declaration

{

public: // access modifier

void fun3() // Method definition

{

cout<<" hello :";

}

};

void main() // main function

{

test ob;// creating object

ob.fun3(); // calling function

}

In this program we declared a class "test " in class test. We giving public access modifier .The public access modifier define that variable and function are accessible outside the class and in main method we create the object ob which call the function fun3().

Output:hello :

3 0
3 years ago
*hacked*
galben [10]

Hey Litz06,

Sorry that happened to you.

Try changing your password to something complicated so it’s more secure.

5 0
3 years ago
Other questions:
  • What effects will the different types of lighting produce on mountains?
    15·1 answer
  • What are the problems with security when working on a Web Page?
    13·2 answers
  • How do i show all emails from same sender in outlook
    13·2 answers
  • During a night flight, you observe a steady red light and a flashing red light ahead and at the same altitude. What is the gener
    11·1 answer
  • For almost all networks today, including the internet, the communication protocol used is called ____.
    15·1 answer
  • Careers in information technology deal with
    12·2 answers
  • How many passes will it take to find the four in this list? 4, 5, 6, 7, 8, 9, 10 1 2 3 4
    12·2 answers
  • Hello everyone! can anybody help me? i need help with computing.
    10·1 answer
  • What is the decimal number 86 when written as a
    9·1 answer
  • Matt uploads a malware sample to a third-party malware scanning site that uses multiple antimalware and antivirus engines to sca
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!