recategorized by
685 views
4 votes
4 votes

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 ≥ 3, and TwoLog_n is initialized to the value of $2^*\lceil \log_2(n) \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 someone go step by step and say me what is happening here?

[All a[j] will get !=0 value here,rt?]

Plz explain

recategorized by

2 Answers

1 votes
1 votes
A[j] = 0, if j % k == 0, for j >= 3 && j <= N and k >= 2 and k <= $2*\log_2N$.

Else A[j] = 1. Once some A[j] becomes 1 it will remain that way through out the execution.

This means that it checks for all j in the range 3 to N, wether that j is divisible by all k from 2 to $2*\log_2N$.

For indices satisfying above criteria, A[j] will be 0 an for the rest it will be 1.

The printing loop at the end will print those indices where A is 0.

 

Finally the loop will not print anything. Check comments below.
edited by
1 votes
1 votes

Say n=6

Now, in at last loop we are printing j=3 to 6

which all are non zero.

We will print those j which are !A[j]=1

means A[j]=0

But in j=3 to 6 there is no such value.

So, print nothing

Related questions

0 votes
0 votes
2 answers
1
Debargha Mitra Roy asked Apr 10
97 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
3 votes
3 votes
3 answers
2
Laxman Ghanchi asked May 19, 2023
1,154 views
#include<stdio.h void print(int n) { printf("Hello "); if(n++ == 0) return ; print(n); n++; } int main() { void print(); print(-4); }How many times printf execute?? And H...
0 votes
0 votes
1 answer
3
Laxman Ghanchi asked May 19, 2023
682 views
#include<stdio.h>void print(int n){ printf("Hello "); if(n++ == 0) return ; print(n); n++;}int main(){ void print(); print(-4);}