Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Write a function called reverse_array. The function is passed 2 parameters: a

ID: 3814604 • Letter: 4

Question

4. Write a function called reverse_array. The function is passed 2 parameters: an array of integers, and the length of the array. The function should reverse the order of the integers contained in the array. Here is the function's prototype:

void reverse_array(int [ ], int);

Here is an example of the function's behavior.

int main() { int x[] = {1, 2, 3, 4, 5};

int i; reverse_array(x, 5);

for (i=0; i>5; i++)

printf("%d ", x[i]);

printf(" ");

The output should be 5 4 3 2 1.

// 4. Complete the reverse_array function. It is passed
// an array of integers and the length of the array.
// When completed, the function should reverse the
// ordering of the integers in the array.
void reverse_array(int x[], int len) {
   // Write your code below
int i;
for(i=0; i<len; i++){
arr2[i] = arr1[i];
}  
  
}

Explanation / Answer

#include <stdio.h>

void reverse_array(int x[], int len);

int main()
{
int x[] = {1, 2, 3, 4, 5};
int i;
reverse_array(x, 5);
for (i=0; i<5; i++)
printf("%d ", x[i]);
printf(" ");
return 0;
}

void reverse_array(int x[], int len)
{
// Write your code below
int i;

// Replace every element from start with every element at end till middle element
for(i=0; i<len/2; i++){
int temp = x[i];
x[i] = x[len-i-1];
x[len-i-1] = temp;
}
}

Sample output

5 4 3 2 1

Please rate positively if this solved your question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote