到这就想不出来了,有没有大神会的。。
main函数需要呼出bubble_sort_countries
#include <stdio.h>
#define COUNTRYNO 4
void bubble_sort_countries(int countries[COUNTRYNO],int n){
int i,j,tmp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (countries[j] > countries[j + 1]) {
tmp = countries[j];
countries[j] = countries[j + 1];
countries[j + 1] = tmp;
}
}
}
}
struct Country {
char country_name[50];
int population;
};
int find_most_populated( struct Country * );
int main( void ) {
struct Country countries[COUNTRYNO] = {
{ "United States", 331 },
{ "China", 1439 },
{ "Japan", 126},
{ "Brazil", 212 }
};
bubble_sort_countries(countries[], 5);
for (i = 0; i < 5; i++) {
printf("%d ", countries[i]);
}
if (countries[j] > countries[j + 1])
改为
if (countries[j].population < countries[j + 1].population)
temp改为struct Country 类型
#include <stdio.h>
#define COUNTRYNO 4
struct Country {
char country_name[50];
int population;
};
void bubble_sort_countries(struct Country countries[COUNTRYNO],int n){
int i,j;
struct Country tmp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (countries[j].population < countries[j + 1].population) {
tmp = countries[j];
countries[j] = countries[j + 1];
countries[j + 1] = tmp;
}
}
}
}
int main( void )
{
int i=0;
struct Country countries[COUNTRYNO] =
{
{ "United States", 331 },
{ "China", 1439 },
{ "Japan", 126},
{ "Brazil", 212 }
};
bubble_sort_countries(countries, COUNTRYNO);
for (i = 0; i < COUNTRYNO; i++) {
printf("Country: %s, Population: %d million\n", countries[i].country_name,countries[i].population);
}
return 0;
}