10.13-3 指针与一维数组相关运算

2017-07-15  本文已影响0人  日常表白结衣

【输出数组最大值】

#include<stdio.h>
#define SIZE 5
void max(  int *source,int *end);
int main()
{
     int source[SIZE] = { 1,2,3,4,5 };
     printf("the max of the arry:\n");
     max(source, source + SIZE);

    getchar();
    getchar();
    return 0;
}
void max(  int *source, int *end)
{
    int max = *source;
    while (source < end)
    {
        if (max < *source)
        {
            max = *source;
            source++;
        }
        else
        {
            source++;
        }
    }
    printf("%d ", max);
}

【数组逆序输出】

#include<stdio.h>
#define SIZE 5
void Reverse_Arry( double *source, double *arry, double *end);
int main()
{
    double source[SIZE] = { 1.1,2.2,3.3,4.4,5.5 };
    double arry[SIZE] = { 0 };

    Reverse_Arry(source, arry, source + SIZE);
    for (int i = 0; i < SIZE; i++)
        printf("%8.3lf  ", arry[i]);
    putchar('\n');

    getchar();
    getchar();

    return 0;
}
void Reverse_Arry(double *source, double *arry, double *end)
{
    while (source < end)
    {
        *(arry + (SIZE-1)) = *(source);
        source++;
        arry--;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读