玩弄函数中变量的类型

时间:2018-12-10 19:59:23

标签: c arrays function

我的问题是,当我将{l“的值传递给toDeg()函数(在切换中)时,切换始终变为0。代码中没有位置可以更改切换,只有比较即可。

这不是全部代码,只是一部分。但是主要的问题在这里。我想我正在某处丢失类型。

double  inp(double toggle, int N, int M) //toggle means in RAD or DEG
{
    double a[N][M];
    double input;
    double sum = 0;
    int k = 0;

    printf("Please, enter yor angles\n");

    for (int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++) //Angles
        {
            scanf("%lf", &input);
            a[i][j] = input;
        }
    }

    if (toggle == 0) //radians
    {
        for (int i = 0; i < N; i++)
        {
            sum = 0;
            for(int j = 0; j < M; j++) //Angles
            {
                sum += a[i][j];
            }
            if ((sum > (pi * (M - 2) + pi / 45)) || (sum < (pi * (M - 2) - pi / 45))) // oversight is 4 DEG
            {   
                printf("Angles were entered incorrectly for polygon %d\n", i);
                return 0;
                k = 1.0;
            }
            else k = 0.0;
        }
    }
    else //degrees
    {
        for (int i = 0; i < N; i++)
        {
            sum = 0;
            for(int j = 0; j < M; j++) //Angles
            {
                sum += a[i][j];
            }
            if (sum != (180 * (M - 2)))
            {
                printf("Angles were entered incorrectly for polygon %d\n", i);
                k = 1.0;
            }
            else k = 0.0;
        }
    }
    return k;
}

double toRad(double ONOFF, double toggle, int N, int M, double arr[][M])
{
    if (fabs(ONOFF - 1.0) < FLT_EPSILON) return 0;
    double TheWay; //How it should Output

    if (fabs(toggle - 1.0) < FLT_EPSILON)
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                arr[i][j] *= (pi / 180);
            }
        }
        TheWay = 0.0; //Is in RAD
        return TheWay;
    }
    else return 0;
}
    double toDeg(double ONOFF, double toggle, int N, int M, double arr[][M])
{
    if (fabs(ONOFF - 1.0) < FLT_EPSILON) return 0;
    double TheWay;

    if (fabs(toggle - 1.0) < FLT_EPSILON)
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                arr[i][j] *= (180 / pi);
            }
        }
        TheWay = 1.0; //Is in DEG
        return TheWay;
    }
    else return 0;
}

void outp(double ONOFF, double TheWay, int N, int M, double arr[][M])
{
    if (ONOFF == 1.0) return;
    (TheWay == 1.0) ? printf("All angles are now in RAD\n") : printf("All angles are now in DEG\n");

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            printf("%.02lf\t", arr[i][j]);
        }
        printf("\n");
    }
}

int main() 
{
    double k;
    int M, N, TheWay;
    double l, toggle; //toggle
    printf ("Please, enter the numbers of polygons and its angles\n");
    scanf ("%d%d", &N, &M);
    //double * a = malloc (N * M * sizeof (double));
    double a[N][M];
    printf ("Enter 0 if you want your angles in RAD and 1 if in DEG\n");
    scanf("%lf", &l);
    /*do
    {
        printf("Enter 0 if you want your angles in RAD and 1 if in DEG\n");
        scanf("%d", &l);
    } while((l != 0) && (l != 1));*/
    inp (l, N, M);
    toRad (k, l, N, M, a);
    toDeg (k, l, N, M, a);
    outp (k, TheWay, N, M, a);
    printf("\n toggle = %lf, l = %lf", toggle, l);

    return 0;       
}

1 个答案:

答案 0 :(得分:0)

我假设您引用此行:

printf("\n toggle = %lf, l = %lf", toggle, l);

打印toggle的值

答案是您从不初始化toggle。换句话说,您的程序中没有地方为toggle赋值。这意味着您的程序具有未定义的行为,因为您访问(也称为打印)未初始化的值。

看看您的main

int main() 
{
    double k;
    int M, N, TheWay;
    double l, toggle; // toggle is uninitialize
    printf ("Please, enter the numbers of polygons and its angles\n");

    scanf ("%d%d", &N, &M);  // No change of toggle

    //double * a = malloc (N * M * sizeof (double));
    double a[N][M];
    printf ("Enter 0 if you want your angles in RAD and 1 if in DEG\n");

    scanf("%lf", &l);  // No change of toggle


    /*do
    {
        printf("Enter 0 if you want your angles in RAD and 1 if in DEG\n");
        scanf("%d", &l);  // No change of toggle

    } while((l != 0) && (l != 1));*/

    inp (l, N, M);          // Can't change toggle
    toRad (k, l, N, M, a);  // Can't change toggle
    toDeg (k, l, N, M, a);  // Can't change toggle
    outp (k, TheWay, N, M, a);  // Can't change toggle

    printf("\n toggle = %lf, l = %lf", toggle, l); // Print of uninitialized toggle

    return 0;       
}

我认为您会混淆ltoggle