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
Defensive programming is sometimes referred to as _________.
Romashka [77]
Anti-malware and Anti-Virus
6 0
3 years ago
A species of snake lives in a rainforest. Which would be an adaptation for this
raketka [301]

Answer:

The Answer is D

6 0
3 years ago
Read 2 more answers
Which statement is false? Select one: a. A class is to an object as a blueprint is to a house. b. Classes are reusable software
kow [346]

Answer:

A class is an instance of its object

Explanation:

6 0
3 years ago
Which would take more storage space, a layer file showing all the us counties or a layer file showing all the us states?
Ksju [112]
All counties. Much more info. Although most layers are just excel files with geographical reference data in them. That one file may be several MB as the state file may be 1mb or less
5 0
3 years ago
Fill in the blank with the correct response.<br> A submenu in a text editor is indicated by a <br> .
igomit [66]

Answer:

triangle

Explanation:

7 0
3 years ago
Other questions:
  • Which option organizes tasks based on importance?
    12·1 answer
  • Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the
    7·1 answer
  • The computer component that makes sure that instructions are decoded and executed properly is the ___________.
    11·2 answers
  • Insert the components that the computer uses to perform the tasks of each of the parts:
    13·1 answer
  • What does Pentium means?:/
    7·2 answers
  • Consider the following threats to Web security, and describe how each is countered by a particular feature of SSL.
    15·1 answer
  • Write a code to define a structure named student, which includes name, student number and email address
    5·1 answer
  • What is sum after the following loop terminates? int sum = 0; int item = 0; do { item ; sum = item; if (sum &gt; 4) break; } whi
    8·2 answers
  • Security breaches are easier to address with which payment form?
    11·2 answers
  • Use the drop-down menus to complete each sentence.
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!