In: Computer Science
How many + operations occur when the following pseudocode is executed? What is the output? s ← 0, t ← 0 for i ∈ {1, ..., 6} do for j ∈ {1, ..., 6} do for k ∈ {1, ..., 6} do if i + j + k = 9 then t ← t + 1 s ← s + 1 print t / s
code in C language corresponding to above pseudo code is:
case1
#include <stdio.h>
int main(void) {
int s=0, t=0;
int countOfPlus=0;
int i,j,k;
for(i=1;i<=6;i++)
{
for(j=1;j<=6;j++)
{
for(k=1;k<=6;k++)
{
countOfPlus=countOfPlus+2; // checking of if condition
every time
if(i+j+k ==9)
{
t=t+1;
countOfPlus++; // when incrementing t
s=s+1;
countOfPlus++; // when incrementing s
}
countOfPlus++; //loop increment of k
}
countOfPlus++; //loop increment of j
}
countOfPlus++; //loop increment of i
}
printf("%d\n",t/s);
printf("%d",countOfPlus);
return 0;
}
count of + operation is 740
output is 1
case 2:
#include <stdio.h>
int main(void) {
int s=0, t=0;
int countOfPlus=0;
int i,j,k;
for(i=1;i<=6;i++)
{
for(j=1;j<=6;j++)
{
for(k=1;k<=6;k++)
{
countOfPlus=countOfPlus+2; // checking of if condition
every time
if(i+j+k ==9)
{
t=t+1;
countOfPlus++; // when incrementing t
s=s+1;
countOfPlus++; // when incrementing s
printf("%d\n",t/s);
}
countOfPlus++; //loop increment of k
}
countOfPlus++; //loop increment of j
}
countOfPlus++; //loop increment of i
}
printf("%d",countOfPlus);
return 0;
}
count of + operation is 740
output is 1111111111111111111111111 i.e 25 times it will print 1 because it will enter if condition for 25 times
Count of + operations. How I calculated? Simply took a variable and increased it as mentioned in comments in code. Its simple right !
Since your pseudo code was written in 1 line with no indentations, So i was confused where this "print" statement be placed?
So I made these 2 cases. Whichever suits you , take that.
I guess case 2 is what you are looking for.
Hope you like it. Please Upvote!