简体   繁体   中英

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.

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++)

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.

I also suggest setting largest and next_largest to INT_MIN from <limits.h> , and then finding these values from their. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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