#include "func.h"

void func(int x, int (*massive)[COL]){
    int r, c, i, t, ixac, ixar, ixbc, ixbr, one, two;
    int a, b, min, max;
    int inbuff = 0, null = 0;
    int buff[5];

    switch(x){
        case 31:
            clear();
            seeMassive(massive);
            printf("\nSearch min and max in massive:");
            min = max = a = *(*(massive)); ixar = 0; ixac = 0;
            for(c = 0; c < COL; c++){
                for(r = 0; r < ROW; r++){
                    a = *(*(massive + c) + r);
                    if(a < min){ min = a; ixac = c; ixar = r; }
                    if(a > max){ max = a; ixbc = c; ixbr = r; }
                }
            }
            printf("\n\tResult: Min:[%lc] | Index [%d;%d]\nMax:[%lc] | Index:[%d;%d]", min, ixac, ixar, max, ixbc, ixbr);
            inp;
        break;

        case 32:
            clear();
            seeMassive(massive);
            printf("\nSearch max in every row and column:");
            b = a = *(*(massive));
            for(c = 0; c < COL; c++){
                for(r = 0; r < ROW; r++){
                    a = *(*(massive + c) + r);
                    if(a > b){ b = a; ixac = c; ixar = r; }
                }
                printf("\n\tColumn [%d] | Max: %lc | Index [%d;%d]", c, b, ixac, ixar);
            }
            b = a = *(*(massive));
            for(r = 0; r < ROW; r++){
                for(c = 0; c < COL; c++){
                    a = *(*(massive + c) + r);
                    if(a > b){ b = a; ixac = c; ixar = r; }
                }
                printf("\n\tRow [%d] | Max: %lc | Index [%d;%d]", r, b, ixac, ixar);
            }
            inp;
        break;

        case 33:
            clear();
            seeMassive(massive);
            printf("\nSearch [] > input");
            inp;
            if((i = buffinput(&null, &inbuff, buff)) == -1){ flush(); return; }
            a = *buff;
            for(c = 0; c < COL; c++){
                for(r = 0; r < ROW; r++){
                    b = *(*(massive + c) + r);
                    if(b > a)
                        printf("\n\tResult: [%lc] > %lc | Index [%d;%d]", b, a, c, r);
                }
            }
            inp;
        break;

        case 34:
            clear();
            seeMassive(massive);
            printf("\nSearch [] < input");
            inp;
            if((i = buffinput(&null, &inbuff, buff)) == -1){ return; }
            a = *buff;
            for(c = 0; c < COL; c++){
                for(r = 0; r < ROW; r++){
                    b = *(*(massive + c) + r);
                    if(b < a)
                        printf("\n\tResult: [%lc] < %lc | Index [%d;%d]", b, a, c, r);
                }
            }
            inp;
        break;

        case 35:
            t = 0;
            do{
                clear();
                seeMassive(massive);
                printf("\nSearch [] > input in Columns Between m and l");
                finp; printf("k m l\t m & l should be 0-9\n");
                finp;
                if((i = buffinput(&null, &inbuff, buff)) == -1){ return; }
                a = *buff;
                one = *(buff + 2);
                two = *(buff + 4);
                if(one > two){ one = -1; }
            } while(!(two >= '0' && two <= '9') && !(one >= '0' && one <= '9'));
            printf("\nSearch [] > %lc in Columns %lc-%lc", a, one, two);
            for(; one <= two; one++){
                for(r = 0; r < ROW; r++){
                    b = *(*(massive + (one - '0')) + r);
                    if(b > a){
                        printf("\n\tResult: [%lc] > %lc | Index [%d;%d]", b, a, one - '0', r); t++;
                    }
                }
            }
            if(t == 0) printf("\n\t[] > %lc not found.", a);
            inp;
        break;

        case 36:
            t = 0;
            do{
                clear();
                seeMassive(massive);
                printf("\nSearch [] > input in Rows Between m and l");
                finp; printf("k m l\t m & l should be 0-9\n");
                finp;
                if((i = buffinput(&null, &inbuff, buff)) == -1){ return; }
                a = *buff;
                one = *(buff + 2);
                two = *(buff + 4);
                if(one > two){ one = -1; }
            } while(!(two >= '0' && two <= '9') && !(one >= '0' && one <= '9'));
            printf("\nSearch [] > %lc in Rows %lc-%lc", a, one, two);
            for(; one <= two; one++){
                for(c = 0; c < COL; c++){
                    b = *(*(massive + c) + (one - '0'));
                    if(b > a){
                        printf("\n\tResult: [%lc] > %lc | Index [%d;%d]", b, a, c, one - '0'); t++;
                    }
                }
            }
            if(t == 0) printf("\n\t[] > %lc not found.", a);
            inp;
        break;

        case 37:
            t = 0;
            do{
                clear();
                seeMassive(massive);
                printf("\nSearch [] < input in Columns Between m and l");
                finp; printf("k m l\t m & l should be 0-9\n");
                finp;
                if((i = buffinput(&null, &inbuff, buff)) == -1){ return; }
                a = *buff;
                one = *(buff + 2);
                two = *(buff + 4);
                if(one > two){ one = -1; }
            } while(!(two >= '0' && two <= '9') && !(one >= '0' && one <= '9'));
            printf("\nSearch [] < %lc in Columns %lc-%lc", a, one, two);
            for(; one <= two; one++){
                for(r = 0; r < ROW; r++){
                    b = *(*(massive + (one - '0')) + r);
                    if(b < a){
                        printf("\n\tResult: [%lc] < %lc | Index [%d;%d]", b, a, one - '0', r); t++;
                    }
                }
            }
            if(t == 0) printf("\n\t[] < %lc not found.", a);
            inp;
        break;

        case 38:
            t = 0;
            do{
                clear();
                seeMassive(massive);
                printf("\nSearch [] < input in Rows Between m and l");
                finp; printf("k m l\t m & l should be 0-9\n");
                finp;
                if((i = buffinput(&null, &inbuff, buff)) == -1){ return; }
                a = *buff;
                one = *(buff + 2);
                two = *(buff + 4);
                if(one > two){ one = -1; }
            } while(!(two >= '0' && two <= '9') && !(one >= '0' && one <= '9'));
            printf("\nSearch [] < %lc in Rows %lc-%lc", a, one, two);
            for(; one <= two; one++){
                for(c = 0; c < COL; c++){
                    b = *(*(massive + c) + (one - '0'));
                    if(b < a){
                        printf("\n\tResult: [%lc] < %lc | Index [%d;%d]", b, a, c, one - '0'); t++;
                    }
                }
            }
            if(t == 0) printf("\n\t[] < %lc not found.", a);
            inp;
        break;

        case 39:
            clear();
            seeMassive(massive);
            printf("\nSearch all [] between Min & Max in massive.");
            min = max = a = *(*(massive)); ixac = 0; ixar = 0;
            for(c = 0; c < COL; c++){
                for(r = 0; r < ROW; r++){
                    a = *(*(massive + c) + r);
                    if(a < min){ min = a; ixac = c; }
                    if(a > max){ max = a; ixar = r; }
                }
            }
            for(c = 0; c < COL; c++){
                for(r = 0; r < ROW; r++){
                    if((*(*(massive + c) + r)) >= min && (*(*(massive + c) + r) <= max)){
                        printf("\n\tResult: [%lc] | Index [%d|%d]", *(*(massive + c) + r), c, r);            
                    }
                }
            }
            inp;
        break;

        case 310:
    clear();
    seeMassive(massive);
    int max = *(*(massive));
    for(c = 0; c < COL; c++){
        for(r = 0; r < ROW; r++){
            if(*(*(massive + c) + r) > max)
                max = *(*(massive + c) + r);
        }
    }

    do {
        printf("\nInput k <= max value %lc: ", max);
        finp;
        if((i = buffinput(&null, &inbuff, buff)) == -1){ return; }
        a = *buff;
        if(a > max){
            printf("\nError: k > max value.  Try again.");
        }
    } while(a > max);
    for(c = 0; c < COL; c++){
        for(r = 0; r < ROW; r++){
            if(*(*(massive + c) + r) % a == 0){
                printf("\nResult: [%lc] | Index [%d;%d]", *(*(massive + c) + r), c, r);
    }}}
    inp;
    break;
    }
}



void func2(int x, int (*massive)[COL]){
    int r,c,sum,i;
    int inbuff = 0, null = 0;
    int buff[5];
    int k;

    switch(x){

        case 311:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of even []");
            for (c = 0; c < COL; c++){
                for (r = 0; r < ROW; r++){
                    if ((*(*(massive + c) + r)) % 2 == 0)
                        sum += *(*(massive + c) + r);
                }
            }
            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 312:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of odd []");
            for (c = 0; c < COL; c++){
                for (r = 0; r < ROW; r++){
                    if ((*(*(massive + c) + r)) % 2 != 0)
                        sum += *(*(massive + c) + r);
                }
            }
            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 313:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of []%%k");
            finp;

            if ((i = buffinput(&null, &inbuff, buff)) == -1) return;
            k = *buff;    

            for (c = 0; c < COL; c++){
                for (r = 0; r < ROW; r++){
                    if ((*(*(massive + c) + r)) % k == 0)
                        sum += *(*(massive + c) + r);
                }
            }
            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 314:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] column%%2==0");

            for (c = 0; c < COL; c += 2){
                for (r = 0; r < ROW; r++){
                    sum += *(*(massive + c) + r);
                }
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 315:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] column%%2!=0");

            for (c = 1; c < COL; c += 2){
                for (r = 0; r < ROW; r++){
                    sum += *(*(massive + c) + r);
                }
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 316:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] rows%%2==0");

            for (c = 0; c < COL; c++){
                for (r = 0; r < ROW; r += 2){
                    sum += *(*(massive + c) + r);
                }
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 317:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] rows%%2!=0");

            for (c = 0; c < COL; c++){
                for (r = 1; r < ROW; r += 2){
                    sum += *(*(massive + c) + r);
                }
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 318:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] above main diagonal");

            for (c = 0; c < COL; c++){
                for (r = 0; r <= c; r++){
                    sum += *(*(massive + c) + r);
                }
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 319:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] below main diagonal");

            for (c = 0; c < COL; c++){
                for (r = c; r < ROW; r++){
                    sum += *(*(massive + c) + r);
                }
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;


        case 320:
            sum = 0;
            clear();
            seeMassive(massive);
            printf("\nSumm of [] on both diagonals");

            for (c = 0; c < COL; c++){
                sum += *(*(massive + c) + c); 
                sum += *(*(massive + c) + (ROW - 1 - c));
            }

            printf("\n\tResult: sum %d", sum);
            inp;
        break;
    }
}

