【问题标题】:Identifying 1's in columns of sparse Matrix array C在稀疏矩阵数组 C 的列中识别 1
【发布时间】:2016-02-24 09:29:56
【问题描述】:

我试图让我的程序识别用户输入的稀疏矩阵的每一列中有多少个 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++;
       }

    }
}

【问题讨论】:

  • printColumnStatisticsOfMatrixrc 中没有初始化。
  • 另外,if(p[i][j]=1) 应该是 if (p[i][j] == 1)。如果您将1 放在 LHS 中,您的编译器会警告您。养成一个好习惯。
  • @JohnnyMopp 我不确定你的意思,当你说我没有初始化 rc 并且我不知道 LHS 代表什么时,我是新手。跨度>
  • printColumnStatisticsOfMatrixrfor(i=0; i&lt;r; i++) 中的值是多少?它是未定义的。你在int c, r, k; 中声明了r',但你没有给它一个初始值。
  • LHS 在左侧。如果你输入了if (1 = p[i][j]),编译器会警告你不能给1赋值。这只是我多年前养成的一个小习惯,已经帮助了不止几次。

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


【解决方案1】:

您需要在main() 中声明rc 并将它们传递给其他函数。一次引用,一次值:

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);
}

【讨论】:

  • 在我按照你所说的更改了rc 之后,现在我在编译时收到错误p= malloc(r* sizeof(int*));p[i]= malloc(c* sizeof(int)); 以及多个警告。
  • getBinarySparseMatrixFromUser 中的任何位置,如果您有 rc,您需要更改为 *r*c,因为在该函数中它们是指针,
  • 谢谢你的工作,但我似乎仍然无法让程序识别每列中 1 的位置
  • @Leo.p 更新了最后一个循环。
  • 我声明 rowcolumnint 正确,即 int row, column;
猜你喜欢
  • 1970-01-01
  • 2015-04-08
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多