Help with C.
Go to solution
Solved by Art Vandelay,
I'm tryna make sense out of this code but I can't seem to fully understand it...can you please explain this further? just the printf bit at least...
I think it works out to:
// % is the modulo operator if ((x+1)%7 != 0) //(x+1)%7?"\t%d":"\t%d\n" this is a ternary operator, basically if-else on the same line. 0 means false, not 0 means true in a boolean expression, you don't need a comparison. { if ((x/7) % 2 == 1) //x/7&1?6-x%7:x%7 another ternary operator. & is the logical AND operation. doing &1 checks the last binary digit, which is 1 if odd, 0 if even. { printf("\t%d",(arr)[x/7*7 + 6-x%7]); //x/7 is the row at which the point is. Adding to the variable arr is the same as adding the same number to the index //this is because arr is the memory index of the first element of the array, and adding 1 to arr iterates //the memory index by 1, pointing to the next index //note that iterating the memory pointer by 1 moves you 1*(the size of the data type) in memory } else { printf("\t%d",(arr)[x/7*7 + x%7]); } } else { if ((x/7) % 2 == 1) { printf("\t%d\n",(arr)[x/7*7 + 6-x%7]); } else { printf("\t%d\n",(arr)[x/7*7 + x%7]); } } }
Hopefully that helps, and please never use modulo ternary operators in your code. They are very irritating to read.
edit: I said modulo instead of ternary by accident. I don't really proof read.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now