qq_37051324 2021-05-18 00:58 采纳率: 100%
浏览 245
已采纳

c++ 按以下要求编写程序:①定义一个具有100个元素的整数数组。

    按以下要求编写程序:①定义一个具有100个元素的整数数组。②使用随机函数为该数组的各元素赋值为0-100之间的随机整数值。    ③以适当格式显示该数组中的所有元素值。    ④如果数组的第一个数为偶数,则降序排列,如果第一个数为奇数,则升序排列。    ⑤统计该数组中最大值、最小值、总计值和平均值。

 

写了好几组都运行不出来,不知道自己思路是不是错了,要怎么编写呢,有没有案例可以让我研究一下

  • 写回答

2条回答 默认 最新

  • CSDN专家-Time 2021-05-18 03:21
    关注

     代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <algorithm>
    using namespace std;
    #pragma warning(disable:4996)
    int desc(int x,int y) {
    	return x > y;
    }
    int asc(int x, int y) {
    	return x < y;
    }
    int main() {
    	int b = 100;
    	int a = 0;
    	int arrs[100];
    	int count = 0;
    	int sum = 0;
    	srand((unsigned int)time(NULL));
    	// 生成随机数
    	while (count < 100) {
    
    		int x = (rand() % (b - a) + a);
    		arrs[count] = x;
    		count++;
    	}
    	// 适当形式打印
    	count = 0;
    	while (count < 100) {
    		if (count % 5 == 0) {
    			printf("\n");
    		}
    		printf("%d\t", arrs[count]);
    		sum += arrs[count];
    		count++;
    	}
    	int x = arrs[0];
    	if (arrs[0] % 2 == 1) {
    		// c++ 11 中 lambda表达式;
    		// 如果编译器比较老 
    		// 可以传递参数  bool cmp(int a,int b);
    		sort(arrs, arrs + 100, asc);
    		// sort(arrs, arrs + 100, [](int x, int y) {return x > y; });
    	}
    	else {
    		// sort(arrs, arrs + 100, [](int x, int y) {return x < y; });
    		sort(arrs, arrs + 100, desc);
    	}
    	printf("\n第一个数:%d\n排序后\n",x);
    	count = 0;
    	while (count < 100) {
    		if (count % 5 == 0) {
    			printf("\n");
    		}
    		printf("%d\t", arrs[count]);
    		count++;
    	}
    	if (x % 2 == 1) {
    		printf("\n最大值%d,最小值%d,总值%d,平均值%lf:\n", arrs[99],arrs[0], sum, (double)sum / 100);
    	}
    	else {
    		printf("\n最大值%d,最小值%d,总值%d,平均值%lf:\n", arrs[0],arrs[99], sum, (double)sum / 100);
    	}
    	return 0;
    }

    打印值:

    
    90      21      39      49      29
    31      28      19      67      1
    25      68      10      32      82
    4       49      43      22      36
    67      4       91      44      85
    65      75      35      91      56
    40      26      76      37      75
    80      41      63      68      1
    34      77      85      98      38
    30      64      96      24      52
    2       37      39      33      57
    75      19      58      84      30
    84      35      13      37      30
    44      96      16      70      99
    74      64      89      93      33
    10      45      45      46      89
    0       98      22      86      83
    69      1       29      83      35
    4       49      32      21      90
    12      86      96      30      96
    第一个数:90
    排序后
    
    99      98      98      96      96
    96      96      93      91      91
    90      90      89      89      86
    86      85      85      84      84
    83      83      82      80      77
    76      75      75      75      74
    70      69      68      68      67
    67      65      64      64      63
    58      57      56      52      49
    49      49      46      45      45
    44      44      43      41      40
    39      39      38      37      37
    37      36      35      35      35
    34      33      33      32      32
    31      30      30      30      30
    29      29      28      26      25
    24      22      22      21      21
    19      19      16      13      12
    10      10      4       4       4
    2       1       1       1       0
    最大值99,最小值0,总值5031,平均值50.310000:
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格