简体   繁体   English

如何在C中返回数组中最大的两个数字?

[英]How to return largest two numbers in array in C?

So, I have this so far. 所以,到目前为止,我有这个。 I'm trying to find the two largest numbers in an array and return them. 我试图在数组中找到两个最大的数字并返回它们。 I looked up a lot of resources online, and most of them say "call by reference" is the way to go. 我在网上查了很多资源,大多数人说“按参考电话”是要走的路。 But I've no idea how to make it work with my program. 但我不知道如何使它与我的程序一起工作。 For example, I saw this example online: 例如,我在网上看到了这个例子:

void Calculate(int x, int y, int* prod, int* quot)
{
    *prod = x*y;
    *quot = x/y;
}

int x = 10,y = 2, prod, quot;
Calculate(x, y, &prod, &quot)

How does the above program actually "return"? 上述程序如何实际“返回”? How do I print the return values to the console? 如何将返回值打印到控制台?

#include "stdio.h"


void largest_two( int numbers[], int len, int *largest, int *next_largest){


  int i, temp;

  *largest = numbers[0];
  *next_largest = numbers[1];

  if(*largest < *next_largest){
    temp = *next_largest;
    *largest = *next_largest;
    *next_largest = temp;
  }

  for (i=0; i<sizeof(numbers); i++) {
    if(numbers[i]>= *largest){
      *largest = numbers[i];
      *next_largest = *largest;
    }
    else if ( numbers[i] > *next_largest){
      *next_largest = numbers[i];
    }
  }
}

int main() {
  int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
  int len = 3;
  int largest, next_largest;

  //==>??? printf("%d %d", largest_two(numbers, len, &largest, &next_largest));

  }

Sides' from the pointer issues (you should read a tutorial / book on them), your main problem is that you're attempting to print the single return value of a function with return type void which means it won't return at all. 从指针发出的副本(你应该阅读它们的教程/书),你的主要问题是你试图打印一个返回类型为void的函数的单个返回值,这意味着它根本不会返回。

Your code: 你的代码:

int main() {
  int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
  int len = 10; // sizeof(numbers)
  int largest, next_largest;
  largest_two(numbers, len, &largest, &next_largest);
  printf("%d %d", largest, next_largest);
  }

Keep in mind this is still not entirely correct, but it does adress your problem of printing the numbers. 请记住,这仍然不完全正确,但它确实解决了打印数字的问题。

Also, passing len means you shouldn't do this for (i=0; i<sizeof(numbers); i++) but this instead for (i=0; i<len; i++) 另外,传递len意味着你不应该for (i=0; i<sizeof(numbers); i++)这样做,而是for (i=0; i<len; i++)

Firstly, this line: 首先,这一行:

for (i=0; i<sizeof(numbers); i++)

is not correct. 是不正确的。 You want this to be instead: 你想要这样做:

for (i=0; i<len; i++)

which should be passed to largest_two() as sizeof numbers/sizeof numbers[0] , which is the actual length of the array. 应该传递给largest_two()作为sizeof numbers/sizeof numbers[0] ,这是数组的实际长度。

I also suggest setting largest and next_largest to INT_MIN from <limits.h> , and then finding these values from their. 我还建议从<limits.h>设置largestnext_largestINT_MIN ,然后从它们中找到这些值。 It seems you are also having trouble with pointers, and it would be best to use them only when needed. 看起来你也遇到指针问题,最好只在需要时才使用它们。

Here is an example which simplifies your approach, which finds the largest and second largest element in one loop of the array. 这是一个简化您的方法的示例,它在数组的一个循环中找到最大和第二大的元素。 It also only uses pointers when needed. 它也只在需要时使用指针。

Code: 码:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define ARRAYSIZE(x) (sizeof x/sizeof x[0])

void largest_two(int numbers[], size_t len, int *largest, int *next_largest);

int main(void) {
    int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
    int largest, next_largest;

    largest_two(numbers, ARRAYSIZE(numbers), &largest, &next_largest);

    printf("largest = %d\nnext_largest = %d\n", largest, next_largest);
}   

void largest_two(int numbers[], size_t len, int *largest, int *next_largest) {
    int max, smax;

    max = smax = INT_MIN;
    for (size_t i = 0; i < len; i++) {
        if (numbers[i] > max) {
            smax = max;
            max = numbers[i];
        } else if (numbers[i] > smax && numbers[i] < max) {
            smax = numbers[i];
        }
    }
    *largest = max;
    *next_largest = smax;
}

Output: 输出:

largest = 8
next_largest = 6

Second dataset: 第二个数据集:

int numbers[] = {3, 1, 6, 3, 6, 2, 8, 0, 8, 7};

Output: 输出:

largest = 8
next_largest = 7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM