用C语言编写,查找数组中的最值

 时间:2026-02-15 15:08:24

1、打开visual C++ 6.0-文件-新建-文件-C++ Source File

用C语言编写,查找数组中的最值

用C语言编写,查找数组中的最值

2、定义变量:

#include <stdio.h>

main()

{

    int a[20], max, min, i, j, k, n;                      /*定义数组及变量数据类型为基本整型*/

    j=0; k=0;

用C语言编写,查找数组中的最值

3、输入元素的个数:

    printf("please input the nunber of elements:\n");

    scanf("%d", &n);                                     /*输入要输入的元素个数*/

用C语言编写,查找数组中的最值

4、输入数据:

    printf("please input the element:\n");

    for (i = 0; i < n; i++)                              /*输入数据*/

        scanf("%d", &a[i]);

    min = a[0];

用C语言编写,查找数组中的最值

5、找出数组中最小的数:

    for (i = 1; i < n; i++)                              /*找出数组中最小的数*/

        if (a[i] < min)

        {

            min = a[i];

            j = i;                                  /*将最小数所存储的位置赋给j*/

        }

    max = a[0];

用C语言编写,查找数组中的最值

6、找出数组中的最大数:

    for (i = 1; i < n; i++)                              /*找出这组数据中的最大数*/

        if (a[i] > max)

        {

            max = a[i];

            k = i;                                  /*将最大数说存储的位置赋给k*/

        }

用C语言编写,查找数组中的最值

7、输出结果:

    printf("\nthe position of min is:%3d\n", j);          /*输出原数组中最小数所在的位置*/

    printf("距八the min number is:%3d\n", min);

    printf("the position of max is:%3d\n", k);                /*输出原数组中最大数所在的位置*/

    printf("the max number is:%3d\n", max);

    return 0;

用C语言编写,查找数组中的最值

8、完整的源代码:

#include <stdio.h>

main()

{

    int a[20], max, min, i, j, k, n;                      /*定义数组及变量数据类型为基本整型*/

    j=0; k=0;

    printf("please input the nunber of elements:\n");

    scanf("%d", &n);                                     /*输入要输入的元素个数*/

    printf("please input the element:\n");

    for (i = 0; i < n; i++)                              /*输入数据*/

        scanf("%d", &a[i]);

    min = a[0];

    for (i = 1; i < n; i++)                              /*找出数组中最小的数*/

        if (a[i] < min)

        {

          三眠改  min = a[i];

            j = i;                                  /*将最小数所存储的位置赋给j*/

        }

    max = a[0];

    for (i = 1; i < n; i++)                              /*找出这组数据中的最大数*/

        if (a[i] > max)

        {

            max = a[i];

            k = i;                              薪耻    /*将最大数说存储的位置赋给k*/

        }

    printf("\nthe position of min is:%3d\n", j);          /*输出原数组中最小数所在的位置*/

    printf("the min number is:%3d\n", min);

    printf("the position of max is:%3d\n", k);                /*输出原数组中最大数所在的位置*/

    printf("the max number is:%3d\n", max);

    return 0;

}

用C语言编写,查找数组中的最值

  • 如何利用单片机测量正弦波的峰值?
  • 如何在proteus中放置直流电压表?
  • Keil uVision5如何添加.H文件
  • 15按键控制数码管加减
  • 【Keil+Proteus仿真】51单片机实现心形流水灯
  • 热门搜索
    衣服搭配学习 石正丽简介 太监怎么净身 心里压力大怎么办 短靴搭配 斐济旅游攻略 健康减肥 网络怎么挣钱 菲亚特菲跃怎么样 西塘旅游攻略