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
jenyasd209 [6]
3 years ago
8

Suppose that a is a one-dimensional array of ints with a length of at least 2. Which of the following code fragments successfull

y exchange(s) the values of the first two elements of a?I a[ 0 ] = a[ 1 ]; a[ 1 ] = a[ 0 ];II int t = a[ 0 ]; a[ 0 ] = a[ 1 ]; a[ 1 ] = t;III a[ 0 ] = a[ 0 ] - a[ 1 ]; a[ 1 ] = a[ 0 ] + a[ 1 ]; a[ 0 ] = a[ 1 ] - a[ 0 ]; A) I only B) II only C) III only D) I and II only E) II and III only
Computers and Technology
1 answer:
Lyrx [107]3 years ago
6 0

Answer:

E) II and III only

<h2>Explanation of II</h2>

The chunk of code given in II uses an array a and a variable t which is called temporary variable and it holds the value of some other variable or an array element temporarily.

So the first line of the code int t=a[0] the temporary variable t holds the current value of the array a i.e the array element at first index of the array a[0] is copied to variable t.  This variable t is used to hold the original value of a[0] so that if the value of a[0] is overwritten later, it wont lose its original value.

a[0]=a[1] This line of code stores the value of a[1] in a[0]. This means that a[0] holds the array element at next index a[1] . This means the original value of a[0] is no longer the same because now it contains the value of a[1].  

The next line a[1]=t. This means that now a[1] holds the value which was stored in variable t. As the variable t was used to store the original value of a[0]. So that value is now stored in a[1].

Lets take an example: array a contains the two elements 1  at a[0] and 2 at a[1] position.

So according to the first statement

t=a[0] means that t holds value of array a at 0 index. We have 1 at 0th index of array so t=1

Next statement a[0]=a[1] means the value at a[1] is copied to a[0]

So at a[1] the value is 2. So this value 2 is stored in a[0] position in the array.

The last statement a[1]=t means now the value stored in t is copied to a[1]

As t stored the value of a[0] which was array element 1 so now this array element is copied to a[1]th position of the array. So a[1] holds 1 now.

This is how the elements 1 and 2 are swapped as now a[0] holds 2 and a[1] holds 1.

<h2>Explanation for III</h2>

Suppose that a[0]=1 and a[1]=2

The first statement a[ 0 ] = a[ 0 ] - a[ 1 ]; means that the value at a[0] is subtracted by the value at a[1]. That means 1-2 as 1 is the array element at a[0] and 2 is the array element at a[1] so 1-2=-1 This value is now stored at a[0] th position of array.  Now a[0]=-1 and a[1]=2

Next statement a[ 1 ] = a[ 0 ] + a[ 1 ];  means adding array element at a[0] and a[1] and the result of this addition is stored in a[1]. As a[0] is -1 and a[1] is 2 So a[0]+a[1]= -1+2=1 So a[1]=1 This means 1 will be placed at a[1] th position of array a. Now a[0]=-1 and a[1]=1

Note that a[1] now holds the value 1 which was the original value of a[0]  in start. This is because the first statement of code subtracts value a[1] from value of a[0] and then adds back value of a[1] to value of a[0] to keep the original value of a[0] safe for swapping just like in II the original value of a[0] is kept by temporary variable t.

The last line a[ 0 ] = a[ 1 ] - a[ 0 ]; means subtracting value at a[0] from value in a[1]. As we know that a[0]=-1 and a[1]=1 So a[0]=a[1]-a[0]=1+1=2 So a[0]=2.

Note that a[0] now holds the value 2 which was the original value of a[1]  in start. In the statement a[ 1 ] = a[ 0 ] + a[ 1 ] the value of a[0] is added to a[1] to get new value of a[1] and in statement a[ 0 ] = a[ 1 ] - a[ 0 ]; the value of a[0] is subtracted from that of a[1] in order to get previous original value of a[1]. So in the end a[0]=2 and a[1]=1 This is how the two value are swapped.

Explanation of I being false:

The line of code a[0] = a[1]; copies the value of a[1] to a[0]

This means the original value at a[0] is changed. This is called to overwrite the value. It is required to exchange values of elements in a[0] and a[1]. In order to swap two elements their original values should be retained but in this statement overwrites the original value in a[0]. So I is wrong.

 

You might be interested in
WHAT DOES THE SCRATCH CODE BELOW DO?
Vinil7 [7]
I think it’s b vertically
7 0
3 years ago
The basic components of cartridges and shotshells are similar. Shot pellets and a bullet are examples of which basic component?
Liono4ka [1.6K]
The answer is projectile. Handguns and rifles use a cartridge having a single projectile or bullet. Shotguns use a shot shell comprising either a single bullet or a big number of small projectiles (shot or pellets). Though, the basic components of cartridges and shot shells are alike.
5 0
3 years ago
Suppose the m2 money supply is $13 trillion, including: $7 trillion in savings accounts $3 trillion in checking accounts $1 tril
tia_tia [17]

Answer:

M1 is equal to $ 4 trillion

Explanation:

M1 money supplies are liquid money supplies like cash, checkable deposits, traveler's check etc. It is equal to;

M1= coins and currency in circulation + checkable (demand) deposit + traveler's check.

M2 money supply are less liquid and is equated as;

M2 = M1 + savings deposit + money market fund + certificates of deposit + other time deposits.

Savings = $7 trillion

Checkable deposit = $3 trillion

Money market fund = $1 trillion

Currency = $1 trillion

Certificates of deposit = $1 trillion

M1 = currency + checkable deposit

= $1 + $ 3

= $4 trillion.

6 0
2 years ago
John is a mechanical engineer. His first task at his job is to build any microphone of his choice. He chooses to build a dynamic
Jobisdone [24]

Answer:

A. simple to construct and easy to repair

Explanation:

A dynamic microphone works on the principle of electromagnetic principle. A diaphragm is attached to a coil of wire which helps in producing sound. Responding to the sound waves, the coil of the wire is vibrated by the diaphragm.  A magnetic field is created by the magnet which is present inside the coil of wire. The electrical signal is generated in response to the motion of the coil. The speed of the motion produces the amount of the current.

Since the construction of a dynamic microphone is easier as compared to any other microphone, John is likely to opt to build one.

7 0
3 years ago
How to calculate a pid controller
pishuonlain [190]

Answer:

Control by PID1

is a control method often used for servos.

Don't you know what a bondage is? Well, it's a system, capable of reaching and

maintain a setpoint thanks to the measurements it performs.

Imagine, for example, in a car on the highway. You want to drive at 130Km / h

without having to press the accelerator. Your car's cruise control should

by itself maintain this speed. When approaching a slope the system "notices" that for

the same power at the level of the motor, it no longer reaches the 130 km / h setpoint and will add a

little acceleration. Yes but by how much? And how long will it take for the system to

stabilize around the setpoint?

That's the whole servo problem and PID control is one way to solve it!

PID is the most widely used regulator in industry. The idea of ​​this control body is to

intentionally modify the value of the error which remains between the setpoint and the measurement

performed.

For example in the case of a position control the error would be: ε = c (p) - s (p)

In the case of proportional control, the error is virtually amplified by a certain gain

constant that should be determined according to the system.

Setpoint (t) = Kp.ε (t)

What in Laplace gives:

Setpoint (p) = Kp.ε (p)

Explanation:

5 0
3 years ago
Other questions:
  • 2. You have classes to represent different shapes (see below). You realize you can benefit from inheritance and polymorphism by
    13·1 answer
  • Combination lock uses three numbers beween 1 and 36 with repetition , how mant combinations are possiable
    6·1 answer
  • The term Electronic Privacy Information Center (EPIC) refers to a form of the digital subscriber line technology, which enables
    6·1 answer
  • You can avoid culture shock by ____________.
    10·2 answers
  • All living organisms make up the (4 points)
    8·1 answer
  • Explain three specific &amp; major security threats to internet users. In each case, address whether or not the vulnerabilities
    15·1 answer
  • What<br>are<br>the features of secondary storage media​
    13·2 answers
  • Reply emailing a student who is asking about audio materials​
    6·2 answers
  • The use of a concept or product from one technology to solve a problem in an unrelated one
    11·1 answer
  • The impact of the destruction of train station on the budget of the minister of finance
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!