Float-type Array Index (Error:Illegal use of floating point)

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

Float-type Array Index (Error:Illegal use of floating point)

Post by raysa »

Hi,

I need to do a very simple task like:

Code: Select all

y[0.5] = 1;
y[1.5] = 2;
y[2.5] = 3;
but I always get "Illegal use of floating point" error message.
It seems like the array index should only contain a solid integer.

Is there any way to do that type of array assignment
(float-type of array index)?
Any help would be very much appreciated.

Thank you,

raysa
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

Is there any way to do that type of array assignment
(float-type of array index)?
:o :roll:
It is not possible in any p-language till now. :-?
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

For arrays, expression v[n] has the same meaning as *(v+n).
That is, v is interpreted as a pointer to the beginning of array, this pointer is incremented by n and dereferenced. Of course it's nonsensical to increment a pointer by a fraction.

What you probably need is STL's map or hash_map class. Declare v as:
map<double, int> v;
and your code will work. Don't forget to add: #include <map>.
Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Post by Moha »

Another solution is:

Code: Select all

double array[10000];
#define I(a) (((int)(a))*2+((a)-(int)(a))>0)
for accessing the array you can use array[I(1.5)]. but please pay attention that you should know the exact number of digits after decimal point.
raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

Post by raysa »

Thank's A1, mf and Moha for the insights!

@Moha
Can you tell us what your code do?
I'm applying your code in my listing:

Code: Select all

scanf ("%f",y[I(1.5)]);
but it got "Subscripting missing: ]" and "Expression syntax" error.

I can't see any typo in the syntax. Any thought?
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

raysa:

It should be

Code: Select all

scanf("%lf",&y[I(1.5)]);
if y is a double array of course.

You forgot the '&'. Also check your y array declaration.

And I think moha's code is actually a simple hash function which computes the integer index for a floating point value.
raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

Post by raysa »

Nevermind. It's my mistake.
I was putting a semicolon behind the #define syntax.

Anyway, please tell us what your code does...
What part in your code is to modify the number of digits after decimal point?

Thank's,

raysa
raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

Post by raysa »

This is weird...
I can retrive y[1.5] value using y[1.8] call.

Code: Select all

printf ("y = "); scanf ("%lf",&y[I(1.5)]);
printf ("y = %lf\n", y[I(1.8)]);
How to fix this modifying Moha's hash?
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

I don't understand what Moha try to say !
His I() function always return true or false(1\0)!
so how it can be used to iterate in a 10000 size array!!
Moha
Experienced poster
Posts: 216
Joined: Tue Aug 31, 2004 1:02 am
Location: Tehran
Contact:

Post by Moha »

Ok, The I(a) is suitable for all numbers which are in the form of n*0.5.
If you want to use it for all fractional numbers with one digit after decimal place, you should rewite it in this form:

Code: Select all

#define I(a) ((int)((a)*10))
These functions are a one to one mapping.
for the first hash function 1.8 is mapped to place that 1.5 is stored.
I did a very nasty mistake in my previous Hash function it should changed into

Code: Select all

#define I(a) (((int)(a))*2+(((a)-(int)(a))>0)) 
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Post by Martin Macko »

Moha wrote:
code wrote:#define I(a) (((int)(a))*2+(((a)-(int)(a))>0))
If you are using only numbers of the form n*0.5, where n>=0, you can simplfy it and write just the following, can't you?
#define I(a) ((int)((a)*2))
Last edited by Martin Macko on Mon Jun 12, 2006 3:16 am, edited 1 time in total.
raysa
New poster
Posts: 40
Joined: Wed Dec 18, 2002 4:23 pm
Location: Indonesia

Post by raysa »

it's all OK now.
Thank you everyone.. Especially you, Moha!
Your code WORKS like charm! :wink:
Post Reply

Return to “C++”