C程序编写,紧急求助。

来源:百度知道 编辑:UC知道 时间:2024/05/22 04:16:02
DIRECTIONS:
Write a C program will prompt the user for 11 ints, each >= 0, and each in the valid range for a C int. Load the elements into an int array.
1.Sort the 11 ints in ascending order.
2.Next, display the middle three values of the sorted array in ascending order.

HINT: use the bubble sort function to sort the elements of the array.
ASSUMPTIONS: all values entered are valid C ints >= 0.

编写一个C程序,输入11个整数,每个都大于或等于0,并且每一个都在C的整数限制范围内。把这些数装入一个整数的数组排列。
要求:1、由大到小排列数组
2、显示出排列好后的数组中间三个数值。
Hint:运用bubble sort function来按顺序排列数组。
假设:所有的输入数值都大于等于0.

EXAMPLE:

Enter value: 0
Enter value: 9
Enter value: 1
Enter value: 9
Enter value: 8
Enter value: 9
Enter value: 0
Enter value: 9
Enter value: 5
Enter value: 7
Enter value: 2
Middle values: 2 5 7
PROGRAM ENDS

最后程序运行时,就和上面的例子输出的一样就成。谢谢!

//二楼老兄输出的Middle values 不是中间三个
//中间三个的数组下标应该是 4、5、6
#include <stdio.h>

void bubble_sort(int a[], int n){
int i, j, temp;

for(i = 0; i < n; i++)
for(j = n-1; j > i; j--)
if(a[j] < a[j-1]){
temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}

}

int main(){

int a[11], i;

for(i=0;i<11;i++){
printf("Enter value: ");
scanf("%d",&a[i]);
}

bubble_sort(a, 11);

printf("Middle values: %d %d %d\n",a[4],a[5],a[6]);

return 0;
}

void bubble_sort_function(int a[11])
{
int i,j,temp;
for(j=0;j<11;j++)
{
for(i=0;i<11;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}

void main()
{
int a[11];<