Function parameters, array variables, pointer variable, bubble sort

//指针变量做参数替代数组变量,二者的区分
#include <stdio.h>
void maoPaoPaiXu (int arr[],int len);//一维数组做参数
void mppx (int *p1,int len);
int main(int argc, const char * argv[]) {
    int n;
    printf("请输入你有多少数字:\n");
    scanf("%d",&n);
    int abb[n];
    printf("Input your number:\n");
    for (int i = 0; i < n; i++) {
        printf("第%d数:",i+1);
        scanf("%d",&abb[i]);
    }
    
    maoPaoPaiXu(abb,n);//我老是在这里出错!!!!!
    
    for (int i = 0; i < n; i++) {
        printf("第%d数:%d\n",i+1,abb[i]);
    }
    
    mppx (abb,n);
    for (int i = 0; i < n; i++) {
        printf("第%d数:%d\n",i+1,abb[i]);
    }
    
    return 0;
}
void maoPaoPaiXu (int arr[],int len)
{
    for (int i = 0; i < len -1; i++) {
        for (int j = 0; j < len-1-i; j++) {
            if (arr[j]<arr[j+1]) {
                arr[j] = arr[j]^arr[j+1];
                arr[j+1] = arr[j]^arr[j+1];
                arr[j] = arr[j]^arr[j+1];
            }
        }
    }
}
void mppx (int *p1,int len)
{
    //知道数组的地址之后,应该把它取出来?
    //地址是有规律的,所以应该按规律进行每个地址上的数字改变
    //
    
    for (int i = 0; i < len -1; i++) {
        for (int j = 0; j < len-1-i; j++) {
            if (*(p1+j) > *(p1+j+1) ) {
                *(p1+j) = *(p1+j) ^ *(p1+j+1);
                *(p1+j+1) = *(p1+j) ^ *(p1+j+1);
                *(p1+j) = *(p1+j) ^ *(p1+j+1);
            }
        }
    }
}


output:
Please enter your number of figures:
3
the Input your Number The:
Number 1: 5
2: 8
Number 3: 9
Number 1: 9
2: 8
Number 3: 5
number 1: 9
first 2: 8
number of. 3:. 5
Program ended with Exit code: 0

This code can be used to distinguish between an array variable as a function of pointer variables are parameters, and the difference between the operation of the same place.
And a bubble sort of usage.

Published 18 original articles · won praise 0 · Views 206

Guess you like

Origin blog.csdn.net/weixin_46456339/article/details/105098291