Can someone explain the logic of this bit of code? - c

I ran it through an IDE and the remainder values came out 3, 2, 0, 1.
I understand the first remainder, but not the rest.
Also, how come the loop terminates? Isn't x always going to be greater than 0, therefore continuing indefinitely? Thank you.
int x = 1023;
while (x > 0)
{
printf("%d", x% 10);
x = x /10;
}

Note that in C, when both operands of a division have integer type, the division also has an integer type, and the value is the result of division rounded toward zero.
So in the first iteration, the statement x = x /10; changes x from 1023 to 102 (not 102.3).

since you are dividing integers you are getting rounded results each time,
so each iteration of x becomes
102
10
1
Just print x each time and you will see.
So 102 modulo 10 is 2
10 modul0 10 is 0
1 modulo 10 is 1

Related

How to handle "((9^x)-2)%5" without overflow at higher x?

I'm doing some exam prep for my discrete mathematics course, and I have to implement the function
f(x) = ((9^x)-2)%5
Since the values of our x in the assignment is 100000 < x <= 1000000, I'm having some trouble handling the overflow
In our assignment there's a hint: "Find a way to apply the modulus throughout the calculations. Otherwise you will get much too big numbers very quickly when calculating 9^x"
I cannot figure out the logic to make this work tho, any help would be appreciated
/* This function should return 1 if 9^x-2 mod 5 = 2 and 0 otherwise */
int is2mod5(int x){
int a;
double b = pow(9, x);
a = b;
int c = (a-2)%5;
if (c == 2)
{
return 1;
}
else
{
return 0;
}
}
Since you are calculating modulo 5, multiplications can be done modulo 5 as well. 9 is congruent to -1 modulo 5. Thus 9^x is congruent to (-1)^x modulo 5, i.e. 1 if x is even and -1 if x is odd. Subtracting 2 gives -1 and -3 which are congruent to 4 and 2 respectively.
Thus, f(x) is 4 if x is even and 2 if x is odd.

regard C programming code and logic building

Atually i have i understand the code but doesn't understand the logic of the code.I know what is going on but i doesn't know what is happening.The code is of even or odd.The code is.
#include<stdio.h>
int main() {
int i;
for(i=1; i<26; i++) {
if(i%2==1)
printf("%d\n",i);
}
return 0;
}
output:
1
3
5
7
9
11
13
15
17
19
21
23
25.
1st repitition:-
(i=1;i<26;i++)
if(i%2==1)
1%2==1
but the result of 1%2 is 0.
I am still getting 1 on my screen. I don't know how I get this '1' , even 1 is
not equal to 0.
2nd repitition:-
(i=1;i<26;i++)
if(i%2==1)
now, at 2nd repition i is 2. AND (2%2) is equal to 0 and 0 is not equal to 1 as follows (0==1) and i am getting 3 on my output screen as i mention above on my screen.
Either I didn't use if condition. how i get this 3 and so on.
The modulo operator gives your the remainder after integer division. Integer division is different to standard (floating point) division.
1/2 = 0.5 (floating point division)
1\2 = 0 (integer division)
Integer Division
Integer division tells you the total number of times the second number 'fits' inside the first number. It's more obvious with a bigger example:
10/3 = 3.333 etc.
10\3 = 3
You cannot fit another entire 3 into 10.
Modulo (the complement of Integer Division)
Modulo tells you how much space you have left (the remainder). After you put three 3s into 10, you have 1 space left.
10%3 = 1 (i.e. 10-3*3)
If you used 11 instead of 10, you would have 2 spaces left. If you used 12, you could fit another whole 3 in, leaving no space left.
10\3=3 (with 1 space remaining)
11\3=3 (with 2 spaces remaining)
12\3=4
13\3=4 (with 1 space remaining)
Modulo gives you the amount of space remaining:
10%3=1
11%3=2
12%3=0
13%3=1
etc.
Modulo by 2 (to assess even/odd)
In your example, you're doing modulo 2. You cannot fit any whole 2s into 1.
1\2=0 (quotient)
1%2=1 (remainder)
So... In your loop:
i=1 > 1%2=1 > 1==1 (true) > print 1
i=2 > 2%2=0 > 0==1 (false) > do nothing
i=3 > 3%2=1 > 1==1 (true) > print 3
i=4 > 4%2=0 > 0==1 (true) > do nothing
Modulus operator % in this case returns 0 if the number is even and 1 if the number is odd. So, your code:
for(i=1; i<26; i++) {
if(i%2==1)
printf("%d\n",i);
}
checks if the number is odd, and if it is, prints it out. That's the logic behind the modulus operator.
I would suggest in later use to modify this code into:
for(i=1; i<26; i++) {
if(i%2!=0)
printf("%d\n",i);
}
to avoid mistakes with negative numbers.
Read this!
I don't know which part of the comments you didn't understand, but the gist is:
1%2 is NOT 0, but 1.
Either you accept that, and then the program's behavior should be clear, or you don't accept it, and then you did not understand what the % operator does. Go and read up on it.

How would I format a while(loop)?

I'm having trouble understanding the format a while(loop) uses.
How would I go about making one for example one that computes the sum of all multiples of 5 and 9 between a set range of numbers?
while STATEMENT:
CODE BLOCK
The basic format of any while loop is such that "CODE BLOCK" is executed as long as "STATEMENT" holds true.
For example:
x = 6
while (x > 5) and (x < 20):
x = x + 1
As long as x is greater than 5 and less than 20 x will be incremented by one. The loop will terminate when x is equal to twenty. If I had initialized x to 0, the loop would never enter the block and x will remain zero.
Hopefully that helps

Fixed point code division understanding

Code for division by 9 in fixed point.
1. q = 0; // quotient
2. y = (x << 3) - x; // y = x * 7
3. while(y) { // until nothing significant
4. q += y; // add (effectively) binary 0.000111
5. y >>= 6; // realign
6. }
7. q >>= 6; // align
Line 2 through 5 in the FIRST execution of while loop is effectively doing
x*.000111 (in decimal representation x*0.1), what it is trying to achieve in subsequent while loops?
Should it not be again multiplying that with 7 and again shifting instead
of doing only shifting to take care of recurrence?
Explanation with respect to plain decimal number multiplication as to what is being achieved with only shifting would be nice.
Detailed code explanation here:
Divide by 9 without using division or multiplication operator
Lets denote 7/64 by the letter F. 7/64 is represented in binary as 0.000111 and is very close to 1/9. But very close is not enough. We want to use F to get exactly to 1/9.
It is done in the following way
F+ (F/64) + (F/64^2) + (F/64^3) + (F/64^4)+ (F/64^5) + ...
As we add more elements to this sequence the results gets closer to 1/9
Note each element in the sequence is exactly 1/64 from previous element.
A fast way to divide by 64 is >>6
So effectively you want to build a loop which sums this sequence. You start from F and in each iteration do F>>6 and add it to the sum.
Eventually (after enough iterations) the sum will be exactly 1/9.
Ok now, you are ready to understand the code.
Instead of using F (which is a fraction and cannot be represented in fixed points) the code multiplies F by x.
So the sum of the sequence will be X/9 instead of 1/9
Moreover in order to work with fixed points it is better to store 64*X*F and the result would by 64*X/9.
Later after the summation we can divide by 64 to get the X/9
The code stores in the variable y the value of F*x*64
variable q stores the sum of the sequence. In each loop iteration we generate the next element in the sequence by dividing the previous element by 64 (y>>=6)
Finally after the loop we divide the sum by 64 (q>>=6) and get the result X/9.
Regarding you question. We should not multiply by 7 each time or else we will get a sum of the sequence of
F+ (F^2) + (F^3) + (F^4) + (F^5)...
This would yield a result of ~X/(8+1/7) instead of X/9.
Shifting by one to the left multiplies by two, shifting by one to the right divides by two. Why?
Shifting is the action of taking all the bits from your number and moving them n bits to the left/right. For example:
00101010 is 42 in Binary
42 << 2 means "shift 42 2 bits to the left" the result is
10101000 which is 168 in Binary
we multiplied 42 by 4.
42 >> 2 means "shift 42 2 bits to the right" the result is
00001010 which is 10 in binary (Notice the rightmost bits have been discarded)
we divided 42 by 4.
Similarly : (x << 3) is x * 8, so (x << 3) - x is (x * 8) - x => x * 7

Unclear about modulus function c language

Here is a C language function, which I am having a little trouble understanding. I need to show what values of r come out when I input values of 6 or 10 or 13 into the function:
int factor(int val){
int r=val-1;
while(val%r){
r--;
}
return r;
}
I'm not sure if I misunderstood the question but wouldn't the remainder always be true? Since 0 = false and the while statement never reaches 0 because r is always smaller than val and not equal to it, and each time r decreases the remainder just gets bigger?
Edit: Just realized i forgot to account that 6%3 = 0! thanks for the help people who helped!
Test for yourself: 4 % 2.
The result should be 0 as the % operator returns the remainder of a division.
while(x % y) {} translates in this context to something like: as long as x is not dividable by y, do something, whereby 'do something' is decrease y in your case.
Essentially, the function returns the largest factor of the number input to the method.
It translates to "while there is a remainder from division (r is NOT a factor of val), decrease r by 1 and check again. Once the clean factor is found (val % r is 0), return the factor (r).
Here's a JavaScript port with sample output:
http://jsfiddle.net/43HxX/2/
I rewrote it in JS and you can uncomment the alert(...) line to see the output for 6, 10, and 13

Resources