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
What folder holds 32-bit programs installed in a 64-bit installation of windows?
raketka [301]
<span>C:\Program Files (x86) folder</span>
8 0
2 years ago
Take the MBTI test and research information about this tool. Several websites have different versions of the test, including www
slega [8]

Answer:

why

Explanation:

3 0
2 years ago
What have been three technological changes to photography in the past 200 years?
kolbaska11 [484]
I hope this helps but cars, phones and computers.
3 0
3 years ago
Which of these definitions BEST explains what plagiarism is:
Alex777 [14]
To use another person's work without crediting the source.
6 0
3 years ago
Read 2 more answers
IT professionals recognize that successful systems must be user-oriented, and users need to be involved, formally or informally,
bogdanovich [222]

Answer:

True is the correct answer for the above question.

Explanation:

  • The software is a collection of instruction which states the computer to how to do a specific work. The software developer only develops the software, but it is used by many users of the software who wants to do some specific task.
  • The developer creates a graphical event for users through which the user can use the software of the computer system for his personal use.
  • So the above line concludes that the software is for the user only. So the user interaction in software development is necessary which is also said by the question-statement. Hence the question statement is the true statement.
5 0
3 years ago
Other questions:
  • You have noticed that one of your DNS servers has possibly been compromised. You believe that a cached DNS entry for your domain
    8·1 answer
  • Camille prepared excellent PowerPoint slides for her speech about education reform, but the speech didn't go as well as she had
    15·1 answer
  • The house belives that works should be compensated for the loss of jobs due to automation? Describe it in against motion of the
    15·1 answer
  • In 2d design, form makes what possible?
    15·1 answer
  • The first numerical control machine tool was demonstrated in 1952 in the United States at the Massachusetts Institute of Technol
    14·1 answer
  • What is the primary purpose of source code editor features such as keyword hi lighting and auto-completion A.to speed up the cod
    7·1 answer
  • Quick!! Im taking a timed test so pls hurry!! Ill even mark as brainliets!!
    5·2 answers
  • Why can't this app have people ask riddles.
    8·1 answer
  • Phân tích 5 phương hướng nhiệm vụ phát triển nông -lâm -ngư nghiệp ở nước ta hiện nay
    11·1 answer
  • What is the difference between algorithm and program?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!