Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么更改数组大小后结果会发生变化?_Arrays_C_Algorithm_Matrix_Magic Square - Fatal编程技术网

Arrays 为什么更改数组大小后结果会发生变化?

Arrays 为什么更改数组大小后结果会发生变化?,arrays,c,algorithm,matrix,magic-square,Arrays,C,Algorithm,Matrix,Magic Square,我已经做了一个程序,将一个数字n作为输入,然后返回一个平方矩阵n*n,其属性是所有行、列和对角线都具有相同的和 该项目工作正常,我尽可能地优化它,从算法到为此使用特定的数据类型(在我的例子中,unsigned short,因为我不需要更大的存储空间) 毕竟,我试着去看表演,我想用一个更大的数字,比如100200,等等; 但当我试图更改矩阵的存储时,程序无法正常工作,返回了一个包含0的矩阵,其和很奇怪 我不明白这只虫子是从哪里来的 #include <stdio.h> #include

我已经做了一个程序,将一个数字
n
作为输入,然后返回一个平方矩阵
n*n
,其属性是所有行、列和对角线都具有相同的和

该项目工作正常,我尽可能地优化它,从算法到为此使用特定的数据类型(在我的例子中,
unsigned short
,因为我不需要更大的存储空间)

毕竟,我试着去看表演,我想用一个更大的数字,比如100200,等等; 但当我试图更改矩阵的存储时,程序无法正常工作,返回了一个包含0的矩阵,其和很奇怪

我不明白这只虫子是从哪里来的

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

unsigned short a[100][100], i = 0, j = 0, n, suma[100];

void next_b(unsigned short *i, unsigned short *j);   // find the properly i and j
void completare(unsigned short i, unsigned short j); // completes the matrix after i find the i and j
void tipar();                                        // print the matrix

int suma_linie(unsigned short x);   //sum of a row
int suma_coloana(unsigned short y); //sum of a column
int suma_diagonala_principala();    //first diagonal
int suma_diagonala_secundara();     //second one

int main()
{

    scanf("%hu", &n);
    system("cls");
    j = n / 2 - 1;
    a[0][j] = 4;
    a[0][j + 1] = 1;
    a[1][j] = 2;
    a[1][j + 1] = 3;

    suma[0] = 5;
    suma[1] = 5;
    suma[n + j] = 6;
    suma[n + j + 1] = 4;

    for (int x = 2; x <= (n / 2) * (n / 2); x++)
    {
        next_b(&i, &j);
        a[i][j] = x;
        completare(i, j);
    }
    tipar();
    //for(int x=0;x<n;x++){
    //
    //    printf("suma de pe linia %d este: %d\n",x,suma_linie(x));
    //    printf("suma de pe coloana %d este: %d\n\n",x,suma_coloana(x));
    //}
    //printf("suma de pe daig principala  este: %d\n\n",suma_diagonala_principala());
    //  printf("suma de pe daig secundara este: %d\n\n",suma_diagonala_secundara());

    for (int x = 0; x < 2 * n + 2; x++)
    {
        if (x < n)
        {
            printf("suma de pe linia %d este %hu\n", x, suma[x]);
        }
        else if (x < 2 * n)
        {
            printf("suma de pe coloana %d este %hu\n", x % n, suma[x]);
        }
        else if (x == 2 * n)
        {
            printf("suma de pe diag principala este %hu\n", suma[x]);
        }
        else
        {
            printf("suma de pe diag secundara este %hu\n", suma[x]);
        }
    }
    return 0;
}

void tipar()
{

    for (int k = 0; k < n; k++)
    {
        for (int l = 0; l < n; l++)
        {

            if (a[k][l] < 10)
            {
                printf("   %d |", a[k][l]);
            }
            else if (a[k][l] <= 99)
            {
                printf("  %d |", a[k][l]);
            }
            else if (a[k][l] < 1000)
            {
                printf(" %d |", a[k][l]);
            }
            else if (a[k][l] < 10000)
            {
                printf("%d ", a[k][l]);
            }
        }
        printf("\n");
        for (int z = 0; z <= 6 * n - 1; z++)
        {
            printf("-");
        }
        printf("\n");
    }
    printf("\n");
}

void next_b(unsigned short *i, unsigned short *j)
{
    if (*i - 2 < 0)
    {

        if (a[n - 2][*j + 2] == 0 && *j + 2 <= n - 2)
        {
            // printf("cazul 2\n");
            *i = n - 2;
            *j += 2;
            return;
        }
        else if (a[*i - 2][*j] == 0)
        {
            // printf("cazul 7\n");
            *i += 2;
            return;
        }
    }

    else
    {
        if (*j == n - 2)
        { //printf("cazul 3\n");
            *i -= 2;
            *j = 0;
            return;
        }
        else if (a[*i - 2][*j + 2] != 0)
        {
            //printf("cazul 4\n");
            *i += 2;
        }
        else if (a[*i - 2][*j + 2] == 0)
        {
            //         printf("cazul 5\n");
            *i -= 2;
            *j += 2;
        }
    }
}

void completare(unsigned short i, unsigned short j)
{

    if (i <= n / 2)
    { //////////// l

        if (i == n / 2 - 1 && j == n / 2 - 1)
        {

            a[i][j + 1] = 4 * a[i][j];
            a[i + 1][j] = 4 * a[i][j] - 2;
            a[i + 1][j + 1] = 4 * a[i][j] - 1;
            a[i][j] = 4 * a[i][j] - 3;
        }
        else
        {
            a[i][j] = 4 * a[i][j];
            a[i][j + 1] = a[i][j] - 3;
            a[i + 1][j] = a[i][j] - 2;
            a[i + 1][j + 1] = a[i][j] - 1;
        }
    }
    else if (i == n / 2 + 1)
    { ///////////// u

        if (j == n / 2 - 1)
        {

            a[i][j] = 4 * a[i][j];
            a[i][j + 1] = a[i][j] - 3;
            a[i + 1][j] = a[i][j] - 2;
            a[i + 1][j + 1] = a[i][j] - 1;
        }
        else
        {

            a[i][j + 1] = 4 * a[i][j];
            a[i + 1][j] = 4 * a[i][j] - 2;
            a[i + 1][j + 1] = 4 * a[i][j] - 1;
            a[i][j] = 4 * a[i][j] - 3;
        }
    }
    else
    { ///////x

        a[i][j + 1] = 4 * a[i][j];
        a[i + 1][j + 1] = 4 * a[i][j] - 2;
        a[i + 1][j] = 4 * a[i][j] - 1;
        a[i][j] = 4 * a[i][j] - 3;
    }

    suma[i] += a[i][j] + a[i][j + 1];
    suma[i + 1] += a[i + 1][j] + a[i + 1][j + 1];
    suma[n + j] += a[i][j] + a[i + 1][j];
    suma[n + j + 1] += a[i][j + 1] + a[i + 1][j + 1];

    if (i == j)
    {
        suma[2 * n] += a[i][j] + a[i + 1][j + 1];
    }
    if (i + j + 1 == n - 1)
    {
        suma[2 * n + 1] += a[i + 1][j] + a[i][j + 1];
    }
}

int suma_linie(unsigned short x)
{
    int s = 0;
    for (int y = 0; y < n; y++)
    {
        s += a[x][y];
    }
    return s;
}
int suma_coloana(unsigned short y)
{
    int s = 0;
    for (int x = 0; x < n; x++)
    {
        s += a[x][y];
    }
    return s;
}
int suma_diagonala_principala()
{
    int s = 0;

    for (int x = 0; x < n; x++)
    {

        s += a[x][x];
    }
    return s;
}
int suma_diagonala_secundara()
{
    int s = 0;

    for (int x = 0; x < n; x++)
    {

        s += a[x][n - x - 1];
    }
    return s;
}
#包括
#包括
无符号短a[100][100],i=0,j=0,n,suma[100];
无效下一个_b(无符号短*i,无符号短*j);//找到正确的i和j
void completary(无符号短i,无符号短j);//在找到i和j后完成矩阵
void tipar();//打印矩阵
int suma_linie(无符号短x)//行和
科罗纳岛(无符号短y)//列和
这是我的原则//第一对角线
这是一条对角线()//第二个
int main()
{
scanf(“%hu”、&n);
系统(“cls”);
j=n/2-1;
a[0][j]=4;
a[0][j+1]=1;
a[1][j]=2;
a[1][j+1]=3;
suma[0]=5;
suma[1]=5;
suma[n+j]=6;
suma[n+j+1]=4;

对于(int x=2;x我检查了您的代码并发现了一些问题。我尝试了
n=90

for(int x=2; x<=(n/2)*(n/2); x++)
{
   next_b(&i,&j);
   a[i][j]=x;
   completare(i,j);

}

if(*j==n-2)
{
    //printf("cazul 3\n");
    *i-=2;
    *j=0;
    return;
}
else if (a[*i-2][*j+2]!=0) // here
{
    //printf("cazul 4\n");
    *i+=2;

}
else if(a[*i-2][*j+2]==0)  // here
{
    //printf("cazul 5\n");
    *i-=2;
    *j+=2;
}

尝试修复这些负面索引,然后它就会工作(假设您的方法是正确的)。

感谢您的帮助,我向您建议的代码部分添加了
abs
,效果很好
if(*j==n-2)
{
    //printf("cazul 3\n");
    *i-=2;
    *j=0;
    return;
}
else if (a[*i-2][*j+2]!=0) // here
{
    //printf("cazul 4\n");
    *i+=2;

}
else if(a[*i-2][*j+2]==0)  // here
{
    //printf("cazul 5\n");
    *i-=2;
    *j+=2;
}