Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like:
flag = (i%primes[j]);
In this case, the variable 'i', 'j' and 'flag' are integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context?
Thanks
16 108024
Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like:
flag = (i%primes[j]);
In this case, the variable 'i', 'j' and 'flag' are integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context?
Thanks
it is used of mod operator .
it is used of mod operator .
The modulo operator returns the remainder of the left side divided by the right side. So 5%2 will evalutate to 1. (5 / 2 = 2 remainder : 1).
The modulo operator returns the remainder of the left side divided by the right side. So 5%2 will evalutate to 1. (5 / 2 = 2 remainder : 1).
Hi,
I just wanted to add % operator can be used only with "integers".
Regards,
Pradeep
Hi,
I just wanted to add % operator can be used only with "integers".
Regards,
Pradeep
And it deosn't make sense if divisor is 1,but I think we all know that.
:)
Savage
Thanks everyone. I thought it would be something simple!
And it deosn't make sense if divisor is 1,but I think we all know that.
:)
Savage
It makes perfect sense, just not very useful. ;)
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm.
Adrian
It makes perfect sense, just not very useful. ;)
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm.
Adrian
I don't know how (e.g)5%1 can make perfect sense to u.
Savage
I don't know how (e.g)5%1 can make perfect sense to u.
Savage
5/1 = 5 R 0
so 5%1 = 0.
Actually n%1 = 0. Like I said, not very useful.
Adrian
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm
I've heard the negative modulus is badly implemented in some languages (not sure with the c/c++), although I'm not sure whether this is true it gives unexpected results because people don't really understand negative mod (and expect the wrong result).....
The main confusion stems from what should a negative modulus return?
Assuming negative numbers are fins in modular arithmetic
what is the remainder of 7/3?, most people would say 1, but it is in fact 2 (or 1, if you prefer it that way)
Essentially, modulus is an equivalence operator, that looks for "the equivalent of this number between zeero and n", thus for the 7%3 example above: 
let x= 7

while(x not between 0 and 3)

x=x+3;

endwhile

print x

and x = 2;
if we change this to 7%3, is the result the same?
Actually, no. we are looking for "the equivalent of this number between zero and n", that is, we wnat 7's equivalent between 0 and 3. To find this we must reduce 7 into the range, so subtracting 3 three times, we get 2.
To cut a long story short, the % operator gives unexpected results if you expect the wrong result ;)
I've heard the negative modulus is badly implemented in some languages (not sure with the c/c++), although I'm not sure whether this is true it gives unexpected results because people don't really understand negative mod (and expect the wrong result).....
The main confusion stems from what should a negative modulus return?
Assuming negative numbers are fins in modular arithmetic
what is the remainder of 7/3?, most people would say 1, but it is in fact 2 (or 1, if you prefer it that way)
Essentially, modulus is an equivalence operator, that looks for "the equivalent of this number between zeero and n", thus for the 7%3 example above: 
let x= 7

while(x not between 0 and 3)

x=x+3;

endwhile

print x

and x = 2;
if we change this to 7%3, is the result the same?
Actually, no. we are looking for "the equivalent of this number between zero and n", that is, we wnat 7's equivalent between 0 and 3. To find this we must reduce 7 into the range, so subtracting 3 three times, we get 2.
To cut a long story short, the % operator gives unexpected results if you expect the wrong result ;)
That might have been it, it doesn't look strange to me now, but it may have before. ;)
Adrian
Or I could be wrong, we'll find out soon enough
Or I could be wrong, we'll find out soon enough
While looking up the C99 standard, I found this.
Point 25 is probably what was confusing me and causing me grief as I was getting 22 % 7 == 6 instead of 1. Both are correct, but it confused me way back, though I'm not sure if I realised back then it was inconsistent between compilers.
Adrian
I stand corrected (at least in computing terms)...
Mathematically I think it makes more sense to insiast that the number is trubncated to between 0 and the divisor (that is negative if the divisor is negative, but positive if the divisor is positive).....And that the numerator is irrelevant interms of the sign of the modulus....
I can understand, though, that it is easier to define in terms of whether you shorten it toward 0 or allow shortening to plus/minus infinity as well.....
The implementation of the modulo operator to return something like this:
22 % 7 = 6
makes all the same sense as why program language (except MATLAB) often use zero as the first index in an array.
In my opinion, the modulo operation has more applications to indexing than it does the mathematical sense of a modulus. This is because in C/C++ you are more likely to use modulo for pointers and counters.
Thus, the negative interpretation of a modulo is related to circular indexing. Take for example, you want to turn an array of data into a periodic array of data. This can be done identically by saying that data i=[0,6] goes from index [6,13] would produce three repetitions of the data as would [0,20]. When using the modulo operator, 6 % 6 should wrap to zero and that the next element in the array should be 5 % 6 = 1.
Hello everyone,
anyone could please explain me what the below operation does?
mj %= MBIG;
Regards
These expressions:
are equivalent to these expressions:  mj = mj % MBIG;

aj = aj + ABIG;
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve Richter 
last post by:
What does the "." mean in the following sql script stmts?
use
GO
if exists (select * from dbo.sysobjects where
id = object_id(N'.')
and OBJECTPROPERTY(id,N'IsUserTable') = 1)
drop table ....

by: Don 
last post by:
What does the onfocus value of "this.select()" do in the following <input...> tag?
<input type=text name="img" size=20 onfocus="this.select()" value="http://" tabindex=7>
Thanks,
Don
...

by: RobertTG 
last post by:
Someone please translate the code below into English...
Particularly the indicated line
Thanks
function attachComment()
{
var aForms = document.getElementsByTagName("FORM");
for (var i = 0;...

by: allenj 
last post by:
DB21085I Instance "md" uses "32" bits and DB2 code release "SQL08012"
with level identifier "02030106".
Informational tokens are "DB2 v8.1.0.16", "s030508", "MI00048", and
FixPak "2". Product is...

by: Larry David 
last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service"....
What does "64bit" mean to your friendly...

by: Heinrich Pumpernickel 
last post by:
what does this warning mean ?
#include <stdio.h>
int main()
{
long l = 100;
printf("l is %li\n", l * 10L);

by: loudking 
last post by:
Hello, all
I don't quite understand what does ((time_t)1) mean when I execute
"man 2 time"
RETURN VALUE
On success, the value of time in seconds since the Epoch is
retu
rned.
On error,...

by: CloudSolutions 
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

by: ryjfgjl 
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...

by: taylorcarr 
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...

by: ryjfgjl 
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and timeconsuming...

by: ryjfgjl 
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...

by: BarryA 
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

by: nemocccc 
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

by: Sonnysonu 
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by columnwise with in the specific length.
suppose the i have to...

by: Hystou 
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
 