Calculate Frequency Count for each statement in the following code - c

How do I calculate the frequency count of each statement (i.e. the number of time each statement gets read/executed) in the following C code.
The frequency count of each statement must be written in terms of 'n'.
int dividable(int n){
for(int i=3 ;i< = n ; i+=3)
if(n%i = 0){
cout << (“dividable\n”);
return 0;
}
return 1;
}

Suppose the code is corrected, changing < = to <=, n%i = 0 to n%i == 0, and cout to printf:
int dividable(int n)
{
for (int i = 3; i <= n; i += 3)
if (n%i == 0)
{
printf("dividable\n");
return 0;
}
return 1;
}
If n is less than 3, the for is executed once but immediately completes without executing any iterations of the body of the loop.
Otherwise, in the first iteration of the loop, i is 3. Then if n is a multiple of 3, n%i is 0, the if test is satisfied, and the routine prints “dividable” and returns 1.
If n is not a multiple of 3, then n%i is never 0, because i is always a multiple of 3. Then the test is never satisfied, all loop iterations are executed, and the routine returns 0.
Thus we have:
If n is less than 3, the for is executed once, the return 1; is executed once, and the other statements are executed zero times.
Otherwise, if n is a multiple of 3, the for is executed once (partially), the statements starting with if, {, printf, and return 0; are executed once each, and the return 1; is executed zero times.
Otherwise, the for is executed once, the if is executed floor(n/3) times, the statements inside the if are executed zero times, and the return 1; is executed once.

Related

How does 'for' work when all of its "parts" are 'i++'?

I've used Code::Blocks to find the result, and it gives me 2 at the end, where i + j = 1 + 1.
#include <stdio.h>
int main(void) {
int i = -1, j = 1;
for(i++; i++; i++)
j++;
printf("%d",i + j);
return 0;
}
How does i get to be 1, and why was j not incremented?
You start with i == -1, j == 1.
Then you run the first i++, which will make i == 0. The result (which is the original value of i) is unused.
Then you test for the first time in i++. Since i is already 0, the test is false, so the for loop will quit. Now the post-increment runs, which makes it i == 1, and the loop exits.
Note how j++; was never reached!
To see it more clearly, you can think about the for loop in the following equivalent way:
i++; // init statement
while (i++) { // condition
j++; // body of the for loop
i++; // iteration expression
}
Here's the order in which it executes:
i is -1 and j is 1
i is incremented (after which i == 0)
The loop checks if i != 0. Since i is 0 at this point, the contents of the loop are skipped.
i is incremented again (after which i == 1)
The code prints i + j, which is 2 because j is unchanged and i is 1.
Here's a program with a while loop that does the same thing as the program in your question:
int main(void) {
int i = -1, j = 1;
i++;
while(i++)
{
j++;
i++;
}
printf("%d",i + j);
return 0;
}
To directly answer your question:
i is 1 afterwards because it is incremented twice from an original value of -1.
j is not incremented because the contents of the loop are skipped.
Let me first explain how for loop execution happens:
The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the for loop.
Increment operator: value of i increments after execution if it is i++
Explanation of your code:
for(i++; i++; i++)
At init step i++ been executed but i will be still 0 until it get to condition step of for loop.
i becomes zero it means condition is false but at same time one more i++ already executed.
As condition is false for loop statement will never be executed and value of i will be 1 and value of j remains 1.

Loop within loop does not run

This code should count the number of prime numbers based on user input. If user input is 10, then I should get 4. However, I only get 0. Why does the second loop not run?
#include<stdio.h>
#include<math.h>
int main()
{
int N;
scanf("%d", &N);
int numprime;
numprime = 0;
int P=1;
for (P; P<N; P++)
{
if (P%2==0)
continue;
int e = sqrt(P);
for (int j=3;j<=e;j+=2)
{
if (P%j!=0)
{
numprime = numprime + 1;
}
else
{
continue;
}
}
}
printf("%d", numprime);
}
Why does the second loop not run?
It does run, once, when e is equal to 3 (that means that this happens when P is equal to 9), in the case that input is 10.
Change this:
for (P; P<N; P++)
to this:
for (; P <= N; P++)
since the first part had no effect, and more importantly, you want to check all N numbers, so the stop condition had to be modified.
Then, you need to change this:
for (int j=3;j<=e;j+=2)
to this:
for(int j = 2; j <= e; j+=2)
in order to check for prime numbers, are suggested here.
Furthermore, you need to change this:
if (P % j != 0)
to this:
if (P % j == 0)
since P is actually a prime number when the condition is met.
Moreover, you need to get rid of this:
if(P % 2 == 0) continue;
since for example 2 is a prime number, but it meets this condition, and will not be taken into account!
From your code, it looks like P starts out as 1, so e is sqrt(1), which is again 1, and j is 3. Therefore, j is not less than or equal to 1, and the test at the top of the inner loop fails, so the loop doesn't execute the first time through. This pattern continues for each iteration of the outer loop until P becomes large enough that sqrt(P) is greater than 3, at which point the inner loop does run.

Can anyone tell me why this program go to infinite times?

#include< stdio.h>
int main()
{
int n,a=0,i=1,b=11;
while(i<=2)
{
while(i>0)
{
a=a+b;
i--;
}
printf("%d",a);
i++;
}
}
but if i make a little change i will get the output..
what is the difference between both the code??
#include< stdio.h>
int main()
{
int n,a=0,i=1,b=11;
while(i<=2)
{
n=i;
while(n>0)
{
a=a+b;
n--;
}
printf("%d\n",a);
i++;
}
}
output-
11
33
while (i <= 2)
{
while (i > 0)
{
a = a + b;
i--; <- out the inner while loop when i = 0
}
printf("%d", a);
i++; <- at here, the i==0 each time, so infinity loop
}
Because your nested loop always restores the value of i to 0,
And 0 <= 2 is always true, thus it keeps on going.
Initially the value of i is 1, the first loop starts by checking i<=2 , which is true for i=1, then the second loop checks i>0, which is also true, then the second loop decreases the value of i to 0 by i--;
This time the test condition for the nested loop fails and the inner loop exits, back in the first loop, the condition is satisfied as i=0 is<= 2 thus i is incremented, now i = 1.
This keeps on going forever.
In the second code, you're copying the current value of i to n, thus, initially i = 1;
Condition for first loop satisfied, then you set n = i,
And check n >0 which is true as n = i = 1
In this loop you are decrementing n and n becomes 0, thus the loop quits, and the outer loop increments i, i now being 2 allows the outer loop to run and then again n = i = 2,
The inner loop runs twice and then exits and i is incremented to 3 failing the outer loop condition and hence quitting the loop. And you get the result.
In the first code your
While(i<=2) never ends because you add 1 to i with i++ and then you subtract 1 from i with i-- and you never get i=3 to end the while loop.

Project 7 Euler C

I'm stuck at Question 7 of Project Euler. I have this piece of code.
#include <stdio.h>
int main (void)
{
int contador = 0, i, n, variavel = 0;
for (i = 0; contador == 1000; i++)
{
for(n = 0; n == i; n++)
{
if (i % n == 0)
{
variavel = i;
contador++;
}
}
}
printf("%d\n", variavel);
}
It always prints 0. Why is that?
PS: I wrote 1000 but the answer must be the 10001st prime number.
A for loop consist of 3 parts
for( init ; cond ; step )
When the execution reaches the loop,
init is executed.
cond is evaluated.
If false, break the loop
If true, proceed to the next step
Execute the body of the loop.
Do step(in many cases, this is increment/decrement)
Goto step 2
So, in your code, when the execution reaches
for (i = 0; contador == 1000; i++)
i is set to 0. Then the condition contador == 1000 is checked. It is false as contador is initialized to 0. So, the loop breaks and the execution reaches the printf which prints the value of variavel which is 0 and then
return 0;
executes. This ends the execution of your program.
Your inner for loop has a somewhat similar issue. If the condition of the outer for loop is corrected, then the inner for loop executes. n is set to zero and then, the condition n==i is checked. It will be true only when i=0,i.e, it will be true only in the first iteration of the outer for loop.
You need to correct these mistakes.
The reason it is always zero is that the inner loop condition is satisfied in the first iteration:
for(n=0; n==i; n++)
Additionally, your outer loop will never run. It is written to say that it should only loop when contador == 1000, which can never happen as written.

What does "for (; --i >= 0; )" mean in C?

What does for (; --i >= 0; ) mean in C?
How is the counter getting decremented and how is it different from for ( ; i >= 0; --i)?
They are very similar, but not the same! First you have to understand how a for loop in C gets executed:
As an example:
1 2, 5 4
| | |
v v v
for(i = 0; i < 5; i++) {
// Do something <-- 3
}
As you can see 2, 3, 4, 5 is a loop until the condition is false.
Now you should clearly see how the for loop gets executed. The difference now is that in your first example:
int i = 5;
for ( ; --i >= 0; )
printf("%d\n", i);
The output would be:
4
3
2
1
0
Because after the first check of the condition (Point 2), it executes the code block of the for statement and i already gets decremented.
In your second example:
int i = 5;
for( ; i>=0; --i)
printf("%d\n", i);
The output would be:
5 // See here the difference
4
3
2
1
0
Here you get the difference, because it gets decremented in Point 4, so the first time it runs with the value 5.
These constructs are formally equivalent to
while (--i >= 0)
{
Body;
}
and
while (i >= 0)
{
Body;
--i;
}
Do you better see the difference?
In general, we can convert any for loop or while loop into a set of mostly linear statements with a goto. Doing this may be helpful to compare the code.
Case 1
### for (; --i >= 0; ) { statements; }
; // Initializer statement
start:
bool condition = (--i >= 0); // Conditional
if (condition) { // If true, we execute the body as so:
statements; // The statements inside the loop
; // Empty increment statement
goto start // Go to the top of the loop.
}
Case 2
### for( ; i>=0; --i) { statements; }
; // Initializer statement
start:
bool condition = (i >= 0); // Conditional
if (condition) { // If true, we execute the body as so:
statements; // The statements inside the loop
--i; // Increment statement
goto start; // Go to the top of the loop.
}
Let's compare the "simpler" code from those two cases.
In the first case, we decrement i for the first time before each loop body. In the second case, we decrement i after each loop body.
The easiest way to see that is to consider what happens when you enter this loop when i == 0. In the first case, after this block of code, you would have a resulting value of i == -1. In the second case, i wouldn't have changed (that is, i == 0), because we never reached the increment statement.
for (; --i >= 0; ) works like this:
Say if you have i as 10, it will decrement the i value and will compare 9 >= 0 and so on.
So the output would be like 9, 8... till 0.
While the loop for( ; i>=0;--i) would first go to 10, then decrement the value and then it would check i>=0. So the output would be 10, 9, 8... till 0.
The first one decrements i and then checks the condition.
The second one checks the condition first and if true, decrements i after the loop body has been executed.
Well, it is the same as saying for( i=0 or some value; --i <= 0 ; do nothing){}
and --i is an predecrement operator.
That means when this piece of code, i.e., --i, is being read, the value of i will decrease by 1 at the same moment.
There are three main components in a for loop.
Initialization, condition, and afterthought.
Initialization happens once at the start of the entire statement. Condition happens before every cycle. Afterthought comes after every cycle. Consider i starts at 2:
for (; --i >= 0; )
Initialization: i = 2
Condition: i = 1
Afterthought: i = 1
Condition: i = 0
Afterthought: i = 0
Condition: i = -1
Exit
In the other case
for( ; i>=0; --i)
Initialization: i = 2
Condition: i = 2
Afterthought: i = 1
Condition: i = 1
Afterthought: i = 0
Condition: i = 0
Afterthought: i = -1
Condition: i = -1
Exit
You can see that the second version actually lasts one cycle longer!
The three parameters (initialization, condition and increment) in for are optional (but they still requires the semicolon ;). So, you can safely write for(;;) and it's the same as while(1). So, expressions like
for(; x > y; x++) { ... }
for(;1;x++) { if(x > y) break;
for(;;) { if(x++ > y) break;
for(;x++ > y;) { ... }
Are valid and equivalent. In this, the increment occurs in the conditional parameters, just like in your code example.

Resources