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

in C please using MALLOC(malloc) function and while (i<j) the numbers are just e

ID: 3704404 • Letter: I

Question

in C please using MALLOC(malloc) function and

while (i<j)

the numbers are just example. it has to pass all coundition!

General Description Reverse an array of N integers Tasks Read an integer from the user that represents how many elements you will allocate in memory as an array. You can store this in a variable called N. Create an array of N elements Then you will read N integers from the user given to you in the following format and store them in the array 12 34610 12 33 10 Write a function called reverse that reverses the contents of the array. The function receives an array and an integer that represents how many elements the array contains. Also, the function must return the sum of all elements in the array. Note: You must modify the content of the array, that is, you must swap the elements from the first and last position and so on Output the reversed array in the following format using printf 10 33 12 10 643 21 Example When the input is: 1 23 4 The output is 4 3 21

Explanation / Answer

#include <stdio.h>
int reverse (int *, int);
int
main ()
{
int n;
printf ("Enter the no of elements in the array ");
scanf ("%d", &n);
// Initialize the size of the array to numbers
int *numbers = (int *) malloc (sizeof (int) * n);
  
printf ("Enter the elements of the array ");
for (int i = 0; i < n; i++)
{
scanf ("%d", &numbers[i]);
}
// The reverse function reverse the elements of the array
int sum = reverse (numbers, n);
printf ("Elements of the array in the reverse order ");
for (int i = 0; i < n; i++)
{
printf ("%d ", numbers[i]);
}
return 0;
}

int reverse (int *numbers, int size)
{
int i, j, temp, sum = 0;
i = 0;
j = size - 1;
while (i < j)
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++;
j--;
sum += numbers[i] + numbers[j];
}
if(size%2==1)
sum+=numbers[i];
return sum;
}