#include "func.h"

void range(int type,int size, int *massive){
    int x, k, m;

switch(type){
    case 1:
        k = massive[0];
        m = massive[0];
        printf("\tFinding max and min int in massive.\n");
    
        for(int i = 0;i<size;i++){
            m = (m>massive[i])?m:massive[i];
            k = (k<massive[i])?k:massive[i];
            }
        printf("Max massive int is %d.\n",m);
        printf("Min massive int is %d.\n\n",k);
        break;
    


    case 2:
        printf("\tEnter Range where find max int.\n");
        printf("Input: min max\nInput: ");
        while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){ 
            if(k<0)printf("min cannot be below 0!\n");
            if(m>size)printf("max cannot be bigger %d!\n",size);
            if(k>m)printf("min cannot be bigger than max!\n");
            if(flush())return;
            printf("Input: ");
            }
        if(flush())return;
        x=massive[k];
        for(int i = k;i<=m;i++){
            x = (x>massive[i])?x:massive[i];
            }
        printf("Max massive int betveen [%d] and [%d] is %d.\n\n",k,m,x);
        break;
        

    case 3:
        printf("\tEnter Range where find min int.\n");
        printf("Input: min max\nInput: ");
        while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1 || k>m){ 
            if(k<0)printf("min cannot be below 0!\n");
            if(m>size)printf("max cannot be bigger %d!\n",size);
            if(k>m)printf("min cannot be bigger than max!\n");
            if(flush())return;
            }   
        if(flush())return;
        x = massive[k];
        for(int i = k;i<=m;i++){
            x = (x<massive[i])?x:massive[i];
            }
        printf("Min massive int betveen [%d] and [%d] is %d.\n\n",k,m,x);
        return;
    

    case 4:
        printf("\tInput int to find all higher int.\n");
        printf("\nInput: ");
        while(scanf(" %d", &k)!=1)
            if(flush())return;
        if(flush())return;
        x = 0;
        for(int i = 0;i<size;i++){
            if (massive[i]>k){printf("[%3d]\t{%p}\n",i,&massive[i]);x++;}
            }
        if(x==0)printf("int > %d not found.",k);
        break;
    

    case 5:
        printf("\tInput int to find all lower int.\n");
        printf("\nInput: ");
        while(scanf(" %d", &k)!=1)
            if(flush())return;
        if(flush())return;
        x = 0;
        for(int i = 0;i<size;i++){
            if (massive[i]<k){printf("[%3d]\t{%p}\n",i,&massive[i]);x++;}
            }
        if(x==0)printf("int < %d not found.",k);
        break;

    case 16:
    case 10:
        printf("\tEnter Range.\n");
        printf("Input: min max\nInput: ");
        while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){ 
            if(k<0)printf("min cannot be below 0!\n");
            if(m>size)printf("max cannot be bigger %d!\n",size);
            if(k>m)printf("min cannot be bigger than max!\n");
            if(flush())return;
            printf("Input: ");
            }
        if(flush())return;
        x=massive[k];
        for(int i = k;i<=m;i++){
            x += massive[i];
            }
        printf("Sum betveen [%d] and [%d] is %d.\n\n",k,m,x);
        break;

    case 11:
    case 12:
        printf("\tEnter Range where find arithmetic mean\\n.\n");
        printf("Input: min max\nInput: ");
        while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){ 
            if(k<0)printf("min cannot be below 0!\n");
            if(m>size)printf("max cannot be bigger %d!\n",size);
            if(k>m)printf("min cannot be bigger than max!\n");
            if(flush())return;
            printf("Input: ");
            }
        if(flush())return;
        x=massive[k];int c=0;
        for(int i = k;i<=m;i++){
            x += massive[i];
            c++;
            }
        x /= c; 
        printf("arithmetic mean betveen [%d] and [%d] is %d.\n\n",k,m,x);
        break;


    case 13:
        k = massive[0];
        m = massive[0];
        printf("\tFinding max and min int in massive.\n");
    
        for(int i = 0;i<size;i++){
            m = (m>massive[i])?m:massive[i];
            k = (k<massive[i])?k:massive[i];
            }
        for(int i = k;i<m;i++){
            printf("[%3d]\t{%p}\n",i,&massive[i]);
        }
        break;

    case 17:
        printf("\tInput Range to find index in between\\n.\n");
        printf("Input: min max\nInput: ");
        while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){ 
            if(k<0)printf("min cannot be below 0!\n");
            if(m>size)printf("max cannot be bigger %d!\n",size);
            if(k>m)printf("min cannot be bigger than max!\n");
            if(flush())return;
            printf("Input: ");
            }
        if(flush())return;
        for(int i = k;i<m;i++){
            printf("[%3d]\t{%p}\n",i,&massive[i]);
            }   
        break;

    case 18:
        printf("\tInput Range to find diff between min and max\\n.\n");
        printf("Input: min max\nInput: ");
        while(scanf(" %d %d", &k, &m)!=2 || k<0 || m>size-1|| k>m){ 
            if(k<0)printf("min cannot be below 0!\n");
            if(m>size)printf("max cannot be bigger %d!\n",size);
            if(k>m)printf("min cannot be bigger than max!\n");
            if(flush())return;
            printf("Input: ");
            }
        if(flush())return;
        k = massive[k];
        m = massive[k];
        for(int i = k;i<m;i++){
            m = (m>massive[i])?m:massive[i];
            k = (k<massive[i])?k:massive[i];
        }
        printf("min - %d\tmax - %d\tdiff - %d\n",k,m,m-k);
        break;
    }
return;
}
