如何找到动态矩阵中正数的最大面积? [关闭]

问题描述 投票:0回答:0

我在 C99 的动态矩阵中找不到最大的区域。最大的问题是我的代码给出了一些随机数,我不知道如何获得真实数字。

这个矩阵是随机数,是动态的。用户输入行数和列数,并使用此信息程序渲染矩阵,随机数字从 -9 到 9.

以矩阵为例

9 -1 3 6 7 8 -2 -4 -3 0 8 3 -2 1 -4 -8 9 0 -1 0

第一列第一列到第三列第一列结束的区域有3个正数,它是最大的区域。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define max_wie 15 //max numbers of rows and columns
#define max_kol 10
/*
 * 
 */
int main(int argc, char** argv) {
    int col, row;  

    printf("Number of columns (max 10): ");        
    scanf("%d", &col);
    
    printf("Number of rows (max 15): ");       
    scanf("%d", &row);

if(row > max_row) {                                    
        printf("The number of rows must be smaller than 15 \n");
        getchar();
        return(EXIT_SUCCESS);
    }
    
    if(row < 0) {                                        
        printf("The number of rows must be bigger than 0 \n");
        getchar();
        return(EXIT_SUCCESS);
    }
    
    if(col > max_col) {                                   
        printf("The number of rows must be smaller than 10 \n");
        getchar();
        return(EXIT_SUCCESS);
    }
    
    if(col < 0) {                                         
        printf("The number of rows must be biger than 0 \n");
        getchar();
        return(EXIT_SUCCESS);
    }
     
//generate matrix
int i, j;                                           
    int **Tab;                                          
    Tab = (int**)calloc(wie, sizeof(int*));     
    for(int i = 0; i < wie; i ++) {                     
        Tab[i] = (int*)calloc(kol, sizeof(int));
    }
    
    srand(time(NULL));
    
//with random numbers
    for (i = 0; i < row; i++) { 
        for (j = 0; j < col; j++) {
            Tab[i][j] = rand() %19 - 9 ;                
            printf("%d\t", Tab[i][j]);                  
        }
        printf("\n");
    }

for( i = 0; i < rows; i++ ) {
        for ( j = 0; j < col; j++ ) {
            if (Tab[i][j] > 0) {
                tem_size = 0;        // temporary size
                k = i;
                l = j;
                while (l < col && Tab[k][l]>0) {
                    tym_size++;
                    l++;
                }
                if (tem_size > size) {
                    tem_size = size;
                    start_row = i;
                    start_col = j;
                }
                l = j;
                size = 0;
                while (k < row && Tab[k][l] > 0) {
                    tem_row++;
                    k++;
                }
                if (tem_row > size) {
                    size = tem_row;
                    end_row = k;
                    end_col = l ;
                }
            }  
        }  
    }


int rec_row, rec_col;
    rec_row = abs(end_row - start_row);
    rec_col = abs(end_rol - start_col);

    printf("Found rectangle %d x %d, initial position %d,%d, final pozycion %d,%d \n", rec_row, rec_col, start_row, start_col, end_row, end_col);
    
    
    for (i = 0; i < wie; i++) {
        free(Tab[i]);
    } 
    free(Tab);
    
    getch();
    return (EXIT_SUCCESS);  
}
c loops matrix dynamic area
© www.soinside.com 2019 - 2024. All rights reserved.