Any idea why this returns 0 on all the vectors elements?

Code:

int n, i;

scanf("%d", &n);

float* vectors;

vectors = new float[n];

for (i=0; i < n; i++)

{

scanf("%d", &vectors[i]);

}

for (i=0; i < n; i++)

{

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

}

Printable View

- 06-16-2009no_one_knowsScanf and Dynamic arrays
Any idea why this returns 0 on all the vectors elements?

Code:

int n, i;

scanf("%d", &n);

float* vectors;

vectors = new float[n];

for (i=0; i < n; i++)

{

scanf("%d", &vectors[i]);

}

for (i=0; i < n; i++)

{

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

}

- 06-16-2009hk_mp5kpdwCode:
`scanf("%d", vectors[i]);`

*float*, one of*n*such*float*s. The*scanf*function requires you to use the address-of operator (&) when reading data into such a variable. - 06-16-2009no_one_knows
Yes, I corrected that. Originally it was with the & but got posted like this for some reason.

Unfortunately, it still doesn't work. All the prints are 0.0000. - 06-16-2009tabstop
Are you really attempting to read a float with %d? Why?

For that matter, are you really attempting to use scanf in C++? Why? - 06-17-2009no_one_knows