|
@@ -0,0 +1,82 @@
|
|
|
+/*HEAD homework_1_show CPP HOMEWORK */
|
|
|
+/*==============================================================================
|
|
|
+2024年度工程软件底层架构作业
|
|
|
+================================================================================
|
|
|
+File description:
|
|
|
+提交者:陈浩
|
|
|
+邮 箱:335820035@qq.com
|
|
|
+================================================================================
|
|
|
+Date Name Description of Change
|
|
|
+05/14/2024 Chen Hao Created.
|
|
|
+05/21/2024 Chen Hao 增加部分注释、 IO输入输出接口、调整大括号{}位置 对齐方便阅读
|
|
|
+$HISTORY$
|
|
|
+================================================================================
|
|
|
+*/
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <time.h>
|
|
|
+
|
|
|
+#define SIZE 10
|
|
|
+
|
|
|
+void generateRandomNumbers(int arr[], int size);
|
|
|
+void bubbleSort(int arr[], int size);
|
|
|
+void printArray(int arr[], int size);
|
|
|
+
|
|
|
+int main()
|
|
|
+{
|
|
|
+ int numbers[SIZE];
|
|
|
+
|
|
|
+ // 1.生成随机数
|
|
|
+ generateRandomNumbers(numbers, SIZE); /*I*/
|
|
|
+ printf("原始数组:\n");
|
|
|
+ printArray(numbers, SIZE);// 输出原始数组
|
|
|
+
|
|
|
+ // 2.冒泡排序
|
|
|
+ bubbleSort(numbers, SIZE);
|
|
|
+
|
|
|
+ // 3.输出排序后的数组
|
|
|
+ printf("\n排序后的数组:\n"); /*O*/
|
|
|
+ printArray(numbers, SIZE);// 输出排序后数组
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+//生成随机数的程序
|
|
|
+void generateRandomNumbers(int arr[], int size)
|
|
|
+{
|
|
|
+ srand((unsigned)time(NULL)); // 设置随机数种子为当前时间
|
|
|
+ for (int i = 0; i < size; i++)
|
|
|
+ {
|
|
|
+ arr[i] = rand() % 100 + 1; // 生成1到100的随机数
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//冒泡排序的程序
|
|
|
+void bubbleSort(int arr[], int size)
|
|
|
+{
|
|
|
+ for (int i = 0; i < size - 1; i++)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < size - i - 1; j++)
|
|
|
+ {
|
|
|
+ if (arr[j] > arr[j + 1])
|
|
|
+ {
|
|
|
+ // 交换位置
|
|
|
+ int temp = arr[j];
|
|
|
+ arr[j] = arr[j + 1];
|
|
|
+ arr[j + 1] = temp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}//05/21/2024 Chen Hao Added Start,大括号换位置,对齐,方便阅读
|
|
|
+
|
|
|
+//输出排序后的程序
|
|
|
+void printArray(int arr[], int size)
|
|
|
+{
|
|
|
+ for (int i = 0; i < size; i++)
|
|
|
+ {
|
|
|
+ printf("%d ", arr[i]);//输出排序后的随机数
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+}
|
|
|
+
|
|
|
+// 05/21/2024 Chen Hao Added End
|