# A new learner's question

Discussion in 'C Programming' started by flenovo@126.com, Nov 4, 2008.

1. ### Guest

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=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.

, Nov 4, 2008

2. ### Ben BacarisseGuest

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=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.

Ben Bacarisse, Nov 4, 2008

3. ### Chris DollinGuest

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=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

Chris Dollin, Nov 4, 2008
4. ### James KuyperGuest

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=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 = 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 = 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 = (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 = 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.

James Kuyper, Nov 4, 2008
5. ### Flash GordonGuest

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=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
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

Flash Gordon, Nov 4, 2008
6. ### Ben BacarisseGuest

Flash Gordon <> writes:

> wrote, On 04/11/08 11:39:

<snip>
>> for(i=1;i<=count;++i){
>> number=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.

Ben Bacarisse, Nov 4, 2008
7. ### Phil CarmodyGuest

Ben Bacarisse <> writes:
> Flash Gordon <> writes:
>
>> wrote, On 04/11/08 11:39:

> <snip>
>>> for(i=1;i<=count;++i){
>>> number=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

Phil Carmody, Nov 4, 2008
8. ### osmiumGuest

"Phil Carmody" wrote:

> Ben Bacarisse <> writes:
>> Flash Gordon <> writes:
>>
>>> wrote, On 04/11/08 11:39:

>> <snip>
>>>> for(i=1;i<=count;++i){
>>>> number=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.

osmium, Nov 4, 2008
9. ### Ben BacarisseGuest

Phil Carmody <> writes:

> Ben Bacarisse <> writes:
>> Flash Gordon <> writes:
>>
>>> wrote, On 04/11/08 11:39:

>> <snip>
>>>> for(i=1;i<=count;++i){
>>>> number=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.

Ben Bacarisse, Nov 4, 2008
10. ### Flash GordonGuest

osmium wrote, On 04/11/08 16:28:
> "Phil Carmody" wrote:
>
>> Ben Bacarisse <> writes:
>>> Flash Gordon <> writes:
>>>
>>>> wrote, On 04/11/08 11:39:
>>> <snip>
>>>>> for(i=1;i<=count;++i){
>>>>> number=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
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

Flash Gordon, Nov 4, 2008
11. ### jameskuyperGuest

Flash Gordon wrote:
> osmium wrote, On 04/11/08 16:28:
> > "Phil Carmody" wrote:
> >
> >> Ben Bacarisse <> writes:
> >>> Flash Gordon <> writes:
> >>>
> >>>> wrote, On 04/11/08 11:39:
> >>> <snip>
> >>>>> for(i=1;i<=count;++i){
> >>>>> number=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.

>
> I read it as being:
> 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

As far as figuring out what behavior is required by the standard,
you're correct. For purposes of guessing what the intent of the
defective code was, it's more appropriate to look for the smallest,
simplest set of errors that could explain why it was written
defectively. In this case, to me it seems that the simplest assumption
is that the OP was unaware of the constraint that this code violates,
in which case that code was probably intended to be interpreted in the
same fashion that it would have been interpreted as if the return type

(integer % pow(10,i)) / pow(10,i-1)

One advantage of this interpretation is that it makes the use of two
different pow() calls more reasonable (though it is still not by any
means the best way to do this).

jameskuyper, Nov 4, 2008
12. ### Phil CarmodyGuest

Ben Bacarisse <> writes:
> Phil Carmody <> writes:
>
>> Ben Bacarisse <> writes:
>>> Flash Gordon <> writes:
>>>
>>>> wrote, On 04/11/08 11:39:
>>> <snip>
>>>>> for(i=1;i<=count;++i){
>>>>> number=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

....
>>>>
>>>> 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!

By beginning my post with the conjunction "and" immediately after
your statement I was, I thought explicitly, but perhaps it was
only implicitly, confirming and agreeing with your post.

> 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).

You are quite correct with that statement - well spotted.

Better?

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

Phil Carmody, Nov 5, 2008
13. ### Guest

On 11ÔÂ4ÈÕ, ÏÂÎç7Ê±39·Ö, 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=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.

Think you very much every one!I have learned a lot from your
response.my original idear is to change a binary number into its
decimal equivalent.Here are my totle code:
/*chang a binary number to a decmal number*/
#include<stdio.h>
#include<math.h>

int main()
{
int integer;/*input number*/
int i;/*counter*/
int count;/*store the digits number*/
int number[10]={0};/*store the each digits number*/
int result=0;/*store the decmal number*/

printf("Enter the number: ");
scanf("%d",&integer);

for(i=1,count=0;i<=integer;i*=10){
count++;
}

/*caculate each digits number of the binary*/
for(i=1;i<=count;++i){
int a=pow(10,i);
number=integer % a/ pow(10,i-1);
}

/*change process*/
for(i=0;i<=count;++i){
number *= pow(2,i-1);
result += number;
}

printf("the number you want is: %d",result);

getchar();
getchar();

return 0;

}/*end function main*/
I think you can tell me more way to complete this using more simple
tools.Think you!

, Nov 14, 2008