All pastes #2063706 Raw Edit

Yury

public c v1 · immutable
#2063706 ·published 2011-05-18 16:20 UTC
rendered paste body
/*	(C) 2011, yury@nix.co.il*/#include <stdio.h>#define LARGEST_COUNT 5typedef struct {	int value;	int position;}  val_pos_type;int val_pos_cmp(const void *a, const void *b) {     const val_pos_type *vpa = (const val_pos_type *)a; // casting pointer types     const val_pos_type *vpb = (const val_pos_type *)b;    return vpa->value - vpb->value; }  int is_neighbors( val_pos_type a, val_pos_type b ) {	return ( a.position +1 == b.position || a.position -1 == b.position );}void find_largest( int arr[], int size ) {	val_pos_type largest_elements[LARGEST_COUNT];	val_pos_type *assist = (val_pos_type*)malloc( sizeof( val_pos_type ) * size );	int largest_ct = 0, i = 0, j = 0, is_nb_flag = 0;	// copy data to the helper array	for (i = 0; i < size; ++i ) {		assist[i].value = arr[i];		assist[i].position = i;	}	// sort the data	qsort( assist, size, sizeof(val_pos_type), val_pos_cmp );	//  print the sorted data	for ( i = 0; i < size; ++i ) {		   printf("%d[%d] ", assist[i].value, assist[i].position );	}	printf("\n");	// choose from the largest down, skip the neighbors	for( i = 0; i < size && largest_ct < LARGEST_COUNT; ++i ) {		is_nb_flag = 0;		for( j = 0; j < largest_ct; ++j ) {			if ( is_neighbors( assist[ size - i -1 ], largest_elements[j] ) ) {				is_nb_flag = 1;				break;			}		}		if (!is_nb_flag) {			largest_elements[ largest_ct ] = assist[ size - i -1 ];			++largest_ct;		}	}		// print the largest who are not neighbors	for ( i = 0; i < LARGEST_COUNT; ++i ) {		   printf("%d[%d] ", largest_elements[i].value, largest_elements[i].position );	}	printf("\n");	free( assist );}int main() {	int numbers_arr[] = { 3, 56, 63, 3, 4, 5, 1, 2, 325, 235, 1, 43, 42, 23 };	find_largest( numbers_arr, sizeof(numbers_arr)/sizeof(int));	return 0;}