在稀疏矩阵数组C

时间:2015-11-22 03:36:35

标签: c pointers for-loop multidimensional-array sparse-matrix

我试图让我的程序识别用户输入的稀疏矩阵的每列中有多少1个。我想过使用嵌套的for循环,if(p[i][j]=1)和计数器k++,但它会打印大量的随机数然后崩溃。我的整个代码都包含在内,但我遇到问题的部分是int **printColumnStatisticsOfMatrix(int **p)

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

int **getBinarySparseMatrixFromUser();
int **printColumnStatisticsOfMatrix(int **p);

int main()
{
int **p;
p = getBinarySparseMatrixFromUser();
printColumnStatisticsOfMatrix(p);
return 0;
}

int ** getBinarySparseMatrixFromUser()
{
int atoi(const char *str);
int i;
int j;
int r, c, f, g;
int **p;
char str[20];
char term;

    printf("Please enter the number of rows:\n");
        scanf("%s", &str);

while(atoi(str)==0)
{
    printf("Invalid entry. \nPlease enter the number of rows:\n");
        scanf("%s",str);
}
    r = atoi(str);

    printf("Please enter the number of columns:\n");
        scanf("%s", &str);

while(atoi(str)==0)
{
    printf("Invalid entry. \nPlease enter the number of columns:\n");
        scanf("%s",str);
}
    c = atoi(str);

    p= malloc(r* sizeof(int*));

for (i=0; i<r; i++)
{
    p[i]= malloc(c* sizeof(int));
}

for(i=0; i<r; i++)
{
    for(j=0; j<c; j++)
    {
        p[i][j]=0;
    }
}

for (i=0; i<r; i++)
{
    printf("Please enter the number of 1's in row %d :\n", (i+1));
        scanf("%d", &f);


        if (f>0)
        {
            printf("Please enter column location of the 1's in row %d : \n", (i+1));

                for (j=0; j<f; j++)
                {
                    scanf("%d", &g);
                        p[i][g-1]= 1;
                }
        }
}
        printf("\nYou Have entered the Matrix!!!\n\nI mean this is the Matrix you have entered:\n\n");

    return p;
}

(以上所有内容只是为了帮助任何有帮助的人,这是我遇到麻烦的地方)

int **printColumnStatisticsOfMatrix(int **p)
{
int i;
int j;
int c, r, k;

for(i=0; i<r; i++)
{
    printf("\t");

    for(j=0; j<c; j++)
    {
        printf("%d ", p[i][j]);
    }

    printf("\n");
}

for(i=0; i<r; i++)
{
    for(j=0; j<c; j++)
    {
        if(p[i][j]==1)
       {
            printf("The number of 1’s in column %d is %d\n", (i+1), k);
            k++;
       }
    }
}



}

如果我删除下面显示的部分,它仍然有效,但我仍然需要识别每列中的1。我知道问题在这里,但我不明白为什么它崩溃或为什么它不起作用。

for(i=0; i<r; i++)
{
    for(j=0; j<c; j++)
    {
        if(p[i][j]==1)
       {
            printf("The number of 1’s in column %d is %d\n", (i+1), k);
            k++;
       }

    }
}

1 个答案:

答案 0 :(得分:1)

您需要在r中声明cmain()并将其传递给其他功能。一次参考,一次按值:

int main()
{
    int **p;
    int r, c;
    p = getBinarySparseMatrixFromUser(&r, &c);
    printColumnStatisticsOfMatrix(p, r, c);
    return 0;
}

int ** getBinarySparseMatrixFromUser(int *r, int *c)
{
    // int r, c;    <- Remove
    ...
}

由于您现在将rc作为指针传递,因此您必须使用指针表示法来对它们进行处理:

*r = atoi(str);

则...

void printColumnStatisticsOfMatrix(int **p, int r, int c)
{
     // int r, c;    <- Remove
     ...
}

最后,您希望在每列中找到#1。

// Reverse your indices (and use better variable names)
for (int column = 0; column < c; column++)
{
    k = 0;
    for (int row = 0; row < r; row++)
    {
        if (p[row][column] == 1)
            k++;
    }
    // This goes here
    printf("The number of 1’s in column %d is %d\n", (column + 1), k);
}