/**
 * assignment 4 solution.
 * 
 * test case: -2 3 -4 -200 5 -3 120 0
 * 
 */

#include <stdio.h>

int main (){
    /* variables declaration */
    int value;
    int max1 = -101; /* the biggest positive number */
    int max2 = -101; /* the secondery biggest positive number */
    int min1 =  101; /* the smallest negative number */
    int min2 =  101; /* the secondery smallest negative number */
    int pos_sum = 0; /* the sum of the positive numbers */
    int neg_sum = 0; /* the sum of the negative numbers */
    double negaverage;/* the average of all negative numbers */
    double posaverage;/* the average of all positive numbers */
    int pos_counter = 0;/* counter for the positive numbers */
    int neg_counter = 0;/* counter for the negative numbers */
    
   
    /* continuiously ask for value unless input value is 0 */
    while ( 1 ){
		  /* ask the user to input the value, 0 to exit */
		  printf ("Please input the value ranging from -100 to 100, 0 to exit: ");
		  scanf ("%d",&value);

		  if (value==0) break;

		  if (( value > 100) || (value < -100)){
    		  printf ("Invalid input value");
			  continue;
		  }  

          /* 
		   * Definetly, the value is in the range from -100 to 100 
		   */
          
            /*
			 * for positive number: calculate the positive sum, add the positive
			 * counter
			 */
             if (value > 0){          
                pos_sum = pos_sum + value;
                pos_counter++;
             }else {
            /*
			 * for negative number: calculate the negative sum, add the negative
			 * counter
			 */
               neg_sum = neg_sum  + value;
               neg_counter++;
             }         
          
            /*
			 * if the value is bigger than the current biggest value, substitute
			 * it but before that save the previous one as the second bigest value first
			 * else if the value is bigger than the secondery biggest value,
			 * substitute it
			 */
            if (value > max1){
               max2=max1;
               max1=value;
            }else if (value > max2){
               max2= value;
            }
          
            /*
			 * if the value is smaller than the current smallest value,
			 * substitute it but before that save the previous one as the second smallest value first
			 * else if the value is smaller than the secondery smallest value,
			 * substitute it
			 */
            if (value < min1){
               min2 = min1;
               min1 = value;
            }else if (value <min2){      
               min2 = value;
            }   
              
    } /* end of while */
    
    /* output the result */
    printf ("Two largest numbers are:%i, %i\n", max1, max2);
    printf ("Two smallest numbers are:%i, %i\n", min1, min2);
    
    if(neg_counter > 0){ 
      /* calculate the average of the negative numbers */
      negaverage = (double)neg_sum/neg_counter;
      printf ("Average of negative values is: %lf\n",negaverage);
    } else 
      printf("No negative value is given so avg cannot be computed\n");
    
    if(pos_counter > 0) {   
       /* calculate the average of the positive numbers */
       posaverage = (double)pos_sum/pos_counter;
       printf ("Average of positive values is: %lf\n",posaverage);
    } else 
      printf("No positive value is given so avg cannot be computed\n");

    return 0;
}
