#include "func.h"

void find(int type, int size, int *massive){

int x,k,m,sum;
switch(type){

    case 6:
    case 8: 
        for (int i = size - 1; i>=0;i--)
        if(massive[i]>0){
        printf("last positive int: ");
        printf("[%3d]\t{%p}\t%d\n",i,&massive[i],massive[i]);
        break;
        }
        printf("positive int is not found.\n");
        break;
    

    case 7:
    case 9: 
        for (int i = size; i>=0;i--)
        if(massive[i]<0){
        printf("last negative int: ");
        printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
        break;
        }
        printf("negative int is not found.\n");
        break;
    
    
    case 14:
        x=k=0;
        printf("find all modules of k\nInput: ");
        while(scanf(" %d",&k)!=1||k>size-1){
        if(k>size-1)printf("k shood be lower than %d",size);
        if(flush())return;
            }
        if(flush())return;
        for(int i = 0; i<size;i++){
            if(massive[i]%k==0){
                printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);x++;}
        }
        printf("Num of modules - %d",x);
        break;


    case 15:
        x=0;k=1;m=0;
        for(int i = 0;i<size-1;i++){
            for(int j = i+1;j<size-1;j++){
              if(massive[i] == massive[j]){
                k=i;
                m=j;
                break;}
           }
            if (k==m)break;
        }
        for(int i = k;i<m;i++)
            x++;
        
        if(k==m)
            printf("elements between [%d] and [%d] - %d",k,m,x);
        else printf("Error same elements not found!\n");
        break;

    case 19:
    case 21:
    case 23:
        sum=0;
        printf("find all int that lower k\nInput: ");
        while(scanf(" %d",&k)!=1){
        if(flush())return;
            }
        if(flush())return;
        for(int i = 0; i<size;i++){
            if(k<massive[i]){
                if(type != 21)
                    printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
                x++;
                sum+=massive[i];    
            }
        }
        if(type == 21)printf("summ - %d\n",sum);
        break;

    case 20:
    case 22:
    case 24:
        sum=0;
        printf("find all int higher lower k\nInput: ");
        while(scanf(" %d",&k)!=1){
        if(flush())return;
            }
        if(flush())return;
        for(int i = 0; i<size;i++){
            if(k>massive[i]){
                if(type != 21)
                    printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
                x++;
                sum+=massive[i];    
            }
        }
        if(type == 21)printf("summ - %d\n",sum);
        break;

    case 25:
    case 27:
    case 29:
        sum=0;
        printf("find all int higher lower mean\nInput: ");
        for(int i = 0; i<size;i++){
        sum +=massive[i];
        x++;
        }
        k = sum/x;
        x = sum = 0;
        for(int i = 0; i<size;i++){
            if(k>massive[i]){
                if(type != 21)
                    printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
                x++;
                sum+=massive[i];    
            }
        }
        if(type == 29)printf("summ - %d\n",sum);
        break;

    case 26:
    case 28:
    case 30:
        sum=0;
        printf("find all int higher lower mean\nInput: ");
        for(int i = 0; i<size;i++){
        sum +=massive[i];
        x++;
        }
        k = sum/x;
        x = sum = 0;
        for(int i = 0; i<size;i++){
            if(k<massive[i]){
                if(type != 21)
                    printf("[%3d]{%p}\t%d\n",i,&massive[i],massive[i]);
                x++;
                sum+=massive[i];    
            }
        }
        if(type == 30)printf("summ - %d\n",sum);
        break;


    }
    return;
}
