Velocity Reviews > A new learner's question

# A new learner's question

flenovo@126.com
Guest
Posts: n/a

 11-04-2008
I'm a student who is studying program firstly.Recently I wrote
some code below in a program.
for(i=1;i<=count;++i){
number[i]=integer % pow(10,i)/ pow(10,i-1);
}
But my compiler(VC) told me: '%' : illegal,
right operand has type 'double'.Please tell me what does it mean?
And who can I correct it?Think you.

Ben Bacarisse
Guest
Posts: n/a

 11-04-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> I'm a student who is studying program firstly.Recently I wrote
> some code below in a program.
> for(i=1;i<=count;++i){
> number[i]=integer % pow(10,i)/ pow(10,i-1);
> }
> But my compiler(VC) told me: '%' : illegal,
> right operand has type 'double'.Please tell me what does it mean?

The compiler is telling you that the remainder operator (%) can only
be used with integer operands. pow(10, 1) is an expression whose
value is floating point number (double is the name one of C's floating
point types).

> And who can I correct it?Think you.

You could convert the result of pow(10, i) to an int (or some other
integer type -- I can't tell from the fragment which would be right)
with cast, but since you already have a loop, I would simple keep
track of the powers of ten by having another integer variable -- start
it at 1 and multiply by 10 every time i is incremented.

--
Ben.

Chris Dollin
Guest
Posts: n/a

 11-04-2008
(E-Mail Removed) wrote:

> I'm a student who is studying program firstly.Recently I wrote
> some code below in a program.
> for(i=1;i<=count;++i){
> number[i]=integer % pow(10,i)/ pow(10,i-1);

Note: you realise that C array indexes start at 0, not 1? The
code you've written will access elements 1 to count, and
depending on your declaration for `number`, might fall off
the end of the array.

Other note: `number` and `integer` are pretty crummy names for
variables.

> }
> But my compiler(VC) told me: '%' : illegal,
> right operand has type 'double'.Please tell me what does it mean?

It means that [this use of] `%` is illegal, [because] the right
operand [of `%`] [which is `pow(10,i)`] has type 'double' [because
`pow` returns a double result] [but the right operand of `%` is required
to have an integral type].

> And who can I correct it?Think you.

I'm not sure what you're trying to do, so I don't know how to
correct it. If you're going to use `%`, then you need to make
sure the right operand is an integer /somehow/.

(A cast is almost certainly the wrong answer.)

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

James Kuyper
Guest
Posts: n/a

 11-04-2008
(E-Mail Removed) wrote:
> I'm a student who is studying program firstly.Recently I wrote
> some code below in a program.
> for(i=1;i<=count;++i){
> number[i]=integer % pow(10,i)/ pow(10,i-1);
> }

Your code never sets the value of number[0] - is that what you actually
intended to do? In C, array-filling loops that neither start nor stop at
an index of 0 are often errors. Are you sure you didn't mean:

for(i=0; i<count; ++i){
number[i] = integer % pow(10,i+1)/pow(10, i);
}

Pay careful attention to what I wrote: notice the replacement of "<="
with "<", "i" with "i+1", and "i-1 with "i". However, for the rest of
this message that you intended to fill in the array the same was as in

> But my compiler(VC) told me: '%' : illegal,
> right operand has type 'double'.Please tell me what does it mean?
> And who can I correct it?Think you.

The modulus operator '%' has a constraint: both of it's operands must be
integers. For floating point arguments the corresponding operation is
performed by the fmod() function:

number[i] = floor(fmod(integer,pow(10,i)/pow(10, i-1));

However, this is not the right solution to your problem. For this
purpose, you should be dividing by integers, not floating point numbers.
You don't indicate what type "integer" has. For simplicity, I'll assume
that it's int:

int power=1; // This should be same type as 'integer'

for(i=1; i<=count; i++)
{
number[i] = (integer%(10*power)) / power;
power *= 10;
}

A better solution, however, would involve changing the value of a copy
of 'integer' during each pass through the loop:

int copy = integer;

for(i=1; i<=count; ++i)
{
number[i] = copy % 10;
copy /= 10;
}

If you don't need to retain the previous value of 'integer', you can use
'integer' itself, instead of the copy.

Before you blindly use this code, make sure that you understand,
mathematically, why it produces the same result as the previous version.

Flash Gordon
Guest
Posts: n/a

 11-04-2008
(E-Mail Removed) wrote, On 04/11/08 11:39:
> I'm a student who is studying program firstly.Recently I wrote
> some code below in a program.
> for(i=1;i<=count;++i){
> number[i]=integer % pow(10,i)/ pow(10,i-1);
> }
> But my compiler(VC) told me: '%' : illegal,
> right operand has type 'double'.Please tell me what does it mean?

It means the right hand operand of % in your code is of type double
(because that is what pow()/pow() gives) and that is not allowed.

> And who can I correct it?Think you.

You would need to convert it to an integral type (e.g. int) first.
However, I suggest you need to think about your maths. Consider the
definition of the power function and consider the results of the
following sums...

10*10
-----
10

10*10*10
--------
10*10

10*10*10*10
-----------
10*10*10

y*y*y*y*y
---------
y*y*y*y

science teacher) if you still don't get the point. Then go back and look
at the homework you were set again since I don't believe you are trying
to do the right thing.
--
Flash Gordon
If spamming me sent it to (E-Mail Removed)
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

Ben Bacarisse
Guest
Posts: n/a

 11-04-2008
Flash Gordon <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote, On 04/11/08 11:39:

<snip>
>> for(i=1;i<=count;++i){
>> number[i]=integer % pow(10,i)/ pow(10,i-1);
>> }

<snip>
> However, I suggest you need to think about your maths. Consider the
> definition of the power function and consider the results of the
> following sums...
>
> 10*10
> -----
> 10
>
> 10*10*10
> --------
> 10*10
>
>
> 10*10*10*10
> -----------
> 10*10*10
>
> y*y*y*y*y
> ---------
> y*y*y*y
>
> science teacher) if you still don't get the point.

They are not doing this calculation so your remarks are likely to be
confusing.

--
Ben.

Phil Carmody
Guest
Posts: n/a

 11-04-2008
Ben Bacarisse <(E-Mail Removed)> writes:
> Flash Gordon <(E-Mail Removed)> writes:
>
>> (E-Mail Removed) wrote, On 04/11/08 11:39:

> <snip>
>>> for(i=1;i<=count;++i){
>>> number[i]=integer % pow(10,i)/ pow(10,i-1);
>>> }

> <snip>
>> However, I suggest you need to think about your maths. Consider the
>> definition of the power function and consider the results of the
>> following sums...
>>
>> 10*10
>> -----
>> 10
>>
>> 10*10*10
>> --------
>> 10*10
>>
>>
>> 10*10*10*10
>> -----------
>> 10*10*10
>>
>> y*y*y*y*y
>> ---------
>> y*y*y*y
>>
>> science teacher) if you still don't get the point.

>
> They are not doing this calculation so your remarks are likely to be
> confusing.

And aren't sums.

Phil
--
We must respect the other fellow's religion, but only in the sense and to the
extent that we respect his theory that his wife is beautiful and his children
smart. -- Henry Louis Mencken (1880-1956), American editor and critic

osmium
Guest
Posts: n/a

 11-04-2008
"Phil Carmody" wrote:

> Ben Bacarisse <(E-Mail Removed)> writes:
>> Flash Gordon <(E-Mail Removed)> writes:
>>
>>> (E-Mail Removed) wrote, On 04/11/08 11:39:

>> <snip>
>>>> for(i=1;i<=count;++i){
>>>> number[i]=integer % pow(10,i)/ pow(10,i-1);
>>>> }

>> <snip>
>>> However, I suggest you need to think about your maths. Consider the
>>> definition of the power function and consider the results of the
>>> following sums...
>>>
>>> 10*10
>>> -----
>>> 10
>>>
>>> 10*10*10
>>> --------
>>> 10*10
>>>
>>>
>>> 10*10*10*10
>>> -----------
>>> 10*10*10
>>>
>>> y*y*y*y*y
>>> ---------
>>> y*y*y*y
>>>
>>> science teacher) if you still don't get the point.

>>
>> They are not doing this calculation so your remarks are likely to be
>> confusing.

>
> And aren't sums.

I think "sums" is an Englishism for "arithmetic", or possibly even,
"mathematics". Flash seems to be in the UK.

Ben Bacarisse
Guest
Posts: n/a

 11-04-2008
Phil Carmody <(E-Mail Removed)> writes:

> Ben Bacarisse <(E-Mail Removed)> writes:
>> Flash Gordon <(E-Mail Removed)> writes:
>>
>>> (E-Mail Removed) wrote, On 04/11/08 11:39:

>> <snip>
>>>> for(i=1;i<=count;++i){
>>>> number[i]=integer % pow(10,i)/ pow(10,i-1);
>>>> }

>> <snip>
>>> However, I suggest you need to think about your maths. Consider the
>>> definition of the power function and consider the results of the
>>> following sums...
>>>
>>> 10*10
>>> -----
>>> 10
>>>
>>> 10*10*10
>>> --------
>>> 10*10
>>>
>>>
>>> 10*10*10*10
>>> -----------
>>> 10*10*10
>>>
>>> y*y*y*y*y
>>> ---------
>>> y*y*y*y
>>>
>>> science teacher) if you still don't get the point.

>>
>> They are not doing this calculation so your remarks are likely to be
>> confusing.

>
> And aren't sums.

If you don't like Flash's terminology, reply to his message -- I'm just
quoting it!

For those that are (understandably) confused, in British English a
"sum" is an informal term for an arithmetic calculation. My point is
that the OP is not calculating 10**n/10**(n-1).

--
Ben.

Flash Gordon
Guest
Posts: n/a

 11-04-2008
osmium wrote, On 04/11/08 16:28:
> "Phil Carmody" wrote:
>
>> Ben Bacarisse <(E-Mail Removed)> writes:
>>> Flash Gordon <(E-Mail Removed)> writes:
>>>
>>>> (E-Mail Removed) wrote, On 04/11/08 11:39:
>>> <snip>
>>>>> for(i=1;i<=count;++i){
>>>>> number[i]=integer % pow(10,i)/ pow(10,i-1);
>>>>> }
>>> <snip>
>>>> However, I suggest you need to think about your maths. Consider the
>>>> definition of the power function and consider the results of the
>>>> following sums...
>>>>
>>>> 10*10
>>>> -----
>>>> 10
>>>>
>>>> 10*10*10
>>>> --------
>>>> 10*10
>>>>
>>>>
>>>> 10*10*10*10
>>>> -----------
>>>> 10*10*10
>>>>
>>>> y*y*y*y*y
>>>> ---------
>>>> y*y*y*y
>>>>
>>>> science teacher) if you still don't get the point.
>>> They are not doing this calculation so your remarks are likely to be
>>> confusing.

integer % (pow(10,i)/ pow(10,i-1))
which does have the calculation I mentioned. I'll claim that any
interpretation of of invalid code is just as valid as any other

>> And aren't sums.

>
> I think "sums" is an Englishism for "arithmetic", or possibly even,
> "mathematics".

Sums is used to mean arithmetic in English English.

> Flash seems to be in the UK.

I am in the UK but my surname is Scottish and the Scots don't like being
lumped in under English, and quite correctly too. You should have either
used "in England" and "English" or "in Britain" and "British" or "in the
UK" and got in trouble for UKism However, despite the surname I am
English and in England (so I'm also British and in Britain, and in the
UK and a UK citizen).
--
Flash Gordon
If spamming me sent it to (E-Mail Removed)
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/