Home > front end >  How to generate a matrix with certain areas appended certain numbers in C programming?
How to generate a matrix with certain areas appended certain numbers in C programming?

Time:09-25

int main(int argc, char *argv[]){
    int grid_u[51][51];
    int grid_v[51][51];
    int u[] = {0,1};
    int v[] = {0,1};
    int i,j;
    int n = 0;
    for (i = 0; i < 10; i  ){
        for (j = 0; j < 10; j  ){
            grid_u[i][j] = u[1];
            grid_v[i][j] = v[0];
        }
    }
    for (i = 10;i <= 20; i  ){
        for (j = 10;j <= 20; j  ){
        grid_u[i][j] = u[0];
        grid_v[i][j] = v[1];
        }
    }
    for (i = 21; i <= 50; i  ){
        for (j = 21; j <= 50; j  ){
            grid_u[i][j] = u[1];
            grid_v[i][j] = v[0];
        }
    }
    for (i = 0; i <= 50; i  ){
        for (j = 0; j <= 50; j  , n  ){
            if (n % 51 == 0){
                printf("\n");
            }
            printf("%d ", grid_u[i][j]);
        }
        printf("\n");
    }
    for (i = 0; i <= 50; i  ){
        for (j = 0; j <= 50; j  , n  ){
            if (n % 51 == 0){
                printf("\n");
            }
            printf("%d ", grid_v[i][j]);
        }
        printf("\n");
    }
}

Like the code above, I want to create two 51*51 matrices with certain areas a box indexed from 10~20 appended certain numbers, which is an 11*11 squared box. But the results are pretty weird, can somebody help me figure it out? Very appreciated. The expected matrix is like this:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

But my output is:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -389530064 32766 201198390 1 0 0 539592928 32767 0 -507641278 539592928 32767 539592928 32767 539592928 32767 0 0 0 0 -389529936 32766 201195565 1 0 0 0 0 0 

0 0 0 0 0 0 0 -389527408 32766 539592928 32767 446038026 -2119743701 539592928 32767 539592928 32767 539592928 32767 0 0 -389527408 32766 -389528784 32766 201194920 1 0 0 0 0 0 0 0 0 539592928 32767 0 0 -389527408 32766 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -389529648 32766 201198390 1 -389529632 32766 538241768 32767 0 -11761900 538241768 32767 538241768 32767 538241768 32767 -389529584 32766 201198390 1 -389529520 32766 710482432 32767 0 -1690551536 710482432 

32767 710482432 32767 710482432 32767 0 0 0 0 -389529456 32766 201195565 1 446038026 -2119743701 538241768 32767 538241768 32767 538241768 32767 0 0 0 0 -389526928 32766 710482432 32767 446038026 -2119743701 710482432 32767 710482432 32767 710482432 32767 0 0 -389526928 32766 -389528304 32766 201194920 1 -389526976 32766 0 0 0 0 

0 0 710482432 32767 0 0 -389526928 32766 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -389529184 32766 201198390 1 0 0 538145032 32767 0 -886581335 538145032 32767 538145032 

32767 538145032 32767 0 0 0 0 -389529056 32766 201195565 1 -389529088 32766 201198390 1 0 0 541141784 32767 0 -1288945918 541141784 32767 541141784 32767 541141784 32767 0 0 0 0 -389528960 32766 201195565 1 0 0 -389526528 32766 -389527904 32766 201194920 1 0 0 0 0 -389526432 32766 541141784 32767 

446038026 -2119743701 541141784 32767 541141784 32767 541141784 32767 0 0 -389526432 32766 -389527808 32766 201194920 1 0 0 0 0 0 0 0 0 541141784 32767 0 0 -389526432 32766 0 0 0 0 0 0 0 0 0 0 32 0 135296 0 446038026 -2119743701 539592928 32767 539592928 32767 -389527408 

32766 0 0 539592928 32767 -389528704 32766 201192449 1 9 0 0 0 446038026 -2119743701 539592928 32767 0 0 9 0 65536 0 539592928 32767 -389528576 32766 201157890 1 0 5 132858 133013 133035 135150 135254 135274 -389528576 32766 0 0 -389527408 32766 539592928 32767 446038026 -2119743701 539592928 32767 -389527408 32766 

9 0 65536 0 539592928 32767 -389527424 32766 201156681 1 0 0 0 0 0 0 0 0 0 0 0 0 -389525920 32766 616766208 26 201842144 1 616766208 32767 -389527104 32766 201814680 1 0 0 -389525920 32766 -389527296 32766 201194920 1 -389528352 32766 201198390 1 446038026 -2119743701 538241768 32767 538241768 

32767 -389526992 32766 0 0 538241768 32767 -389528288 32766 201192449 1 446038026 -2119743701 710482432 32767 710482432 32767 -389526928 32766 0 0 710482432 32767 -389528224 32766 201192449 1 -389528160 32766 201157890 1 446038026 -2119743701 710482432 32767 0 0 12 0 65536 0 710482432 32767 -389528096 32766 201157890 1 446038026 -2119743701 538241768 32767 

-389526992 32766 11 0 65536 0 0 0 -389526928 32766 710482432 32767 446038026 -2119743701 710482432 32767 -389526928 32766 12 0 65536 0 710482432 32767 -389526944 32766 201156681 1 201837632 1 537923568 27 -389526640 32766 201814680 1 -389526640 32766 201814680 1 -389527904 32766 201195565 27 201841856 1 540166192 32767 -389526544 32766 201814680 

1 201846864 1 0 0 -389525376 32766 537923568 32767 446038026 -2119743701 538145032 32767 538145032 32767 -389526528 32766 0 0 538145032 32767 -389527824 32766 201192449 1 -389527808 32766 201198390 1 446038026 -2119743701 538145032 32767 446038026 -2119743701 541141784 32767 541141784 32767 -389526432 32766 0 0 541141784 32767 -389527728 32766 201192449 1 -389527664 32766 

201195565 1 446038026 -2119743701 541141784 32767 0 0 13 0 65536 0 541141784 32767 -389527600 32766 201157890 1 65536 0 538145032 32767 -389526544 32766 201156681 1 -389526528 32766 0 0 -389526432 32766 541141784 32767 446038026 -2119743701 541141784 32767 -389526432 32766 13 0 65536 0 541141784 32767 -389526448 32766 201156681 1 -389527456 

32766 201195565 1 -389527488 32766 201198390 1 -389527408 32766 578869728 32767 0 -1288945918 578869728 30 201842416 1 578869728 32767 -389526032 32766 201814680 1 -389527360 32766 201195565 1 446038026 -2119743701 201842928 1 201842144 0 9 0 65536 0 -389527144 32766 446038026 -2119743701 9 0 201842144 1 -389527120 32766 0 0 201814680 1 

-389526960 32766 201237120 1 539592360 32767 2 65536 14942208 -2130640895 539592416 32767 2 65536 55836928 1 539592472 32767 2 65536 471567399 1 539592536 32767 2 65536 16666626 1 539592608 32767 2 65536 -389527152 32766 538248373 32767 -2029124312 32767 -2140435368 32767 -2140435352 32767 -2140435320 32767 0 0 -2140435664 32767 0 0 -2140434248 

32767 -389527072 32766 538248373 32767 -2029124312 32767 -2140435368 32767 -2140435352 32767 -2140435320 32767 0 0 -2140435664 32767 0 0 -2140434248 32767 -389526992 32766 538248373 32767 -2029124312 32767 -389526976 32766 -389526960 32766 -389526928 32766 0 0 -2140434248 32767 -389526992 32766 540045747 32767 0 0 -389526512 32766 -389526400 32766 538250950 32767 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 538902282 32767 0 0 5120 0 1 0 2 0 116988416 1 116993536 1 -389526464 32766 538901088 32767 -1073737726 -157745152 -2140405761 32767 1530938464 32721 116961280 1 -389526544 32766 538904623 32767 

0 0 538902282 32767 0 0 5120 0 1 0 2 0 116988416 1 116993536 1 -389526336 32766 538901088 32767 -1073737726 -157745152 539492351 32767 1530938464 32721 116961280 1 -389526416 32766 538904623 32767 0 0 538902282 32767 0 0 5120 0 1 0 2 0 116988416 1 116993536 1 -389526208 32766 538901088 

32767 -1073737726 -157745152 539492351 32767 1530938464 32721 116961280 1 -389526288 32766 538904623 32767 0 0 1530938464 32721 -389526224 32766 538919169 32767 -2146959359 0 1530938464 32721 1530938464 32721 -2140581792 32767 1 0 0 0 -389526096 32766 539453250 32767 -389526160 32766 538895975 32767 -2140323840 32767 32 0 0 0 2 0 -389526096 32766 

539457653 32767 -2140581712 32767 1535149624 32721 0 0 -2029115376 32767 2 0 0 0 -389525968 32766 -2140581672 32767 1530938432 32721 116961280 1 -389526032 32766 538904623 32767 0 0 1530938432 32721 -389525968 32766 538919169 32767 -2146959360 0 1530938432 32721 1530938432 32721 -2140581752 32767 -389525696 32766 0 0 -389525840 32766 539453250 32767 0 

It's pretty messy and I cannot find why this came out like this. It seems like the appending process in for loop does not act in expected order so that it returns a lot of large memory addresses...

CodePudding user response:

You are not explicitly setting all the entries in the arrays. Local variables are not automatically initialised so they contain garbage until they are set. Change the array declarations to be the following to initialise them to all zeroes:

int grid_u[51][51] = { 0 };
int grid_v[51][51] = { 0 };
  •  Tags:  
  • c
  • Related