Stores n integers in the array in reverse order.
Solution: Program 1:
#include <stdio.h>
int inv (int x[], int n)
{
int temp, I, j, M = (n-1)/2;
for (i = 0; I <= m; i++)
{
j = n-1-I;
temp = X[i];
X[i] = X[j];
X[J] = temp;
}
Return
}
int main ()
{
int I, a[10] = {1,2,3,4,5,6,7,8,9,10};
printf ("The original array:\n");
for (i = 0; i <; i++)
{
printf ("%d", a[i]);
}
printf ("\ n");
INV (A, 10);
printf ("The array has been inverted:\n");//inverted: Reverse
for (i = 0; i <; i++)
{
printf ("%d", a[i]);
}
printf ("\ n");
return 0;
}
Program 2: Using pointer variables as arguments
#include <stdio.h>
int inv (int *x, int n)
{
int *p, temp, *i, *j, M = (n-1)/2;
Shape parameter group name x receives the address of the real parameter group first element a[0]
i = x; j = x + n-1; p = x + M;
for (; I <= p; i++, j--)
{
temp = *i;
*i = *j;
*j = temp;
}
Return
}
int main ()
{
int I, a[10] = {1,2,3,4,5,6,7,8,9,10};
printf ("The original array:\n");
for (i = 0; i <; i++)
{
printf ("%d", a[i]);
}
printf ("\ n");
INV (A, 10);
printf ("The array has been inverted:\n");//inverted: Reverse
for (i = 0; i <; i++)
{
printf ("%d", a[i]);
}
printf ("\ n");
return 0;
}
Results:
He original array:
1 2 3 4 5 6 7 8 9 10
The array has been inverted:
10 9 8 7 6 5 4 3 2 1
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1752943
C: stores n integers in the array in reverse order.