程序对多项式求和的C运行时错误

时间:2013-06-12 04:05:42

标签: c runtime-error

该程序用于添加两个多项式并打印结果。

它只是将乘数存储在数组中,并使用索引az作为参数

的幂

它总和和减去但是当它涉及产品它打印

0x0 0*x2 0*x3 ...

这是基本的C编程课,在三个小时内我应该把它给大师: - (

它获取数组中的乘数并使用某些函数计算结果:

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

double a[50], b[50], c[101];
int dega, degb;

SumArray (a, b)
double a[], b[];
{
    extern int dega, degb;
    extern double c[];
    int i, max = (dega < degb ? degb : dega) + 1;

    for(i = 0; i < max; i++)
        c[i] = a[i] + b[i];
}

SubtractArray(a, b)
double a[], b[];
{
    extern int dega, degb;
    extern double c[];
    int i, max = (dega < degb ? degb : dega) + 1;
    for (i = 0; i < max; i++)
        c[i]=a[i]-b[i];
}

ProductArray(a, b)
double a[], b[];
{
    extern int dega, degb;
    extern double c[];
    int i,j;
    double tempa, tempb;
    for(i = 0; i < dega + 1; i++)
        for(j = 0; j < degb + 1; j++)
        {
            tempa = a[i];
            tempb = b[j];
            c[i + j] = c[i + j] + (tempa * tempb);
        }
}

int main()
{
    extern int dega, degb;
    extern double a[50], b[50], c[]; //stores the multipliers
    int i, operation;

    for(i = 0; i < 50; i++)
        a[i] = b[i] = 0;
    for(i = 0; i < 102; i++)
        c[i] = 0;

    printf("darjeye chand jomleee ha ra vared konid");
    scanf("%d %d", &dega, &degb);
    printf("zarayebe chand jomlee aval ra vaerd konid");

    for(i = 0; i < dega + 1; i++)
        scanf("%d", &a[i]);
    printf("zarayebe chand jomlee dovoom ra vaerd konid");
    for(i = 0; i < degb + 1; i++)
        scanf("%d", &b[i]);
    printf("amaliyate morede nazartan ra vare konid baraye jame 0, tafrigh 1, zarb 2 ra vared konid");
    scanf("%d", &operation);

    switch(operation)
    {
        case 0:
        {
            SumArray(a,b);
            for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
            {
                printf(" %d*x", c[i]);
                printf("%d ", i);
            }
            break;
        }
        case 1:
        {
            SubtractArray(a,b);
            for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
            {
                printf(" %d*x", c[i]);
                printf("%d ", i);
            }
            break;
        }
        case 2:
        {
            ProductArray(a,b);

            for(i = 0; i < (dega + degb + 1); i++)
            {
                printf("\%d*x", c[i]);
                printf("%d ", i);
            }
            break;
        }
        default:
            printf("amaliyate vared shode sahih nabud");
    }

    system("PAUSE");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

scanf("%d",a[i]);应为scanf("%d",&a[i]);scanf("%d",b[i]);应为scanf("%d",&b[i]);

scanf需要变量的地址。对于数组,例如,在您的情况double a[50]中,只需编写a即可为您提供数组a的起始地址,该地址与&a[0]相同,其中a[0]只是第一个元素,但不是第一个元素的地址。 a[i]&a[i]是该元素的地址的元素。希望你明白了。