0

这是问题所在:http ://cps125.scs.ryerson.ca/labs/homework8.gif

这是我的尝试:

#include <stdio.h>

void tenfold(int (*array1)[9], int (*array2)[9])
{
    int i;
    for (i = 0; i < 9; i++) {
        if ((*array1)[i] > 0) {
            (*array2)[i] = 10 * (*array1)[i];
        } else {
            (*array2)[i] = (*array1)[i];
        }
    }
}

int main()
{
    int array1[9] = { 3, 4, 5, 6, 7, -8, -9, 1, 2 }, array2[9], i;

    tenfold(&array1, &array2);

    for (i=0; i < 9; i++) {
        printf("%lf ", array2[i]);
    }
    return 0;
}

这是输出:

44100667367791664000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000.000000 441006673677917200000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000.000000 4410066736779177 60000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000.000000 4410066736779183200000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000 44100667367791888000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000.000000 44100691391845304000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000.000000 441006913918452980000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000.000000 4410066736779155 20000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000.000000 4410066736779160800000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000000000000000000000000000000000000000000000 4410066736779155 20000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000.000000 4410066736779160800000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000000000 4410066736779155 20000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000.000000 4410066736779160800000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000000000 4410066736779160800000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000000000 4410066736779160800000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000.000000

4

3 回答 3

4

这里至少有一个问题:

printf("%lf ", array2[i]);

%lf字面意思是“长浮点数”,但是你想打印整数:

printf("%i\n", array2[i]);

还要记住 printf 等待\n刷新。

编辑:正如 newacct 所指出的,%f并且最终%lf都是double,因为 varargs 提升floatdouble.

于 2013-04-01T17:33:43.370 回答
0

函数中输入数组的定义无需加星号tenfold

只需将其定义如下

void tenfold(int array1[9], int array2[9])

*并在使用array1array2进入函数时去掉星号tenfold

void tenfold(int array1[9], int array2[9])
{
    int i;
    for (i = 0; i < 9; i++) {
        if ((array1)[i] > 0) {
            (array2)[i] = 10 * (array1)[i];
        } else {
            (array2)[i] = (array1)[i];
        }
    }
}

当你调用tenfold函数时main

tenfold(array1, array2);
于 2013-04-01T17:33:43.513 回答
0

"%lf" 是浮点数的格式,而不是整数。

于 2013-04-01T17:35:36.870 回答