Question

In: Computer Science

How many + operations occur when the following pseudocode is executed? What is the output? s...

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

Solutions

Expert Solution

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!


Related Solutions

How many times is line (5) executed in the following pseudocode? Enter your answer in the...
How many times is line (5) executed in the following pseudocode? Enter your answer in the box below. NOTE: Please read the pseudocode very carefully. (1) n=14n=14 (2) m=16m=16 (3) for i=1i=1 to n+2n+2 (4) ---- for j=1j=1 to mm (5) -------- print (i,j)
When the following statements are executed, what is the output from the child process? You may...
When the following statements are executed, what is the output from the child process? You may assume all variables have been properly declared and all function calls are successful. var2 = 200; var3 = 300; var1 = fork(); if ( var1 > 0 ) { printf("%d\n", var2); } else { printf("%d\n", var3); } 200 1 0 300 100 A process that has terminated, but whose parent has not yet called wait(), is known as a __________ process. zombie terminated false...
assume that the following pseudocode is executed in a program module main declare x = 1...
assume that the following pseudocode is executed in a program module main declare x = 1 declare sum = 0 declare max = 5 while ( x < max) Display "x is ", x set x = x + 1 set sum = s + x end while end module after the execution of this program the final value of x is_____ and the final value of sum is_____
The following reaction can occur when a match is struck: KClO3(s) + 3 P4(s) ------- >...
The following reaction can occur when a match is struck: KClO3(s) + 3 P4(s) ------- > 3 P4O10(s) + 10 KCl(s) (a) How many grams of KCl(s) can be produced from 38.56 milligrams of P4 assuming that we have a sufficient amount of KClO3 present? (b) In a separate experiment, how many molecules of P4O10(s) can be obtained from 0.00625 moles of KClO3? Assume we have a sufficient amount of P4.
C++ Questions What is the output of the following pseudocode code: ages = new List Append(ages,...
C++ Questions What is the output of the following pseudocode code: ages = new List Append(ages, 55) Append(ages, 88) Append(ages, 66) Print(ages) A. 55, 66, 88 B. 55, 88, 66 C. 55, because there is no loop D. 66, because there is no loop E. None of the above. QUESTION 2 Type the list after the given operations. Each question starts with an empty list. Type the list as: 5, 7, 9 Append(list, 3) Append(list, 2) Append(list, 1) Remove(list, 3)...
What is the exact output of the following pseudocode segment? METHOD MAIN CALL myMethod (0,2) CALL...
What is the exact output of the following pseudocode segment? METHOD MAIN CALL myMethod (0,2) CALL myMethod (3,5) CALL myMethod (6,7)   END MAIN Answer: METHOD myMethod(A,B) BEGIN     WHILE (A < B)          PRINT(A + " ")          A ← A + 1     ENDWHILE     PRINTLINE(); END MyMethod
2. Following are three possible reactions that could occur when a sample of KClO3 (s) is...
2. Following are three possible reactions that could occur when a sample of KClO3 (s) is heated. One goal of this lab is to determine the correct one. Balance all three reactions. a. ___ KClO3 (s) ___ KClO2 (s) + _____ O2 (g) __________b. ___ KClO3 (s) ___ KClO (s) + _____ O2 (g) __________ c. ___ KClO3 (s) ___ KCl (s) + _____ O2 (g) __________  
What is translation, where does it occur? How many ribosomal subunits are there, and where are...
What is translation, where does it occur? How many ribosomal subunits are there, and where are they constructed? What are the P and A sites? What occurs during initiation, elongation, and termination and what are the three steps of elongation. What is the function of the stop codon?
What s bakery output? What goals determine bakery output?
What s bakery output? What goals determine bakery output? 
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT