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
Question 1 of 40
Novay_Z [31]

Answer:

D. Federal Work-Study

Explanation:

8 0
3 years ago
In this assignment, you will develop a C++ program and a flowchart that calculates a cost to replace all tablets and desktop com
Alexeev081 [22]

Answer:

Explanation:

The objective of this program we are to develop is to compute a C++ program and a flowchart that calculates a cost to replace all tablets and desktop computers in a business.

C++ Program

#include<iostream>

#include<iomanip>

using namespace std;

/*

⦁ // Name:

⦁ // Date:

⦁ // Program Name:

⦁ // Description:

*/

int main()

{

const double SalesTaxRate = .075, DiscountPercent = .10, FinanceCharge = .15;

const double TABLET = 320.00, DESKTOP = 800.00;

double total = 0.0, subTotal = 0.0,avg=0.0;

int numberOfTablets, numberOfDesktop;

cout << "-------------- Welcome to Computer Selling Company ------------------ ";

cout << "Enter number of tablets: ";

cin >> numberOfTablets;

cout << "Enter number of desktops: ";

cin >> numberOfDesktop;

subTotal = TABLET * numberOfTablets + numberOfDesktop * DESKTOP;

char choice;

cout << "Paying cash or financing: (c/f): ";

cin >> choice;

cout << fixed << setprecision(2);

if (choice == 'c' || choice == 'C')

{

cout << "You have choosen paying cash." << endl;

total = subTotal - subTotal * DiscountPercent;

cout << "Discount you get $" << subTotal * DiscountPercent<<endl;

cout << "Sales Tax: $" << SalesTaxRate * total << endl;

total = total + total * SalesTaxRate;

cout << "Total computers' Cost: $" << total << endl;

avg = total / (numberOfDesktop + numberOfTablets);

cout << "Average computer cost: $ " << avg << endl;

}

else if (choice == 'f' || choice == 'F')

{

cout << "You have choosen Finance option." << endl;

total = subTotal + subTotal * FinanceCharge;

cout << "Finance Charge $" << subTotal * FinanceCharge << endl;

cout << "Sales Tax: $" << SalesTaxRate * total << endl;

total = total + total * SalesTaxRate;

cout << "Total computers' Cost: $" << total << endl;

avg = total / (numberOfDesktop + numberOfTablets);

cout << "Average computer cost: $ " << avg << endl;

}

else

{

cout << "Wrong choice.......Existing.... ";

system("pause");

}

//to hold the output screen

system("pause");

} }

OUTPUT:

The Output of the program is shown in the first data file attached below:

FLOWCHART:

See the second diagram attached for the designed flowchart.

5 0
3 years ago
How to remove duplicates from list in python?
kondaur [170]
<span> the new way of </span>removing duplicates<span> from an iterable while keeping it in the original order </span>
5 0
3 years ago
Read 2 more answers
How are gems and precious metals similar?
Ierofanga [76]
The answer would be d they are both rare and valuable minerals
7 0
2 years ago
Read 2 more answers
In this lab, you complete a prewritten C++ program that calculates an employee’s productivity bonus and prints the employee’s na
Bas_tet [7]

Answer:

The answer to this question is given below in the explanation section

Explanation:

    The formula for productivity socre is      

 productivity score = ((transaction dollar value/no of transaction)/no of shift)

Productivity Score Bonus  is:  

<=30 $50

31–69 $75

70–199 $100

>= 200 $200

........................................................................................................................................

the code is given below

........................................................................................................................................

#include <iostream>

using namespace std;

int main()

{

   string firstName;

   string lastName;

   int noOfShift;

   int noOfTransaction;

   int transactionDollarValue;

   int productivityScore;

   int bonus;

   

   cout<<"Employee’s first name: ";

   cin>>firstName;

   cout<<"Employee's last name: ";

   cin>>lastName;

   cout<<"Number of shifts:";

   cin>>noOfShift;

   cout<<" Number of transactions: ";

   cin>>noOfTransaction;

   cout<<"Transaction dollar value:";

   cin>>transactionDollarValue;

   productivityScore = (transactionDollarValue/noOfTransaction)/noOfShift;

   

   if (productivityScore <= 30)

   {

       bonus =50;

       cout<<"Employee’s first name: "<<firstName<<" "<<lastName;

       cout<<endl;

       cout<<"Employee Bonuse: $"<<bonus;

       cout<<endl;

   }

   

   else if (productivityScore >= 79 && productivityScore <=199)

   

   {

       bonus =100;

       cout<<"Employee’s first name: "<<firstName<<" "<<lastName;

       cout<<"Employee Bonuse: $"<<bonus;

   }

   

   else if (productivityScore >= 200)

   

   {

       bonus =200;

       cout<<"Employee’s first name: "<<firstName<<" "<<lastName;

       cout<<"Employee Bonuse: $"<<bonus;

   }

   

   return 0;

}

               

       

6 0
3 years ago
Other questions:
  • The operation of the waste spark Ignition system is being discussed. technician a says that the spark that occurs on the exhaust
    14·1 answer
  • In a(n) __________ situation, a wireless device is configured to appear to be a legitimate access point, enabling the operator t
    14·1 answer
  • Which option is most likely used to create many-to-many relationships in Access?
    8·1 answer
  • Timeliness is an important goal of any access control monitoring system.<br> A. True<br> B. False
    9·1 answer
  • Which one of the following statements is correct? a. Web browsers cannot function without cookies. b. Cookies are text files and
    9·1 answer
  • Bill Schultz works at a high power investment firm in Los Angeles. Bill is responsible for promoting the firm's vision and creat
    15·1 answer
  • Given a Fahrenheit value temperatureFahrenheit, write a statement that assigns temperatureCelsius with the equivalent Celsius va
    9·1 answer
  • Claire wants to include animations in her presentation slides. Which element of the presentation programs interface will have th
    11·2 answers
  • Help me with this, please. Are vacuum cleaners, Cd players, and telephones considered computers? Do they store any data or proce
    8·2 answers
  • What does the following process describe?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!