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
A .jpg file is an example is an example of which if the following file types
Setler79 [48]
Answer chooses would help but a .jpg is a picture
4 0
3 years ago
Name some of the file extensions for images and provide more information about one or two of them.
lidiya [134]
.png - ability to maintain a transparent background. good for logos.

.jpg - more common image format. used for general image rendering and also has a higher quality than the .png format.
7 0
3 years ago
Can someone reply me
Georgia [21]
Yes how can i help you?
4 0
3 years ago
Can someone write a 5 sentence summary about Virtual Assistants. Please. I'm in a rush. Personal problems.
Lyrx [107]
Virtual Assistants can be useful. Blind people can use them to be informed, browse the web, and learn new facts. They can make tasks quicker too. They can control things like your heater or air conditioner. But if one were to malfunction, it could take lives.
7 0
3 years ago
_____________ was largely responsible for the internet becoming a resource of information and not merely a medium to send and re
MissTica

Answer:

Tim Berners-Lee

Explanation:

In 1989, Tim Berners-Lee invented the World Wide Web, an Internet-based hypermedia initiative for global information sharing while at CERN, the European Particle Physics Laboratory. He wrote the first web client and server in 1990. His specifications of URIs, HTTP and HTML were refined as web technology spread.He is also known as TimBL, is an English engineer and computer scientist. He is a Professorial Fellow of Computer Science at the University of Oxford and a professor at the Massachusetts Institute of Technology.

5 0
4 years ago
Other questions:
  • What would make this comparison statement False? Complete with the appropriate relational operator. "G" _____= "G"
    10·1 answer
  • Write a character literal representing the (upper case) letter<br> a.
    13·1 answer
  • Identify requirements that should be considered when determining the locations and features of firewalls. What are some importan
    5·1 answer
  • ________ is the gathering, organizing, sharing, and analyzing of the data and information to which a business has access.
    6·1 answer
  • The component that allows you to make a paper-based copy of a body of text is the _____.
    12·1 answer
  • If I want to have an estimate of the number of people who visited my website, which metric should I use?
    5·2 answers
  • 20 PTS URGENT!!! Derek’s organization uses an emerging technology that uses specialized software to place an image on an object,
    6·2 answers
  • What is the manufacturer’s specific ID for Intel Core?
    7·1 answer
  • What is the final amount stored in value if the computer selects 17 as the
    12·1 answer
  • What are placeholders in Java?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!