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
Question 2 Unsaved Which of these is NOT an example of an emerging technology? Question 2 options: Sixth Sense Close Range Drone
alexandr1967 [171]

The answer is <em>B.) Close Range Drones</em>

I just took the test

3 0
3 years ago
Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method
weeeeeb [17]

Using the computational language in python we have to use it to write to a file and read with the code.

<h3>Writing this code in python we have:</h3>

<em>filename = input()</em>

<em>file = open(filename)</em>

<em>lines = file.readlines()</em>

<em>data = {}</em>

<em>for i in range(0, len(lines), 2):</em>

<em>    num_seasons = int(lines[i].strip())</em>

<em>    show = lines[i + 1].strip()</em>

<em>    if num_seasons not in data:</em>

<em>        data[num_seasons] = []</em>

<em>    data[num_seasons].append(show)</em>

<em>file.close()</em>

<em>file_writer = open('output_keys.txt', 'w')</em>

<em>titles = []</em>

<em>for num_seasons in sorted(data):</em>

<em>    shows = []</em>

<em>    for show in sorted(data[num_seasons]):</em>

<em>        titles.append(show)</em>

<em>    file_writer.write(str(num_seasons) + ': ' + '; '.join(data[num_seasons]) + '\n')</em>

<em>file_writer.close()</em>

<em>file_writer = open('output_titles.txt', 'w')</em>

<em>for title in sorted(titles):</em>

<em>    file_writer.write(title + '\n')</em>

<em>file_writer.close()</em>

See more about python at brainly.com/question/18502436

#SPJ1

3 0
3 years ago
How do you answer a question on here
Monica [59]
Didn’t you just make a question right now?
4 0
4 years ago
Read 2 more answers
Organic farming grows more food than chemical farming true or false?
Studentka2010 [4]

FALSE

Organic farming systems yield approximately 10-20% lesser than chemical farming.

Conventional farming has always prioritized more produce, risking other sustainability metrics such as nutritional quality, energy use, soil quality, biodiversity and pollution. Organic farming presents a more balanced approach to agriculture. Organic farming better harmonizes the four areas of sustainability: environment, economics and social well-being.


6 0
3 years ago
Read 2 more answers
How to fix a blue screen
Ipatiy [6.2K]

Answer:

I recommend unplugging and plugging it back

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • It is appropriate to leave sections of an application blank ?<br><br><br><br> True or False
    7·1 answer
  • A selected graphic appears surrounded by a(n) ______, which has small squares and circles around its edges.
    7·1 answer
  • Difference between Hard copy and Soft copy?​
    13·1 answer
  • You are to write a program name matrix.java that multiplies two matrices. 1. Your program should prompt the user for the dimensi
    5·1 answer
  • WHO SAYS BUP ALL THE TIME ​
    8·1 answer
  • What technique is used when setup times at a workstation are sequence dependent?
    15·1 answer
  • Re:
    15·1 answer
  • You have searched a database to locate US cities with population counts above 1 million people. You need to present the results
    6·2 answers
  • Which of the following safety and privacy features is not included in a P2P app or service?
    5·2 answers
  • What is the purpose of system software?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!