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
lara31 [8.8K]
3 years ago
9

For this problem, you may not look at any other code or pseudo-code (even if it is on the internet), other than what is on our w

ebsite or in our book. You may discuss general ideas with other people. Assume A[1. . . n] is a heap, except that the element at index i might be too large. For the following parts, you should create a method that inputs A, n, and i, and makes A into a heap.
Engineering
1 answer:
sergiy2304 [10]3 years ago
8 0

Answer:

(a)

(i) pseudo code :-

current = i

// assuming parent of root is -1

while A[parent] < A[current] && parent != -1 do,

if A[parent] < A[current] // if current element is bigger than parent then shift it up

swap(A[current],A[parent])

current = parent

(ii) In heap we create a complete binary tree which has height of log(n). In shift up we will take maximum steps equal to the height of tree so number of comparison will be in term of O(log(n))

(b)

(i) There are two cases while comparing with grandparent. If grandparent is less than current node then surely parent node also will be less than current node so swap current node with parent and then swap parent node with grandparent.

If above condition is not true then we will check for parent node and if it is less than current node then swap these.

pseudo code :-

current = i

// assuming parent of root is -1

parent is parent node of current node

while A[parent] < A[current] && parent != -1 do,

if A[grandparent] < A[current] // if current element is bigger than parent then shift it up

swap(A[current],A[parent])

swap(A[grandparent],A[parent])

current = grandparent

else if A[parent] < A[current]

swap(A[parent],A[current])

current = parent

(ii) Here we are skipping the one level so max we can make our comparison half from last approach, that would be (height/2)

so order would be log(n)/2

(iii) C++ code :-

#include<bits/stdc++.h>

using namespace std;

// function to return index of parent node

int parent(int i)

{

if(i == 0)

return -1;

return (i-1)/2;

}

// function to return index of grandparent node

int grandparent(int i)

{

int p = parent(i);

if(p == -1)

return -1;

else

return parent(p);

}

void shift_up(int A[], int n, int ind)

{

int curr = ind-1; // because array is 0-indexed

while(parent(curr) != -1 && A[parent(curr)] < A[curr])

{

int g = grandparent(curr);

int p = parent(curr);

if(g != -1 && A[g] < A[curr])

{

swap(A[curr],A[p]);

swap(A[p],A[g]);

curr = g;

}

else if(A[p] < A[curr])

{

swap(A[p],A[curr]);

curr = p;

}

}

}

int main()

{

int n;

cout<<"enter the number of elements :-\n";

cin>>n;

int A[n];

cout<<"enter the elements of array :-\n";

for(int i=0;i<n;i++)

cin>>A[i];

int ind;

cout<<"enter the index value :- \n";

cin>>ind;

shift_up(A,n,ind);

cout<<"array after shift up :-\n";

for(int i=0;i<n;i++)

cout<<A[i]<<" ";

cout<<endl;

}

Explanation:

You might be interested in
Four kilograms of carbon monoxide (CO) is contained in a rigid tank with a volume of 1 m3. The tank is fitted with a paddle whee
Juli2301 [7.4K]

Answer:

a) 1 m^3/Kg  

b) 504 kJ

c) 514 kJ

Explanation:

<u>Given  </u>

-The mass of C_o2 = 1 kg  

-The volume of the tank V_tank = 1 m^3  

-The added energy E = 14 W  

-The time of adding energy t = 10 s  

-The increase in specific internal energy Δu = +10 kJ/kg  

-The change in kinetic energy ΔKE = 0 and The change in potential energy  

ΔPE =0  

<u>Required  </u>

(a)Specific volume at the final state v_2

(b)The energy transferred by the work W in kJ.  

(c)The energy transferred by the heat transfer W in kJ and the direction of  

the heat transfer.  

Assumption  

-Quasi-equilibrium process.  

<u>Solution</u>  

(a) The volume and the mass doesn't change then, the specific volume is constant.

 v= V_tank/m ---> 1/1= 1 m^3/Kg  

(b) The added work is defined by.  

W =E * t --->  14 x 10 x 3600 x 10^-3 = 504 kJ  

(c) From the first law of thermodynamics.  

Q - W = m * Δu

Q = (m * Δu) + W--> (1 x 10) + 504 = 514 kJ

The heat have (+) sign the n it is added to the system.

7 0
3 years ago
How many hours should it take an articulated wheel loader equipped with a 4-yd^3 bucket to load 3000 yd^3 of gravel (average mat
densk [106]

Answer:

17 hours 15 minutes

Explanation:

See attached picture.

4 0
3 years ago
The electric motor exerts a torque of 800 N·m on the steel shaft ABCD when it is rotating at a constant speed. Design specificat
kodGreya [7K]

Answer:

d= 4.079m ≈ 4.1m

Explanation:

calculate the shaft diameter from the torque,    \frac{τ}{r} = \frac{T}{J} = \frac{C . ∅}{l}

Where, τ = Torsional stress induced at the outer surface of the shaft (Maximum Shear stress).

r = Radius of the shaft.

T = Twisting Moment or Torque.

J = Polar moment of inertia.

C = Modulus of rigidity for the shaft material.

l = Length of the shaft.

θ = Angle of twist in radians on a length.  

Maximum Torque, ζ= τ ×  \frac{ π}{16} × d³

τ= 60 MPa

ζ= 800 N·m

800 = 60 ×  \frac{ π}{16} × d³

800= 11.78 ×  d³

d³= 800 ÷ 11.78

d³= 67.9

d= \sqrt[3]{} 67.9

d= 4.079m ≈ 4.1m

3 0
3 years ago
Read 2 more answers
PELASEE HELPPP WITH MARK BRAINLIST!!!! You are stopped at a red light, and a long string of cars is crossing in front of you. Wh
choli [55]

Answer:

1st one.

Explanation:

I think that because they the one who is going to get a ticket and also you will not gonna get in a car accident.

7 0
3 years ago
Read 2 more answers
A vector AP is rotated about the Z-axis by 60 degrees and is subsequently rotated about X-axis by 30 degrees. Give the rotation
Musya8 [376]

Answer:

R = \left[\begin{array}{ccc}1&0&0\\0&cos30&-sin30\\0&sin30&cos30\end{array}\right]\left[\begin{array}{ccc}cos 60&-sin60&0\\sin60&cos60&60\0&0&1\end{array}\right]

Explanation:

The mappings always involve a translation and a rotation of the matrix. Therefore, the rotation matrix will be given by:

Let \theta and \alpha be the the angles 60⁰ and 30⁰ respectively

that is \theta = 60⁰ and

\alpha = 30⁰

The matrix is given by the following expression:

\left[\begin{array}{ccc}1&0&0\\0&cos30&-sin30\\0&sin30&cos30\end{array}\right]\left[\begin{array}{ccc}cos 60&-sin60&0\\sin60&cos60&60\0&0&1\end{array}\right]

The angles can be evaluated and left in the surd form.

4 0
3 years ago
Other questions:
  • In the contemporary approach to control systems, benefits of continuous monitoring include which one of the following? Multiple
    9·1 answer
  • Power is a fundamental dimension. a) True b) False
    15·1 answer
  • # 17
    13·2 answers
  • Why might construction crews want to install pipes before the foundation is poured
    8·1 answer
  • The conditions at the beginning of compression in an Otto engine operating on hot-air standard with k=1.35 and 101.325 kPa, 0.05
    10·1 answer
  • melinda is using a rectangular brass bar in a sculpture she is creating. the brass bar has a length that is 4 more than 3 times
    11·2 answers
  • Any help is appreciated.
    7·1 answer
  • What differentiates the master builder approach prior to the Renaissance from later approaches? Projects do not depend on indivi
    14·1 answer
  • Why do you suppose a value of 5 is used? Do you think other values might work?
    6·1 answer
  • Report of invertor to convert 12 volt to 220 volt.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!