Question

A) Rewrite the following pseudocode segment in C++ using the loop structures (for and while). Assume that all the variables a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

A.cpp( for )

//including header files
#include<iostream>
using namespace std;

int main()
{
//   declaring j,k and i and initializing j with 50
   int j=50,k,i;
  
//   adding 13 in j and dividing 27 after that and assigning to k
   k=(j+13)/27;
  
//   infinite loop
   for(;;)
   {
//       if k is greater than 10 then jump to out label
       if(k>10)
       goto out;
      
//       adding 1 with k and assign it in k
       k = k+1;
//       multiplying 3 with k after that substract 1 with k and assign it in i
       i = 3 * k - 1;
   }
//   jump label
   out:
  
return 0;
}
A.cpp(while)

// including header files
#include<iostream>
using namespace std;


int main()
{
//   declaring j,k and i and initializing j with 50
   int j=50,k,i;
//   adding 13 in j and dividing after that and assigning to k
   k=(j+13)/27;
//   infinite loop
   while(1)
   {
//       if k greater than 10 then jump to out label
       if(k>10)
       goto out;

//       adding 1 with k and assign it in k
       k = k+1;
//       multiplying 3 with k after that substract 1 with k and assign it in i
       i = 3 * k - 1;
   }
//   jump label
   out:
  
return 0;
}
B.cpp(switch)

// including header files
#include<iostream>
using namespace std;

int main()
{
//   declaring k and j and initializing k with 1 and j with 0
   int k=1,j=0;
  
  
   switch(k){
      
//       if case 1 and case 2 matches then j = 2 * k - 1 and break;
       case 1:
       case 2:
           j = 2 * k - 1;
           break;
//       if case 3 and case 5 matches then j = 3 * k + 1 and break;
       case 3:
       case 5:
           j = 3 * k + 1;
           break;
//       if case 4 matches then j = 4 * k - 1 and break;
       case 4:
           j = 4 * k - 1;
           break;
//       if case 6, case 7 and case 8 matches then j = k - 2 and break;
       case 6:  
       case 7:
       case 8:
           j = k - 2;
           break;  
   }
  
return 0;
}

B.cpp(if-else)

// declaring header files
#include<iostream>
using namespace std;

int main()
{
//   declaring j, i and initializing j with -3 and i with 0
   int j = -3, i = 0;
  
//   loop until i less than 3
   for(i = 0; i<3; i++)
   {
       switch(j+2){
//       if case 3 and case 2 matches then j = j-- and jump to out label;  
           case 3:
           case 2:
               j--;
               goto out;
//       if case 0 matches then j=j+2 and jump to out label;  
           case 0:
               j+=2;
               goto out;
//           default j = 0
           default:
               j=0;  
       }
      
//       if j is greater than 2 then jump to goto label
       if(j>2)
           goto out;
//       substract i with 3 and assign it to j
       j = 3 - i;
   }
  
//   label
   out:
      
return 0;
}


C.cpp

// declaring header files
#include<iostream>
using namespace std;

int main()
{
//   declaring j, i and initializing j with -3 and i with 0
   int j = -3, i = 0;
  
//   loop until i less than 3
   for(i = 0; i<3; i++)
   {
       switch(j+2){
//       if case 3 and case 2 matches then j = j-- and jump to out label;  
           case 3:
           case 2:
               j--;
               goto out;
//       if case 0 matches then j=j+2 and jump to out label;  
           case 0:
               j+=2;
               goto out;
//           default j = 0
           default:
               j=0;  
       }
      
//       if j is greater than 2 then jump to goto label
       if(j>2)
           goto out;
//       substract i with 3 and assign it to j
       j = 3 - i;
   }
  
//   label
   out:
      
return 0;
}

Code output:

A.cpp(for)

D:\HomeworkLib\October 69\Afor.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window Help Afor.cpp Bswitch.c

A.cpp(while)

х CcPP D:\HomeworkLib October 269\A.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window Help Afor.cpp Bswi

B.cpp(swtich)

х D:\HomeworkLib\October (69\Bswitch.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tools Style Window Help Afor.cpp [*]

B.cpp(if-else)

х D:\HomeworkLib\October 269\B.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window Help Afor.cpp Bswitch.c

C.cpp

х D:\HomeworkLib\October 269\C.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window Help Afor.cpp Bswitch.c

Х j--; D:\HomeworkLib\October 269\C.cpp - Dev-C++ 5.11 File Edit Search View Project Execute Tools AStyle Window Help Afor.cpp Bswi

Add a comment
Know the answer?
Add Answer to:
A) Rewrite the following pseudocode segment in C++ using the loop structures (for and while). Assume...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CS-320 -- Programming Exercise 1 Create a program in Python that rewrites the following pseudocode segment...

    CS-320 -- Programming Exercise 1 Create a program in Python that rewrites the following pseudocode segment using a loop structure: k = (j + 13) / 27 loop: if k > 10 then goto out k = k + 1 i = 3 * k – 1 goto loop out: ...

  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • Need in Python! Consider the following program and expected output. Rewrite it using a for loop...

    Need in Python! Consider the following program and expected output. Rewrite it using a for loop and a conditional break statement such that it has the same output.   HINT: Stay with i as the main loop counter and let j control the break behavior.   i = 0 while i < 10 and j < 10 : j = i * 2 print(i, j) i += 1 # Output below, for reference - not part of the program! 0 0 1...

  • 41. Executing the "continue" statement from within a loop     causes control to go     A....

    41. Executing the "continue" statement from within a loop     causes control to go     A. to the next line of code     B. out of the loop     C. to the beginning of the loop          D. to check the loop condition for repeating the loop     E. none of the above      42. The 'default' statement inside a 'switch()' is optional.          T__   F__             43. The following 'switch' implementation is legal:              int i = 2;        ...

  • 39. Rewrite the following C code segment in x86 (assume bs kebx) x86 switch (b) case...

    39. Rewrite the following C code segment in x86 (assume bs kebx) x86 switch (b) case 1 case 2: case 3: default: return Walpha" return "beta" return gamma" return "delta" 40. Does the following function definition qualfy as a leaf function? Why or why not? int quadratic(int x, int a, int b, int c) return a square (x) b 41. Assuming the GCC calling conventions are being used, what instructions always start the prolog of a function? 42. Assuming the...

  • Rewrite the if statement below using only the relational operator < in all conditions

     Rewrite the if statement below using only the relational operator < in all conditions. Assume that the value of score is between 0 and 100 inclusive. Rewrite the following code segment as an equivalent segment that uses a do-while statement. Assuming the following declaration: char my array[9] = ['C', 'p', 't', 'S', '', '1', '2', '1'); What is the value of *(myarray, + 4)? What will be the values of k[1], k[2], and k[3] after execution of the code segment below assuming the input data shown?

  • Draw a picture of array A, showing the values stored in it after execution of each...

    Draw a picture of array A, showing the values stored in it after execution of each code segment below. Use the following data: 0 1 2 3 4 5 6 7 8 9 int A[4][2]: int k, m: for (int i = 0: i < = 3: i++) for (int j = 0: j < = 1: j++) A[i][j] = 0: for (int j = 1: j < = 10: j++) { cin > > k: switch (k) { case...

  • Please try to explain the answers as well. It will be highly appreciated. Output of the...

    Please try to explain the answers as well. It will be highly appreciated. Output of the following expression: 5 * 4 % 2 = ? Output of a loop: Example: What will be the output of the Java code (Assume all variables are properly declared.) num = 10; while (num <= 32)                 num = num + 5; System.out.println(num); What will be the output of the Java code (Assume all variables are properly declared.) public class test {   ...

  • should be in C language What is the output of the following segment of C code:...

    should be in C language What is the output of the following segment of C code: void CapsLetter (char* x, charl); int main() { char* text; text="This is some sample text."; CapsLetter(text, 'e'); printf("%s", text); return 0; سی void Caps Letter (char* x, char 1) { int i=0; while (x[i]!='\0') { if (x[i]==1) x[i]=1-32; } 1. This is som sample text. 2. THIS IS SOME SAMPLE TEXT. 3. This is some sample text. 4. this is some sample text. What...

  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT