There's a garbage value in the c language sequence

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

(Among examples of c language books studying alone) In an array written from 1 to 20, the element in the last column stores the sum of each row, and the element in the last row stores the sum of each column

#include <stdio.h>

int main(void)
{
    int ary[5][6] = {
        { 1, 2, 3, 4, 5 },
        { 6, 7, 8, 9, 10 },
        { 11, 12, 13, 14, 15 },
        { 16, 17, 18, 19, 20 }
    };
    int i, j;


    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            ary[4][j] += ary[i][j];
            ary[i][5] += ary[i][j];
            ary[4][5] += ary[i][j];
        }
    }

    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 6; j++)
        {
            printf("%5d", ary[i][j]);
        }
        printf("\n");
    }

    return 0;
}

If you initialize the array in the following way, both the run and the result will be normal

    int ary[5][6] = {
        { 1, 2, 3, 4, 5 },
        { 6, 7, 8, 9, 10 },
        { 11, 12, 13, 14, 15 },
        { 16, 17, 18, 19, 20 }
    };

However, if you initialize the array in the form below,

    int ary[5][6];
    int i, j;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            ary[i][j] = (5 * i) + j + 1;
        }
    }

The price of the garbage came out like this.

          1          2          3          4          5 -858993445
          6          7          8          9         10 -858993420
         11         12         13         14         15 -858993395
         16         17         18         19         20 -858993370
 -858993426 -858993422 -858993418 -858993414 -858993410 -858993250

And when you initialize an array like that, if you output the function in the following way, it will output well again.

#include <stdio.h>

int main(void)
{
    int ary[5][6];
    int i, j;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            ary[i][j] = (5 * i) + j + 1;
        }
    }

    int count;
    for (i = 0; i < 5; i++)
    {
        count = 0;
        for (j = 0; j < 4; j++)
        {
            count += ary[j][i];
        }
        ary[4][i] = count;
    }

    for (i = 0; i < 4; i++)
    {
        count = 0;
        for (j = 0; j < 5; j++)
        {
            count += ary[i][j];
        }
        ary[i][5] = count;
    }
    count = 0;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            count += ary[i][j];
        }
    }
    ary[4][5] = count;

    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 6; j++)
        {
            printf("%5d", ary[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Is there a rule of C language that I don't know? Masters, please help me I don't understand why the garbage is so expensive.

c

2022-09-20 11:08

1 Answers

for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            arithmetic
        }
    }

In this part, the value of i goes from 0 to 3, and j goes from 0 to 4.

Look at this

for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 6; j++)
        {
            arithmetic
        }
    }

It works well if you change it to .


2022-09-20 11:08

If you have any answers or tips


© 2022 pinfo. All rights reserved.