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
In modern computer systems, a byte consists of
Cerrena [4.2K]
8 smaller units, called bits :)
8 0
3 years ago
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county s
iVinArrow [24]

Answer:

purchase_amount = float(input("Enter the amount of a purchase: "))

state_sales_tax = purchase_amount * 0.05

country_sales_tax = purchase_amount * 0.025

print("The amount of the purchase is " + str(purchase_amount))

print("The state sales tax is " + str(state_sales_tax))

print("The county sales tax is " + str(country_sales_tax))

print("The total sales tax is " + str(state_sales_tax + country_sales_tax))

print("The total of the sale is " + str(purchase_amount + state_sales_tax + country_sales_tax))

Explanation:

*The code is in Python.

Ask the user to enter purchase amount

Calculate the state sales tax, multiply purchase amount by 0.05

Calculate the county sales tax, multiply purchase amount by 0.025

Calculate the total sales tax, sum the state sales tax and county sales tax

Print the amount of the purchase, state sales tax, county sales tax, total sales tax and  total of the sale

7 0
3 years ago
Robert's computer is not starting due to an error in the BIOS. Which of these chips could have malfunctioned? Select the correct
timofeeve [1]

Hi;

In the question, Robert gives the explanation that there is an error in the BIOS. A BIOS (Standing for Basic Input & Output System) is a ROM chip, and is vital for the computer to initialize devices such as RAM, the CPU, etc. If there is ever an error there, a computer simply cannot boot.

From the options given, your answer given would be C. ROM.

I hope this helps!

8 0
3 years ago
If every element of a list must beprocessed, a(n) __________is more efficient than a(n)______.
statuscvo [17]

Answer:

B - array; hash

Explanation:

Arrays store elements of the same data type in a list. Every element in the array is assigned a unique integer (starting at 0). You are able to access/process an element by using its assigned integer. Hashes are similar in the fact that they also store data. The difference is that each element is assigned an object type (instead of an integer), making it a collection of key pairs, as such you would typically not use this to process elements efficiently.

6 0
3 years ago
When a computer is the.................of an attack, it is used as an active tool to conduct the attack
BARSIC [14]

Answer:

Attack is an intentional or unintentional act that causes damage or compromises information of systems supporting it.When a computer is the subject of an attack, it is used as an active tool to conduct the attack.

On the other hand, when a computer is the entity being attacked, it is the object of an attack.

3 0
2 years ago
Other questions:
  • Set numMatches to the number of elements in userValues (having NUM_VALS elements) that equal matchValue. Ex: If matchValue = 2 a
    6·1 answer
  • PLEASE HELPP!! WILL MARK BRAINLIEST!!~~~~~
    11·1 answer
  • When you save a drawing using paint, it's automatically stored as a?
    15·1 answer
  • Samantha plans an investigation in which she will study a population of animals. Which of these answers best describes the focus
    8·1 answer
  • Write a Java program that prompts the user to enter integer values for the sides of a triangle and then displays the values and
    7·1 answer
  • Two types of formulas in Excel, what are they? A. Complex and simple, B. General and currency, C. Logical and Boolean, D. Trig a
    15·1 answer
  • Seeing an error message on the screen after you click on a link or icon may indicate that your PC doesn't have the correct _____
    15·1 answer
  • Given a PrintWriter reference variable named output that references a PrintWriter object, write a statement that writes the stri
    5·1 answer
  • Definition of powerpoint animation
    14·1 answer
  • An Excel file that contains one or more worksheets
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!