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]); }
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]); }
Last edited by no_one_knows; 06-16-2009 at 07:29 PM.
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]);
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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?