• 数组元素交换


    用函数编程实现两个数组中对应元素值的交换。
    **输入格式要求:"%d" 提示信息:"Input array size(n<=10):" "Input array a:"
    "Input array b:"
    **输出格式要求:"Output array a:" "Output array b:" "%5d"
    程序运行示例如下:
    Input array size(n<=10):5
    Input array a:1 2 3 4 5
    Input array b:6 7 8 9 10
    Output array a: 6 7 8 9 10
    Output array b: 1 2 3 4 5

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #define N 10
     4 
     5 void ReadData(int a[], int n);
     6 void PrintData(int a[], int n);
     7 void Swap(int *x, int *y);
     8 
     9 void ReadData(int a[], int n)
    10 {
    11     int i;
    12     for (i = 0; i < n; i++)
    13     {
    14         scanf("%d", &a[i]);
    15     }
    16 }
    17 
    18 void PrintData(int a[], int n)
    19 {
    20     int i;
    21     for (i = 0; i < n; i++)
    22     {
    23         printf("%5d", a[i]);
    24     }
    25     printf("
    ");
    26 }
    27 
    28 //两整数值互换
    29 void Swap(int *x, int *y)
    30 {
    31     int temp;
    32     temp=*x;
    33     *x=*y;
    34     *y=temp;
    35 }
    36 
    37 int main()
    38 {
    39     int a[N], b[N], i, n;
    40     printf("Input array size(n<=10):");
    41     scanf("%d", &n);
    42     printf("Input array a:");
    43     ReadData(a, n);
    44     printf("Input array b:");
    45     ReadData(b, n);
    46 
    47     for(i=0; i<n; ++i)
    48         Swap(&a[i],&b[i]);
    49     printf("Output array a:");
    50     PrintData(a, n);
    51     printf("Output array b:");
    52     PrintData(b, n);
    53     return 0;
    54 }
  • 相关阅读:
    直线方程和直线系方程
    多个参数之和积的取值范围
    解析几何习题
    React之Perf
    一文看懂npm、yarn、pnpm之间的区别
    Cannot read property 'properties' of undefined
    为什么要用PolyFill(JS中的修补匠)
    es6-promise
    原生js实现each方法
    有趣的js获取input标签中光标的索引
  • 原文地址:https://www.cnblogs.com/crane-practice/p/3727950.html
Copyright © 2020-2023  润新知