aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/duncan-c-white/C/ch-2.c
blob: 7e26d293b03e9ba3730c32d21e347484bfed557d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Task 2: H-Index
// 
// C version.
// 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include "args.h"
#include "parseints.h"
#include "printarray.h"


int intcompare( const void *ap, const void *bp )
{
	int a = *((int *)ap);
	int b = *((int *)bp);
	return b-a;
}


int main( int argc, char **argv )
{
	int argno = process_flag_n_m_args( "h-index", argc, argv,
		1, 1000, "intlist" );

	int nel;
	int *list = parse_int_args( argc, argv, argno, &nel );

	if( debug )
	{
		printf( "debug: initial list: " );
		print_int_array( 60, nel, list, ',', stdout );
		putchar( '\n' );
	}

	qsort( list, nel, sizeof(int), &intcompare );

	if( debug )
	{
		printf( "debug: sorted list: " );
		print_int_array( 60, nel, list, ',', stdout );
		putchar( '\n' );
	}

	int h = -1;

	for( int pos=0; pos<nel; pos++ )
	{
		if( list[pos] >= pos+1 ) h = pos+1;
	}

	printf( "%d\n", h );

	free( list );

	return 0;
}