#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float rand_float ( float *, float * ) ;
int rand_int ( float *, float * ) ;
void min_max( float *, float *, float *, float * ) ;
int num_intero( float ) ;
void max_elem ( int , float * ) ;
int main ( ) {
int n, i, t;
float x1, x2, * vett;
vett
= ( float * ) calloc ( n
, sizeof ( float ) ) ; if ( vett == NULL) {
}
printf ( "\n interval extremes:\t " ) ; scanf ( "%f %f" , & x1
, & x2
) ;
do {
printf ( "1 for decimal random numbers, 2 for integers:\t " ) ; } while ( t != 1 && t != 2 ) ;
switch ( t) {
case 1 :
for ( i = 0 ; i < n; i++ ) {
* ( vett + i) = rand_float( & x1, & x2) ;
printf ( "%d__\t %10f\n " , i
, * ( vett
+ i
) ) ; }
max_elem( n, vett) ;
break ;
case 2 :
for ( i = 0 ; i < n; i++ ) {
* ( vett + i) = rand_int( & x1, & x2) ;
printf ( "%d__\t %10f\n " , i
, * ( vett
+ i
) ) ; }
max_elem( n, vett) ;
break ;
}
return 0 ;
}
void min_max( float * x1, float * x2, float * min, float * max) {
if ( * x1 < * x2) {
* min = * x1;
* max = * x2;
}
else {
* min = * x2;
* max = * x1;
}
}
float rand_float( float * x1, float * x2) {
float min, max;
min_max( x1, x2, & min, & max) ;
return ( ( max
- min
) * ( float ) rand ( ) / RAND_MAX
) + min
; }
int num_intero( float min) {
return ( ( min / ( int ) min) == 1 ) ? 1 : 0 ;
}
int rand_int( float * x1, float * x2) {
float min, max;
min_max( x1, x2, & min, & max) ;
if ( ( int ) min == ( int ) max) {
return ( int ) min;
}
else if ( min == 0 ) {
return ( rand ( ) % ( ( int ) ( max
) + 1 ) ) ; }
else if ( max == 0 ) {
return ( rand ( ) % ( ( int ) ( - min
) + 1 ) + min
) ; }
else if ( ( int ) min != ( int ) max) {
if ( num_intero( min) == 1 )
return ( rand ( ) % ( ( int ) ( max
- min
) + 1 ) + min
) ; else if ( num_intero( max) == 0 )
return ( rand ( ) % ( ( int ) ( max
- min
) ) + min
+ 1 ) ; else
return ( rand ( ) % ( ( int ) ( max
- min
) + 1 ) + min
+ 1 ) ; }
}
void max_elem ( int n, float * vett) {
int i= 0 , * pos, p = 0 , n_ele_pos;
float max = * ( vett) ;
pos
= ( int * ) malloc ( n
* sizeof ( int ) ) ; if ( pos == NULL) {
}
* ( pos) = 0 ;
for ( i = 1 ; i < n; i++ ) {
if ( * ( vett + i) > max) {
max = * ( vett + i) ;
p = 0 ;
* ( pos) = i;
}
else if ( * ( vett + i) == max) {
p++;
* ( pos + p) = i;
}
}
if ( p == 0 ) {
printf ( "\n max element is %f, in position %d " , max
, * pos
+ 1 ) ; }
else {
printf ( "\n max element %f, %d times in position\n " , max
, p
+ 1 ) ; for ( i = 0 ; i < p + 1 ; i++ )
printf ( "%d " , * ( pos
+ i
) + 1 ) ; }
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4KCmZsb2F0IHJhbmRfZmxvYXQgKGZsb2F0ICosIGZsb2F0ICopOwppbnQgcmFuZF9pbnQgKGZsb2F0ICosIGZsb2F0ICopOwp2b2lkIG1pbl9tYXgoZmxvYXQgKiwgZmxvYXQgKiwgZmxvYXQgKiwgZmxvYXQgKik7CmludCBudW1faW50ZXJvKGZsb2F0KTsKdm9pZCBtYXhfZWxlbSAoaW50LCBmbG9hdCopOwoKaW50IG1haW4gKCkgewogICAgaW50IG4sIGksIHQ7CiAgICBmbG9hdCB4MSwgeDIsICp2ZXR0OwogICAgCiAgICBwcmludGYgKCJhcnJheSBzaXplOlx0Iik7CiAgICBzY2FuZiAoIiVkIiwgJm4pOwogICAgCiAgICB2ZXR0ID0gKGZsb2F0KiljYWxsb2Mobiwgc2l6ZW9mKGZsb2F0KSk7CiAgICBpZih2ZXR0ID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIkJVTVxuIiApOwogICAgICAgIGV4aXQoMCk7CiAgICB9CiAgICAKICAgIHByaW50ZigiXG5pbnRlcnZhbCBleHRyZW1lczpcdCIpOwogICAgc2NhbmYgKCIlZiAlZiIsICZ4MSwgJngyKTsKICAKICAgIGRvIHsKICAgICAgICBwcmludGYoIjEgZm9yIGRlY2ltYWwgcmFuZG9tIG51bWJlcnMsIDIgZm9yIGludGVnZXJzOlx0Iik7CiAgICAgICAgc2NhbmYoIiVkIiwgJnQpOwogICAgfSB3aGlsZSh0ICE9IDEgJiYgdCAhPSAyKTsKICAgIAogICAgc3JhbmQodGltZShOVUxMKSk7CiAgICBzd2l0Y2godCkgewogICAgY2FzZSAxOgogICAgICAgIGZvcihpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICAqKHZldHQgKyBpKSA9IHJhbmRfZmxvYXQoJngxLCAmeDIpOwogICAgICAgICAgICBwcmludGYoIiVkX19cdCUxMGZcbiIsIGksICoodmV0dCArIGkpKTsKICAgICAgICB9CiAgICAgICAgbWF4X2VsZW0obiwgdmV0dCk7CiAgICAgICAgYnJlYWs7CiAgICAgICAgCiAgICBjYXNlIDI6CiAgICAgICAgZm9yKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgICoodmV0dCArIGkpID0gcmFuZF9pbnQoJngxLCAmeDIpOwogICAgICAgICAgICBwcmludGYoIiVkX19cdCUxMGZcbiIsIGksICoodmV0dCArIGkpKTsKICAgICAgICB9CiAgICAgICAgbWF4X2VsZW0obiwgdmV0dCk7CiAgICAgICAgYnJlYWs7CiAgICB9CiAgICAKICAgIGZyZWUodmV0dCk7CiAgICByZXR1cm4gMDsKfQoKdm9pZCBtaW5fbWF4KGZsb2F0KiB4MSwgZmxvYXQqIHgyLCBmbG9hdCogbWluLCBmbG9hdCogbWF4KSB7CiAgICBpZigqeDEgPCAqeDIpIHsKICAgICAgICAqbWluID0gKngxOwogICAgICAgICptYXggPSAqeDI7CiAgICB9CiAgICBlbHNlIHsKICAgICAgICAqbWluID0gKngyOwogICAgICAgICptYXggPSAqeDE7CiAgICB9Cn0KCmZsb2F0IHJhbmRfZmxvYXQoZmxvYXQqIHgxLCBmbG9hdCogeDIpIHsKICAgIGZsb2F0IG1pbiwgbWF4OwogICAgCiAgICBtaW5fbWF4KHgxLCB4MiwgJm1pbiwgJm1heCk7CiAgICByZXR1cm4gKChtYXggLSBtaW4pICogKGZsb2F0KXJhbmQoKSAvIFJBTkRfTUFYKSArIG1pbjsKfQoKaW50IG51bV9pbnRlcm8oZmxvYXQgbWluKSB7CiAgICByZXR1cm4gKChtaW4gLyAoaW50KW1pbikgPT0gMSkgPyAxIDogMDsKfQoKaW50IHJhbmRfaW50KGZsb2F0KiB4MSwgZmxvYXQqIHgyKSB7CiAgICBmbG9hdCBtaW4sIG1heDsKICAgIG1pbl9tYXgoeDEsIHgyLCAmbWluLCAmbWF4KTsKICAgIAogICAgaWYoKGludCltaW4gPT0gKGludCltYXgpIHsKICAgICAgICByZXR1cm4gKGludCltaW47CiAgICB9CiAgICBlbHNlIGlmKG1pbiA9PSAwKSB7CiAgICAgICAgcmV0dXJuIChyYW5kKCkgJSAoKGludCkobWF4KSArIDEpKTsKICAgIH0KICAgIGVsc2UgaWYobWF4ID09IDApIHsKICAgICAgICByZXR1cm4gKHJhbmQoKSAlICgoaW50KSgtbWluKSArIDEpICsgbWluKTsKICAgIH0KICAgIGVsc2UgaWYoKGludCltaW4gIT0gKGludCltYXgpIHsKICAgICAgICBpZihudW1faW50ZXJvKG1pbikgPT0gMSkKICAgICAgICAgICAgcmV0dXJuIChyYW5kKCkgJSAoKGludCkobWF4IC0gbWluKSArIDEpICsgbWluKTsKICAgICAgICBlbHNlIGlmKG51bV9pbnRlcm8obWF4KSA9PSAwKQogICAgICAgICAgICByZXR1cm4gKHJhbmQoKSAlICgoaW50KShtYXggLSBtaW4pKSArIG1pbiArIDEpOwogICAgICAgIGVsc2UKICAgICAgICAgICAgcmV0dXJuIChyYW5kKCkgJSAoKGludCkobWF4IC0gbWluKSArIDEpICsgbWluICsgMSk7CiAgICB9Cn0KCnZvaWQgbWF4X2VsZW0gKGludCBuLCBmbG9hdCogdmV0dCkgewogICAgaW50IGk9MCwgKnBvcywgcCA9IDAsIG5fZWxlX3BvczsKICAgIGZsb2F0IG1heCA9ICoodmV0dCk7CiAgICAKICAgIHBvcyA9IChpbnQqKW1hbGxvYyhuICogc2l6ZW9mKGludCkpOwogICAgaWYocG9zID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIkJVTVxuIik7CiAgICAgICAgZXhpdCgwKTsKICAgIH0KICAKICAgICoocG9zKSA9IDA7CiAgICBmb3IoaSA9IDE7IGkgPCBuOyBpKyspIHsKICAgICAgICBpZigqKHZldHQgKyBpKSA+IG1heCkgewogICAgICAgICAgICBtYXggPSAqKHZldHQgKyBpKTsKICAgICAgICAgICAgcCA9IDA7CiAgICAgICAgICAgICoocG9zKSA9IGk7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYoKih2ZXR0ICsgaSkgPT0gbWF4KSB7CiAgICAgICAgICAgIHArKzsKICAgICAgICAgICAgKihwb3MgKyBwKSA9IGk7CiAgICAgICAgfQogICAgfQogICAgCiAgICBpZihwID09IDApIHsKICAgICAgICBwcmludGYoIlxubWF4IGVsZW1lbnQgaXMgJWYsIGluIHBvc2l0aW9uICVkICAiLCBtYXgsICpwb3MgKyAxKTsKICAgIH0KICAgIGVsc2UgewogICAgICAgIHByaW50ZigiXG4gbWF4IGVsZW1lbnQgJWYsICVkIHRpbWVzIGluIHBvc2l0aW9uXG4iLCBtYXgsIHAgKyAxKTsKICAgICAgICBmb3IoaSA9IDA7IGkgPCBwICsgMTsgaSsrKQogICAgICAgICAgICBwcmludGYoIiVkICAiLCAqKHBvcyArIGkpICsgMSk7CiAgICB9CiAgICAKICAgIHByaW50ZigiXG5cbiIpOwogICAgZnJlZShwb3MpOwp9