查找数组

时间:2018-04-15 19:52:11

标签: c arrays function

我正在尝试查找下面所示的以下数组的对角线总和,但每当我调用函数diagsfunc并运行程序时,它都没有给出答案,

我做了一个if语句以确保数组是一个方阵,如果是,它继续调用函数diagsfunc,在那里我找到对角元素的总和但是每当我运行它时都没有打印总和。

下面是函数,main和数组。除了对角线之外,所有其他功能都能正常工作。如果您想知道该文件包含以下数字,前两个数字只是行和列,最后两个数字是用于函数的数组的特定行和列:

该文件包含:

4 4
5.5 7.8 7.8 3.3
2.2 1.1 4.4 7.7
9.9 7.6 4.4 6.6
9.0 4.0 5.0 2.0
2
3

代码:

/*Diagonal function*/
double
diagsfunc (double ** arr, int diagr, int diagc)
{
    int f, g;
    double sum3 = 0;

    for (f=0;f<diagr;++f)
    {
        for (g=0;f<diagc;++g)
        {
            if (f == g)
            {
                sum3 = sum3 + arr[f][g];
            }
        }
    }    

    return(sum3);

}

int
main (void)
{
    int cols, rows, i, j, nrows, ncols;
    double **a;
    double diag;
    FILE * fpointer;
    fpointer = fopen("projectfile.txt", "r");

    fscanf(fpointer, "%d %d", &rows, &cols);

    a = (double **) calloc (rows, sizeof(double *));

    for (i=0; i<rows; ++i)
        a[i] = (double *) calloc (cols, sizeof(double));

    for (i = 0; i < rows; ++i)
        for ( j = 0; j < cols; ++j)
            fscanf(fpointer, "%lf", &a[i][j]);

    for (i = 0; i < rows; ++i){
        for ( j = 0; j < cols; ++j){
            printf("%10.3lf ", a[i][j]);
        }
            puts("");
    }

    fscanf(fpointer, "%d %d", &nrows, &ncols);

    if (rows == cols){
        diag = diagsfunc(a, rows, cols);
        printf("The sum of the diagonal elements is: %.3lf\n", diag);
    } else {
        printf("The array must be square to compute the sum of diagonals\n");
    }
    fclose(fpointer);
    return(0);
}

1 个答案:

答案 0 :(得分:0)

你做的几乎没有愚蠢的错误,更多,但我列出了只需要一个

  1. 循环for (g=0;f<diagc;++g)中使用的错误变量。 它应该是

    for (g = 0;g < diagc; ++g)

  2. 找到对角线的总和,你不需要像Pablo指出的2个循环你可以用一个循环本身作为

    for(int i = 0; i < diagr; ++i) { /* if its a square matrix */
            sum3 + = arr[i][i]      
    }
    
  3. 完整代码

    double diagsfunc (double ** arr, int diagr) {
            int f, g;
            double sum3 = 0;
            for(int i = 0; i < diagr; ++i) {
                    sum3 += arr[i][i];
            }
            return sum3;
    }
    int main (void){
            int cols, rows, i, j, nrows, ncols;
            double **a;
            double diag;
            FILE * fpointer = fopen("projectfile.txt", "r");
            if(fpointer == NULL ) {
                    /* put some message */
                    return 0;
            }
            fscanf(fpointer, "%d %d", &rows, &cols);
            a = (double **) calloc (rows, sizeof(double *));
            for (i=0; i<rows; ++i)
                    a[i] = (double *) calloc (cols, sizeof(double));
            for (i = 0; i < rows; ++i)
                    for ( j = 0; j < cols; ++j)
                            fscanf(fpointer, "%lf", &a[i][j]);
            for (i = 0; i < rows; ++i){
                    for ( j = 0; j < cols; ++j){
                            printf("%10.3lf ", a[i][j]);
                    }
                    puts("");
            }
            //rewind(fpointer); /* this you forget */
            //fscanf(fpointer, "%d %d", &nrows, &ncols); /* why this statement ? */
            if (rows == cols){
                    diag = diagsfunc(a, rows); /* no need to pass col value as its square matrix, both will be equal */
                    printf("The sum of the diagonal elements is: %.3lf\n", diag);
            } else {
                    printf("The array must be square to compute the sum of diagonals\n");
            }
            fclose(fpointer);
            return(0);
    }
    

    同时检查所有功能的返回值&amp;学习如何调试代码。