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
SSSSS [86.1K]
2 years ago
13

Problem 2 Write a method that (a) returns an int[] , (b) takes in two sorted arrays (in descending order) and (c) returns an int

[] with sorted values (in descending order) from the two sorted arrays. The method will also print out the number of compares done. int[] a = new int[]{18, 16, 15, 6, 2}; int[] b = new int[]{13, 12, 11, 7, 1}; public int[] sortArraysDes(int[] a, int[] b){ //your code here } The int[] returned should have all the integers from int[]a and int[]b in descending order.
Computers and Technology
1 answer:
Galina-37 [17]2 years ago
4 0

Answer:

Following are the Program in the Java Programming Language.

//define class

public class Main

{

//define function to merge and sort array

public static int[] sort_Array(int[] a, int[] b)

{

//set integer variable and initialize values

int x = 0, y = 0, k = 0, num = 0;

int n = a.length;

int n1 = b.length;

//set integer type array

int[] ar = new int[n+n1];

//set while loop

while (x<n && y <n1)

{

num++;

num++;

//set if conditional statement

if (a[x] > b[y])

ar[k++] = a[x++];

else

ar[k++] = b[y++];

num++;

}

//set the while loop

while (x < n)

{

num++;

ar[k++] = a[x++];

}

//set the while loop

while (y < n1)

{

num++;

ar[k++] = b[y++];

}

//print the total compares done

System.out.println("Number of compares done: " + n);

//return array

return ar;

}

//define main method

public static void main(String[] args)

{

//declaration and the initialization of array

int[] a = new int[]{18,16, 15, 6, 2};

int[] b = new int[]{13, 12, 11, 7, 1};

int[] ar;

//call function

ar = sort_Array(a, b);

//print message

System.out.println("\nThe resultant array is: \n");

//set for loop to print array

for(int x=0; x<ar.length; x++)

{

System.out.print(ar[x] + " ");

}

}

}

<u>Output</u>:

Number of compares done: 5

The resultant array is:

18 16 15 13 12 11 7 6 2 1

Explanation:

Here, we define a class "Main" inside the class, define a function "sort_Array()"and pass two integer type array argument "a" and "b".

  • Set four integer type variables "x", "y", "k", and "num" and initialize them to 0.
  • Set two integer type array "n", "n1" and initialize them the length of the array then, set integer type array variable "ar".
  • Set the while loop then, set if conditional statement then, agin set while loop to sort array in descending order.
  • Then, print the number of compares and return the value of the variable "ar".

Finally, define the main method inside it, we set two integer type array variable and initialize elements in them then, set the for loop to print the sorted elements of the array.

You might be interested in
Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(l
kifflom [539]

Answer:

The java program for the given scenario is shown below.

import java.util.*;

import java.lang.*;

public class Test

{

   //variables to hold the number, digits and sum of the digits

   //variable to hold number is assigned any random value

   static long num=123;

   static int sum=0;

   static int digit;

   static int s;

   //method to add digits of a number

   public static int sumDigits(long n)

   { do

       {

           digit=(int) (n%10);

           sum=sum+digit;

           n=n/10;

       }while(n>0);

       return sum;

   }

public static void main(String[] args) {

    s = sumDigits(num);

 System.out.println("The sum of the digits of "+num+ " is "+s);  }

}

OUTPUT

The sum of the digits of 123 is 6

Explanation:

1. The variables to hold the number is declared as long and initialized.

2. The variables to store the digits of the number and the sum of the digits are declared as integer. The variable, sum, is initialized to 0.

3. The method, sumDigits(), is defined which takes a long parameter and returns an integer value. The method takes the number as a parameter and returns the sum of its digits.

4. Inside method, sumDigits(), inside the do-while loop, the sum of the digits of the parameter is computed.

5. Inside main(), the method, sumDigits(), is called. The integer value returned by this method is stored in another integer variable, s.

6. The sum of the digits is then displayed to the console.

7. All the variables are declared outside main() and at the class level and hence declared static. The method, sumDigits(), is also declared static since it is to be called inside main().

8. In java, the whole code is written inside a class since java is a purely object-oriented language.

9. In this program, object of the class is not created since only a single class is involved having main() method.

10. The program can be tested for any value of the variable, num.

11. The file is saved as Test.java, where Test is the name of the class having main() method.

7 0
3 years ago
Directions. Read and analyze the sentences below. There are words or phrases to be corrected. Rewrite it on the blank accrording
devlian [24]

Full question attached

Answer and Explanation:

1. There’s need to ask for help and advice from people who have the

knowledge and skills in making a simple electrical gadget.

2. Adapter is used to plug appliances that are far from the main power source.

3. Do not touch the metal pipes while you are doing electrical repairs.

4. Shut off the power at the main switch or circuit breaker and unplug the equipment when working on electricity.

5. Pliers are tools used to grip, pull or cut electric wires.

From the above, we have corrected some of the errors that could be made in everyday home appliances or home electricity. In number 4, it is also worthy of note that shutting off power at the main switch will only require emergency situations otherwise we need only shut off the one for the problem circuit and leave the rest.

5 0
3 years ago
NEED FIVE QUESTIONS ANSWERED!!! take your time~
DochEvi [55]
  1. A
  2. C
  3. C
  4. B
  5. A

I'M NOT 100% SURE i hope this helps you

4 0
3 years ago
Whats the name of the windows 10 nucleus
stepan [7]

Answer:

motheboard

Explanation:

7 0
3 years ago
Diana is running a successful remarketing campaign. She wants to expand her reach with other targeting options. While creating a
mart [117]

Answer:

B: Audience interest around different topics.

Explanation:

When using Google Ads, and the main aim is to create a campaign for a particular marketing activity, there are several options that makes Google Ads the ideal application for this purpose. However, choosing similar audiences under the targeting option is simply telling google to find users or audiences that are interested in the different topics, but are in one way or the other connected to your marketing activity or campaign.

8 0
3 years ago
Other questions:
  • Rachel uses a word processor to write short stories. However, most of her time is spent toying with the keys, rather than in wri
    13·2 answers
  • In Word, tables can be styled much like text can.<br> True<br> False
    6·1 answer
  • What are an administrator's choices for managing file permissions on a drive formatted as fat32?
    10·1 answer
  • Any part of the computer that you can touch is called ________.
    6·1 answer
  • What should be used to keep a tablet dry?
    13·1 answer
  • Computer privacy typically occurs when which of the following is violated?
    6·1 answer
  • Mobile computing is growing in importance each and every day, and the IT manager must take that into account. Do some web resear
    14·1 answer
  • Why would a programmer use a flow chart? (Edge2020 Coding Critical Thinking Questions)
    9·1 answer
  • The purpose of Appetizers on the menu​
    6·2 answers
  • A homeowner uses a smart assistant to set the house alarm, get packages delivery updates, and set time on the outdoor lights. Wh
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!