Hi, i have a weird assignment. I made a program, but my teacher wants -- for the sake of research -- to let me prove that % works how I intend it to work. Where in the include or library files can I find how exactly % is defined?
Are you referring to the % operator (modulus) or trying to find percentages (%) of a number?
If it is the modulus operator then it is not defined in a library (as far as I am aware) because it is an inbuilt C(++) operator.
If you give us a little more information of what you need to do to prove that it works, we can help more.
ISO/IEC 14882:2003 : Programming languages -- C++, 5.6.4: ISO, IEC, 2003. "the binary % operator yields the remainder from the division of the first expression by the second. .... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined"
I also can run a simulation wich somehow shows it, but doenst prove. So I hoped if I can show him the code (of how % is implemented) it will be prove enough. So if it's an inbuild C operator, is there any way I can digg out the code?
I do not know of any way of getting the exact code of it but you could probably "prove" it using a small equation (below). You cannot really prove that it works, really, because it is just a mathematical operator, could you prove that "+" works?
You can show that it would give the same result, however.
% gets the remainder of a number. So, in C(++):
a % b = a - ((a / b) * b)
Where a is the higher number... something like that might be alright.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h>
int main()
{
int a,b;
printf("Enter number 1: ");
scanf("%d",&a);
printf("Enter number 2: ");
scanf("%d",&b);
printf("%d %% %d == %d",a,b,a%b);
printf("\n%d - ((%d / %d) *%d) == %d",a,a,b,b,a-((a/b)*b));
getchar();
getchar();
return 0;
}
You should probably state that a-((a/b)*b) uses integer division. Furthermore, a need not to be the higher number. For example for a=2 and b=4,
2%4 = 2
2-((2/4)*4) = 2-(0*4) = 2