/* Finding two largest values in array */

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

#define MAX_RANDOM_VALUE 1000
#define ARRAY_SIZE 10

void find_two_largest(int a[], int n, int *largest, int *second_largest) {
  for (int i = 0; i < n; i++) {
    if (a[i] > *largest) {
      *second_largest = *largest;
      *largest = a[i];
    } else if (a[i] < *largest && a[i] > *second_largest) {
      *second_largest = a[i];
    }
  }
}

int main() {
  int random_numbers_array[ARRAY_SIZE];
  int largest = 0;
  int second_largest = 0;
  // Generate random numbers
  srand(time(NULL));
  for (int i = 0; i < ARRAY_SIZE; i++) {
    random_numbers_array[i] = rand() % MAX_RANDOM_VALUE + 1;
  }
  // Print array
  printf("[");
  for (int i = 0; i < ARRAY_SIZE; i++) {
    if (i == ARRAY_SIZE - 1) {
      printf("%d", random_numbers_array[i]);
    } else {
      printf("%d,", random_numbers_array[i]);
    }
  }
  printf("]\n");
  // Exercise
  find_two_largest(random_numbers_array, ARRAY_SIZE, &largest, &second_largest);
  printf("Largest: %d\nSecond largest: %d\n", largest, second_largest);
}
