编写程序,从键盘输入10个int类型数,按从小到大顺序进行排序,然后输出。要求用函数sort()进行排序, 用函数input()输入数据,用函数output()输出数据。
(1) 主函数定义数组a[N],N定义为10
(2) 函数声名:
(3) 主函数调用函数实现程序功能。
(4) 函数定义
编写程序,从键盘输入10个int类型数,按从小到大顺序进行排序,然后输出
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答
- save4me 2014-12-23 01:02关注
下面的代码考虑输入的是否是整数
#include<stdio.h> #define N 10 void input(); void sort(); void output(); void main() { int a[N]; input(a, N); sort(a, N); output(a, N); getch(); } void input(int *a, int n) { int i = 0; char c; printf("\nEnter the %d of numbers: ", n); for (i = 0; i < n; i++) { printf("Please input number %d : ", i + 1); while(scanf("%d", &a[i]) != 1) { scanf("%s", &a[i]); printf("Sorry, [%s] is not a number. Please input number %d : ", &a[i], i); } } } void sort(int *a, int n) { int i, j, temp; for(i = 0; i < n; i++) { for(j = i; j < n; j++) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } void output(int *a, int n) { int i; printf("\nSorted array:"); for(i = 0; i < n; i++) { printf("\t%d",a[i]); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
- ¥50 成都蓉城足球俱乐部小程序抢票
- ¥15 yolov7训练自己的数据集
- ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
- ¥15 电力市场出清matlab yalmip kkt 双层优化问题
- ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
- ¥20 matlab yalmip kkt 双层优化问题
- ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
- ¥88 实在没有想法,需要个思路
- ¥15 MATLAB报错输入参数太多