C: stores n integers in the array in reverse order.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.