Write the definition of a function reverse , whose first parameter is an array o
ID: 3641985 • Letter: W
Question
Write the definition of a function reverse , whose first parameter is an array of integers and whose second parameter is the number of elements in the array. The function reverses the elements of the array. The function does not return a value.
So far I have this and it's telling me that I am not reversing all the elements. I think it might be an offset issue.
My code so far:
void reverse (int a[], int x)
{
int swap;
for (int i=0; i<=x; i++)
{
swap = a[i];
a[i] = a[x];
a[x] = swap;
}
return;
}
Explanation / Answer
swap = a[i]; a[i] = a[x]; a[x] = swap; } You are only swapping a[i], a particular element with a[x], the element after last element. The correct code: void reverse (int a[], int x) { int swap; for (int i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.