0

Here is the problem: http://cps125.scs.ryerson.ca/labs/homework8.gif

Here is my attempt:

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

Here is the output:

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.000000

user1251385
  • 197
  • 1
  • 1
  • 8

3 Answers3

4

There is at least a problem here:

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

%lf literally means "long float", however you want to print integers:

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

Also remember that printf waits for \n to flush.

EDIT: As newacct pointed out, %f and %lf are both double in the end, since varargs promote float to double.

cmc
  • 2,061
  • 1
  • 19
  • 18
0

No need to add asterisk in the definition of input array in the function tenfold

just define it as following

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

And remove the asterisk * when using array1 and array2 into the function 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];
        }
    }
}

and when you call the tenfold function in the main:

tenfold(array1, array2);
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

"%lf " is the format for a float, not an int.

stark
  • 12,615
  • 3
  • 33
  • 50