-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29.c
59 lines (50 loc) · 1.06 KB
/
29.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
int main()
{
void sortArray(int *arr, int n);
double average(int *arr, int n);
int n, i;
printf("请输入数组的长度:");
scanf("%d", &n);
int arr[n];
printf("请输入%d个整数:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
sortArray(arr, n);
double avrg = average(arr, n);
printf("排序后的数组:\n");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
printf("平均值:%.2lf\n", avrg);
return 0;
}
void sortArray(int *arr, int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
double average(int *arr, int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum / n;
}