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
katrin2010 [14]
2 years ago
7

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 9. Then, the program must print the original array and the elements that are in even columns of the array.
Computers and Technology
1 answer:
Law Incorporation [45]2 years ago
3 0

Answer:

The java program is as follows:

import java.util.*;

public class Main{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 Random r = new Random();

 int x, y;

 x = input.nextInt();  

 y = input.nextInt();

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

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

     for(int j = 0; j < y; j++){          Array2D[i][j] = r.nextInt(9) + 1;      }

 }

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

     for(int j = 0; j < y; j++){          System.out.print(Array2D[i][j]+" ");      }

     System.out.println();

 }

 

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

     for(int j = 1; j < y; j+=2){          System.out.print(Array2D[i][j]+" ");      }

     System.out.println();

 }

}

}

Explanation:

This creates a random object

 Random r = new Random();

This declares x and y as integers

 int x, y;

This gets input for x

 x = input.nextInt();  

This gets input for y

 y = input.nextInt();

This declares the 2D array

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

The following iteration populates the array with integers 1 to 9

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

<em>      for(int j = 0; j < y; j++){          Array2D[i][j] = r.nextInt(9) + 1;      }</em>

<em>  }</em>

The following iteration prints all elements of the array

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

<em>      for(int j = 0; j < y; j++){          System.out.print(Array2D[i][j]+" ");      }</em>

<em>      System.out.println();</em>

<em>  }</em>

The following iteration prints all elements on the even column

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

<em>      for(int j = 1; j < y; j+=2){          System.out.print(Array2D[i][j]+" ");      }</em>

<em>      System.out.println();</em>

<em>  }</em>

<em />

You might be interested in
Write a program that reads numbers from scanf (keyboard) and then sums them, stopping when 0 has been entered. Construct three v
almond37 [142]

Answer:

For loop Version:

#include <stdio.h>

int main()

{

   int number, sum = 0;

   printf("Enter a positive integer: ");

   scanf("%d", &number);

   

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

       if(number == 0)

           break;

       sum += number;

       

       printf("Enter a positive integer: ");

       scanf("%d", &number);

   }

   printf("Sum is: %d", sum);

   return 0;

}

- - - - -

While Loop Version:

#include <stdio.h>

int main()

{

   int number, sum = 0, i = 0;

   printf("Enter a positive integer: ");

   scanf("%d", &number);

   

   while (number != 0) {

       sum += number;

       

       printf("Enter a positive integer: ");

       scanf("%d", &number);

   }

   printf("Sum is: %d", sum);

   return 0;

}

Do-while Loop Version:

#include <stdio.h>

int main()

{

   int number, sum = 0, i = 0;

   

   do {

       printf("Enter a positive integer: ");

       scanf("%d", &number);

       

       sum += number;

       

   } while (number != 0);

   printf("Sum is: %d", sum);

   return 0;

}

Explanation:

- Initialize the variables

- Ask the user for the numbers until the user enters 0

-  Calculate and print the sum

4 0
2 years ago
A data flow cannot go directly back to the same process it leaves. There must be at least ________ other process(es) that handle
Flauer [41]

Answer:

Answer is A. One.

Refer below.

Explanation:

A data flow cannot go directly back to the same process it leaves. There must be at least one other process that handle(s) the data flow, produce(s) some other data flow, and return(s) the original data flow to the beginning process.

3 0
3 years ago
What is an border in html
kompoz [17]

Borders can be applied to most HTML elements within the body. To make a border around an element, all you need is border-style . The values can be solid , dotted , dashed , double , groove , ridge , inset and outset . border-width sets the width of the border, most commonly using pixels as a value.

8 0
2 years ago
Which step is first in changing the proofing language of an entire document?
Genrish500 [490]
Select the whole document by pressing Ctrl+a.
7 0
3 years ago
TCO 10) Voice packets could experience a significant _____ delay in routers and switches when sharing a network with data traffi
bonufazy [111]

This delay in routers is what we called Packetization delay or also called Accumulation delay. Packetization delay is the time required for the information to pass on the wires. The data rate of the links that passes thru the wires cause the delay.

8 0
2 years ago
Other questions:
  • Write a fragment of code that reads in strings from standard input, until end-of-file and prints to standard output the largest
    10·1 answer
  • There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour).
    11·1 answer
  • 8 POINTS!!!!!!!!
    6·2 answers
  • Given :an int variable k,an int array currentMembers that has been declared and initialized ,an int variable nMembers that conta
    12·1 answer
  • Give a linear-time algorithm to sort the ratios of n given pairs of integers between 1 and n. I.e., we need to sort, within O(n)
    5·1 answer
  • ____________ refers to the computer-to-computer transmission of business data in a structured format.
    15·1 answer
  • Create a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop shou
    5·1 answer
  • In an oblique drawing, which lines can be measured accurately, if there is no foreshortening?
    10·1 answer
  • Print("Weight on Earth?")
    13·1 answer
  • What are the real-life applications of coding?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!