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

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条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)