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
Romashka [77]
3 years ago
7

Your colleague developed a function, which is supposed to reverse an array of integers, e.g., {1, 5, -10, 7, 23} => {23, 7, -

10, 5, 1}. Test cases show that the function does not work correctly. Please look at the following code and identify all the troubles. Please specify which part(s) of the code will not work and explain why not.
void ReverseArray(int arr[], size_t elements_no) {
size_t i = 0; int x = 0; for (i = 0; i <= elements_no; i++)
{ x = arr[i]; arr[i] = arr[elements_no - i]; arr[elements_no - i] = x; }

}
Computers and Technology
1 answer:
Aliun [14]3 years ago
8 0

Answer:

The parts of the code which makes the program not to work properly is:

1. for (i = 0; i <= elements_no; i++)

2. arr[i] = arr[elements_no - i];

3. arr[elements_no - i] = x;

The correction of the above lines are:

1. for (i = 0; i <= elements_no/2; i++)

2. arr[i] = arr[elements_no - i-1];

3. arr[elements_no - i-1] = x;

Explanation:

It should be noted that the code end up not reversing the array. This happens because the programmer iterates through all the array elements.

i.e.

1. for (i = 0; i <= elements_no; i++)

This will result in the original array.

To solve this problem, the iteration has to stop at the middle element and the correction is:

1. for (i = 0; i <= elements_no/2; i++)

Also, to swap the individual elements 1 has to be subtracted from each array index. So, the following lines:

2. arr[i] = arr[elements_no - i];

3. arr[elements_no - i] = x;

is corrected to:

2. arr[i] = arr[elements_no - i-1];

3. arr[elements_no - i-1] = x;

<em>See attachment for complete corrections</em>

Download txt
You might be interested in
Is electricity a force or energy?
nevsk [136]

Answer:

Explanation:

Electrical energy is a form of energy resulting from the flow of electric charge. Energy is the ability to do work or apply force to move an object. In the case of electrical energy, the force is electrical attraction or repulsion between charged particles

6 0
1 year ago
In a dark place you have only a match , a twig , campfire logs , and oil lamp and a candle which do you literally first /
ohaa [14]

Answer:

You would use a twig first :)

Explanation:

3 0
3 years ago
Which example best demonstrates an impact of computers on health care?
Dennis_Churaev [7]
B-Robotic surgery allows surgeons to make smaller incisions.

Explaination: Anything having to do with medical procedures are healthcare so this is my reasoning.
4 0
3 years ago
Read 2 more answers
Editing is the process of cutting out the bad parts. True or false?
Maurinko [17]

Answer:

true

Explanation:

your welcome

7 0
3 years ago
Read 2 more answers
Which technology is used to uniquely identify a wlan network?
expeople1 [14]
The answer is SSID

SSID which in full stands for Service Set Identifier is used to identify a network. It ensures that clients are able to locate a WLAN in which they are attached to. In Layman’s terms, it’s the username or the primary name in a WiFi setup in which a user joins and is connected to.






8 0
3 years ago
Other questions:
  • Assume that aList is a valid ArrayList containing the following:
    8·1 answer
  • if you are trying to reduce your debt, which of these expenses should you consider cutting out or cutting back on first?
    11·1 answer
  • Why do you think it is necessary to set the sequence in which the system initializes video cards so that the primary display is
    15·1 answer
  • In client server network, there is no central server //// true or false​
    8·1 answer
  • 4) Which is a more efficient way to determine the optimal number of multiplications in a matrix-chain multiplication problem: en
    15·1 answer
  • Write an expression whose value is the concatenation of the three strigs name1, name2, and name3, separated by commas
    9·1 answer
  • Question
    14·1 answer
  • Explain the working principal of computer system with suitable diagarm​
    9·1 answer
  • You have been asked to replace a cracked screen on a laptop. The replacement screen was delivered today, but it did not include
    7·1 answer
  • The SQL SELECT built-in function COUNT ____.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!