Question

Write a program with the below specifications/requirements: (Java Language) -Create two arrays of size 10 to...

Write a program with the below specifications/requirements: (Java Language)

-Create two arrays of size 10 to hold integers

- Initialize the two arrays with random numbers in the range {0,19}

-Print the contents of the two arrays after initialization

-Print the numbers that exist in both arrays( i.e intersection of arrays)

Homework Answers

Answer #1

import java.util.Scanner;
import java.util.Random;
import java.lang.*;

public class Print
{

public static void main(String[] args)
{
//creation of two arrays
int arr1[]=new int[10];//declaration and instantiation
int arr2[]=new int[10];//declaration and instantiation
int inter[]= new int[10]; //declaration and instantiation used for intersection elements
Random r = new Random(); //create a random object
int i,j,k,a;
//loop to generate 10 random values between 0 to 19 (both inclusive) and assign to arr1
for(i=0;i<10;i++)
arr1[i]=r.nextInt((19 - 0) + 1) + 0; //generate random integer in the range 0 to 19 and assign
//loop to generate 10 random values between 0 to 19 (both inclusive) and assign to arr2
for(i=0;i<10;i++)
arr2[i]=r.nextInt((19 - 0) + 1) + 0;//generate random integer in the range 0 to 19 and assign

//print the first array elements
System.out.println("First Array elements are");
for(i=0;i<10;i++)
System.out.print(arr1[i]+" ");
//print the second array elements
System.out.println("\nSecond Array elements are");
for(i=0;i<10;i++)
System.out.print(arr2[i]+" ");
//compute the intersection of both the arrays
//this block will only store the common elements without repetation
k=0;//k index is used for intersection array
for(i=0;i<10;i++) //loop for first array
{
for(j=0;j<10;j++) //loop for second array
{
if(arr1[i]==arr2[j]) //compare the element of array1 and array2
{
for(a=0;a<k;a++) //loop to compare whether the common element is already in
//intersection array or not
if(arr1[i]==inter[a]) //if already there then terminate the loop
break;
if(a==k) //when element is not in intersection the a will equal to k, i.e/ loop will
//completely executed
{
inter[k]=arr1[i];//assign the common element into intersection array
k++; //increase the index
}
}
}
}
//print the intersection elements
System.out.println("\nThe elements in Both arrays are");
for(a=0;a<k;a++) //loop will continue from 0 to k. where k indicates the number of elements
//in intersection array
System.out.print(inter[a]+" ");
}
}

OUTPUT

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
consider two sets of integer values stored as arrays x and y of size m and...
consider two sets of integer values stored as arrays x and y of size m and n respectively. For example, x[5]={2,68,10,90,4} and y[4]={68,3,10,22}. Write a program to print the intersection (intersection returns the elements that are common in both sets) of these two sets, i.e., the set {68, 10}. In JAVA Programming language
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
write program to compute intersection of two sorted array of integers and compute the CPU time...
write program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE Java
Write a program in Java that creates an array of doubles. The array must have size...
Write a program in Java that creates an array of doubles. The array must have size 1000. Fill this array with random numbers between 0 and 1. Then compute and print the total value of all the elements of the array, the mean, the minimum, and the maximum. Filling the array with random numbers must be done by a method that you define and implement. Computing and printing the statistics must be done by another method, which again you must...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Note: There is a white space in between the numbers. Java programming please answer ASAP before 2:30
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously...
Write the following ANNA assembly language programs. 1.HighestnumberWrite an ANNA assembly program (high.ac) that will continuously prompt the user for numbers. When the user enters a negative number, print the highest positive number entered by the user and exit the program. Print a zero if they did not enter any positive numbers. 2.DivisionWrite an ANNA assembly program (div.ac) that divides two positive numbers that come from user input and returns both the quotient and the remainder. For example, if the...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Program using java !! Create two files, file1.csv and file2.csv Write the following information in file1.csv:...
Program using java !! Create two files, file1.csv and file2.csv Write the following information in file1.csv: Apple 1.69 001 Banana 1.39 002 Write the following information in file2.csv: Apple 1.69 001 Carrot 1.09 003 Beef 6.99 004 You have two files, both of the two files have some information about products: • Read these two files, find out which products exist in both files, and then save these products in a new file. You can use the two files you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT