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; }

}

Answers

Answer 1

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;

See attachment for complete corrections


Related Questions

what is the difference between internet and intranet​

Answers

Answer:

The internet is a globally connected networks of computers that enables people to share information and communicate with each other.

An intranet is a local or restricted network that enables people to store, organize and share information within an organization

What is the index of 7 in this list?
[5, 6, 10, 7, 3, 2.51]

Answers

The index of 7 is 3.

This is a straightforward technique for creating index numbers. In this, the sum of the prices for various commodities in the current year is divided by the corresponding base year price total, and the result is multiplied by 100. p = the sum of prices for the same commodities in the base year. (in Rs) 0 p (in Rs.) Thus option E is correct.

What are the index of number?

In mathematics, an index (or indexes) is the power or exponent that is added to a number or variable. For instance, the index of 2 in the number 24 is 4. Indexes is the plural form of the word. Constants and variables are concepts found in algebra.

A statistical tool for determining the amount of changes in a group of connected variables is an index number. It is derived from diverging ratios, from which it depicts the overall trend. It is a measurement of the normal change in a group of related variables between two distinct scenarios.

Therefore, 3 is the index of 7 in this list.

Learn more about index here:

https://brainly.com/question/4692093

#SPJ2

set screw compression and indenter are all types of
a. compression couplings.
b. threaded connectors.
C. rain-tight fittings.
D. threadless connecters and couplings.

Answers

nAnswer:

A.

Explanation:

The compression couplings can be defined as a set of screw, compression, and indenter. A compression fitting is a type of coupling used to connect pipes. A compression couplings is used to connect fixtures and tubings.

It contains screw, compression, and indenter. Therefore, option A is correct answer.

Answer:

dude above me capping its threadless connectors stay strong king

Explanation:

Other Questions
Activity: Find three different single digit numbers thatmultiply together to give 120. Go step by step to reduce the radical. 7.2 is 15% of what number? Being leader of the Chineseover the country ______ gave Mao almost complete control1. republican party2. communist party3. nationalist party She is laying her heart on the bottom line as she reaches into her soul and take all happiness that he has given her and hand it back to him. She takes all sadness and pain anyone has ever given him and puts it into her heart. He watches as the heart goes from red to black and she put it back in her chest. Before she walks away from her best friend, she hugs him but the warmth of his body stings against her cool skin for all the blood has stopped moving through out her body. She cannot think for her brain is dead as it was killed with everything else. She pulls away and walks away backwards as three black shadowy figure emerge from the concrete parking lot and drag her to the underground beneath. He scream out No, bring her back to me. And to her surprise, the black shadowy figures emerge once again and release her. He grabs her by her face and kisses her but since he kissed her, his heart becomes black too. The shadowy figures drag both of them underground and she clings to him as she sees its all fire. When they reach the rock bottom a crown is set on his head and he is sat on a throne. He is crowned the Demon Lord. They sit her on a throne too, for her throne is the Demon Lord's lap, where she stays. She became his queen, and he is her king. The first night in a hotel costs 60 dollars. All additional nights cost 49 dollars. How many nights did we stayat this hotel if the total bill was 501 dollars?nights What makes a healthy ecosystem? 7,12,13,14,15,14,19,14,13,10,11,7,18,20,14,16,14,18,13 find the median? HELP PLEASE I NEED TO TURN THIS IN ASAPEnglishwoman Harriet Martineau Visits a Cotton Mill I visited (in 1834) the corporate factory establishment at Waltham, within a few miles of Boston. The Waltham Mills were at work before those of Lowell were set up. The establishment is for the spinning and weaving of cotton (cloth)... Five hundred persons were employed at the time of my visit. The girls earn two, and sometimes three dollars a week, besides their board. The little children earn one dollar a week. Most of the girls live in the houses provided by the corporation, which accommodate from six to eight (girls)... The factory people bullt the church, which stands conspicuous on the green in the midst of the place. The minister's salary (elght hundred dollars last year) is raised by a tax on the pews. The corporation gave them a building for a lyceum, which they have furnished with a good library, and where they have lectures every winter - the best that money can procure. The girls have, in many Instances, private libraries of some merit and value... The people work about seventy hours per week, on the average. The time of work varies with the length of the days, the wages con uing the same. All look like well-dressed young ladies. The health is good; or rather, (as this is too much to be said without health anywhere in the United States ) it is no worse than it is elsewhere. -Harriet Martineau, Society in America (1837) 11. The author wrote this passage to: 1. Describe an American cotton mill.2. O recruit workers for Waltham Mills. 3. Discuss the health of mill workers. 4. show how badly workers were treated. Henry G.J. Moseley developed the current arrangement of elements in the periodic table by atomic number.True or False help with math plsssssssss What is 0.66% of 9,500 is Find the area of the trapezoid Which has more mass, one mole of carbon (C) or one mole of copper (Cu)?Explain. What is the polynomial in standard form? If 0.150 moles of calcium oxide reacts with carbon dioxide gas, how many grams of calcium carbonate can be produced? Lauren Fine Clothing manufactures clothes for professional women. Lauren applies overhead at the rate of $15 per direct labor hour. During April, the company has budgeted 9,420 direct labor hours. At the end of April, 9,200 direct labor hours and $132,670 in manufacturing overhead had been incurred. To adjust for the difference between applied and incurred overhead, which journal entry would the firm record (using the pro-rated approach) given the following ending balances: please help!! I don't know anything about the unit I'm on and this along with other questions soon would help me so much ONLY ANSWER IF YOU ARE GOING TO HELP BEEN ASKING FOR 2 HOURS NOW Need help on a assignmentDUE ON SUDAYPLEASE HELPI CANT POST ON HERE BECAUSE IT ON A DOCTHE ASSIGNMENT IS ABOUT BUSINESS FINANCIALWILL MAKE A GROUP Preguntas captulo # 3:1.Por qu disfrutaba Ana Rosa pasar tanto tiempo sentada en las ramas de su rbol gri gri?2.Desde su rbol gri gri Ana Rosa alcanzaba a ver algo que ella describe como un monstruo marino. Qu eventos se dieron para que la gente del pueblo se enterara de la noticia del monstruo marino?3.Cuando Ana Rosa relata lo que vio la gente no le crey. Qu sucedi para que le creyeran?4.Una vez convencidos los vecinos de que s haba un monstruo marino en la baha de Sosa, qu queran hacer los vecinos? Por qu?5.Cuando Ana Rosa comienza a escribir la historia sobre el monstruo marino tal parece como si comenzara a establecer un parecido entre ella y el monstruo. En qu se parecen el monstruo marino y Ana Rosa?6.La historia que Ana Rosa escribi capt la atencin de todo el vecindario, en especial la de su hermano Guario. Cmo sabe el lector que a Guario le gust mucho la historia que escribi su hermanita?7.Qu result ser el monstruo marino que Ana Rosa haba divisado?8. Cmo Ana Rosa describe la experiencia de escribir la historia y por qu?el color de mis palabras novela de espaol