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
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]);
}
vectors[i] is a float, one of n such floats. The scanf function requires you to use the address-of operator (&) when reading data into such a variable.Code:scanf("%d", vectors[i]);
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.
Are you really attempting to read a float with %d? Why?
For that matter, are you really attempting to use scanf in C++? Why?