Tell me more ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

In the following C program fragment, j, k, n and TwoLog_n are integer variables, and A is an array of integers. The variable n is initialized to an integer $\ge3$, and TwoLog_n is initialized to the value of $2\lceil\log{2n}\rceil$

for (k=3; k <= n; k++)
  A[k] = 0;
for (k=2; k <= TwoLog_n; k++)
  for (j=k+1; j <= n; j++)
    A[j] = A[j] || (j%k);
for (j=3; j <= n; j++)
  if (!A[j]) printf(”%d ”,j);

Can anyone tell me what this piece of code fragment basically does??

share|improve this question
1  
Have you tried running it? – Luke Mathieson Dec 10 '12 at 7:21
yeah i did. But for evry value i give dere is no answer. dats it none of d values in array A ar zero finally – QueueTank Dec 10 '12 at 7:22
1  
This sounds like a question that is more suitable for codereview.stackexchange.com – Jernej Dec 10 '12 at 8:24
1  
@Jernej I do not think so. codereview.se is for correctness, best practices, design issues etc. Their FAQ explicitly mentions "understanding code snippets" is off-topic. Whether or not it is appropriate for cs.se is something I am not sure of. However, I have seen quite a few similar questions asked and answered here. – Paresh Dec 10 '12 at 9:58

closed as off topic by Dave Clarke, AProgrammer, Realz Slaw, AJed, Merbs Dec 10 '12 at 22:11

Questions on Computer Science Stack Exchange are expected to relate to computer science within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

It appears that it attempts to print all integer values between $3$ and $n$ (inclusive) for which each is completely divisible by all integers in the range $[2,2 \cdot \lceil log_2(n)\rceil]$ less than itself. A finer point is that it preserves the first non zero remainder value discovered in the corresponding array element instead of overriding it with subsequent non zero values. This may simply be an unintended result of coding "shorthand."

share|improve this answer
1  
I think you misunderstand the behaviour of || in C: it is defined as returning 0 or 1. – AProgrammer Dec 10 '12 at 20:52
@AProgrammer Yes, my mistake the expression would always express boolean making the second point moot. – tandem5 Dec 11 '12 at 2:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.