C 编写一个函数,在结构数组中查找前5个最大值?

C 编写一个函数,在结构数组中查找前5个最大值?,c,arrays,structure,C,Arrays,Structure,在结构数组中查找前5个最大值(对于C编程)? 我有一系列结构,如下所示: struct info { char name[100]; int number; } struct info people[10] char name[100]中是人名(最多10个),在int balance中有相应的值: Jane 10 John 40 Harry 16 Eva -5 ... 直到有10个人。 如何查找和打印拥有最高数字的5个人? i、 e: 我尝试了以下代码: int i,j, k=5, max,

在结构数组中查找前5个最大值(对于C编程)? 我有一系列结构,如下所示:

struct info {
char name[100];
int number;
}
struct info people[10]
char name[100]中是人名(最多10个),在int balance中有相应的值:

Jane 10
John 40
Harry 16
Eva -5
...
直到有10个人。 如何查找和打印拥有最高数字的5个人?
i、 e:

我尝试了以下代码:

int i,j, k=5, max, temp;
//move maximum 5 numbers to the front of the array
for (i=0; i<k; i++) {
    max=i;
for (j=i+1; j<10; j++) {
    if (people[i].number>people[max].number) {
        max=j;
    }
}
//swap numbers
temp=people[i].number;
people[i].number=people[max].number;
people[max].number=temp;

//swap names to match swapped numbers so they correspond
temp=people[i].name;
people[i].name=people[max].name;
people[max]=temp;
}
for (i=0; i<k; i++) {
    printf("%s  %d\n", people[i].name, people[i].number);
}
inti,j,k=5,最大温度;
//将最多5个数字移动到阵列前面

对于(i=0;i最简单、最通用的方法可能是首先对
人员
数组进行排序。完成后,只需选择前五个元素。

最好的方法是根据数字属性iso单个交换对数组人员进行排序


如果您想继续使用当前方法,请使用strcpy函数iso“=”运算符作为name

而不是跟踪总共10个索引中的5个,排序对我来说似乎更好。 您可以使用qsort对10个元素进行排序,然后选择前5个元素

 int cmp(void *a,void *b)
 {
   struct info as=*((struct info*)a);
   struct info bs=*((struct info*)b)
   return bs.number-as.number;  //sorting in descending order
 }
然后

    qsort(people,10,sizeof people[0],cmp);

只需对数组进行排序,然后获取已排序数组的5个首/末项(取决于排序顺序)

首先定义一个比较函数:

#include <stdlib.h> /* for qsort() */
#include <stdio.h> /* for printf() */


struct info
{
  char name[100];
  int number;
};

int cmp_struct_info_desc(const void * pv1, const void * pv2)
{
  const struct info * pi1 = pv1;
  const struct info * pi2 = pv2;

  return pi2->number - pi1->number;
}

从OP的代码开始,只需交换结构..OP的代码只需稍作更改

不能使用赋值复制数组,但可以指定对象,如
struct info

int i, j, k=5;

//move maximum `k` numbers to the front of the array
for (i=0; i<k; i++) {
  int max=i;
  for (j=i+1; j<10; j++) {
    if (people[i].number > people[max].number) {
      max=j;
    }
  }
  //swap info
  struct info temp = people[i];
  people[i] = people[max];
  people[max] = temp;
}

for (i=0; i<k; i++) {
  printf("%s  %d\n", people[i].name, people[i].number);
}
inti,j,k=5;
//将最大“k”个数字移动到数组的前面

对于(i=0;i)您可以使用strcpy函数族复制字符串。为什么要交换结构?只需找到与数组中具有最大值的五个元素相对应的五个索引。记住要考虑重复值的可能性。
struct info temp=people[i];people[i]=people[max];people[max]=temp;
使用此示例进行排序..@4py-对
c
程序员没有太大帮助:(如果使用现有函数进行排序,这是唯一最简单的方法……否则,如果从头开始编程,扫描数组并记住前5个索引更容易编码。他不应该使用现有函数吗?这就是我的意思。Plussed。
struct info people[] =  {
  ... /* initialise array people here ... */
}

int main(void)
{
  size_t number_of_array_elements = sizeof people/sizeof *people;

  qsort(people, number_of_array_elements, sizeof *people, cmp_struct_info_desc);

  for (size_t s = 0; s < number_of_array_elements; ++s)
  {
    printf("%zu. = {%d, '%s'}\n", s, people[s].number, people[s].name);
  }
}
int i, j, k=5;

//move maximum `k` numbers to the front of the array
for (i=0; i<k; i++) {
  int max=i;
  for (j=i+1; j<10; j++) {
    if (people[i].number > people[max].number) {
      max=j;
    }
  }
  //swap info
  struct info temp = people[i];
  people[i] = people[max];
  people[max] = temp;
}

for (i=0; i<k; i++) {
  printf("%s  %d\n", people[i].name, people[i].number);
}