Question

In: Computer Science

4. Rewrite the following pseudocode segment using a loop structure in the specified languages: k =...

4. Rewrite the following pseudocode segment using a loop structure in the specified languages:

k = (j + 13) / 27
loop:
      if k > 10 then goto out
      k = k + 1
      i = 3 * k - 1
      goto loop
out: . . .

a. C++

b. Python

c. Ruby

Assume all variables are integer type. Discuss the relative merits of the use of these languages for this particular code.

Solutions

Expert Solution

Answer:

a. c++

for (k = ((j + 13) / 27) +1); k <= 10; k++)

{

i = 3 * k – 1;

}

In these languages syntax of loops are very efficient for the users. The for loop contains three parts here:

1) Initialization: It is the first field of for loop. Here, a variable is initialized with an initial value.

2) Condition: Here a Boolean expression is used which tells when the loop will terminate.

3) Updation: This field contains increment or decrement in the value of a variable which is responsible for execution of the loop.

b. Python:

In python for loop iterates over a list. Here a method named range() is defined also with the help of which we can define initial and final range for a loop.

temp= range((((j+13)/27)+1), 11)

for k in temp

i = 3 * k - 1

temp is a variable which is used to hold a list of all values from initial value to final.

c. Ruby:

The range in Ruby’s for loop is defined by:

1 … 8, means loop will execute 1 to 7

1 .. 8, means loop will execute 1 to 8

Do is optional in the for loop of Ruby.

for k in (((j + 13) / 27)+1) .. 10 do

i = 3 * k – 1

end

Best writability:

In C++ for loop is most flexible, and for loop in C++can be used for most complexes looping which makes it writable. The ability to place assignment statements and increment/decrement statements inside the loop provides the programmer a shortcut in writing operations which makes the for loop most writable.

In python’s for loop there is no closure reserved word is used (like end for), But it is not flexible as it iterates over the list.In ruby end closure reserved word is used which makes it hard to write.

Hence, C, C++, Java, or C# has the best writability.

Best readability:

Due to complexity of for loop in C, as assignment, increment/decrement statements are placed inside a loop, readability of for loop damaged.

The for loop in python makes it ambiguous as there is no symbol exist to indicate end of the loop.The Readability of the for loop in Ruby has nearly the same readability but in Ruby end reserve word is used to indicate end of the loop which can makes it ambiguous as user will confuse in taking decision that it is ending of for loop or for anything else.

Readability is more important than writability for a best programming language. But Ruby’s for loop has the best combination of both.

Please give thumbsup or do comment in case of any queries. Thanks.


Related Solutions

Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
How to rewrite the bin() function below using recursion instead of a for loop in C?...
How to rewrite the bin() function below using recursion instead of a for loop in C? #include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdint.h> char* bin(int x, int i); int main(int argc, char **argv) { char* binstr = bin(10, 0); printf("%s\n", binstr); free(binstr); return 0; } char* bin(int x, int i){ int bits = log10((double) x)/log10(2)+1; char* ret = malloc((bits+1)*sizeof(char)); for(int i = 0; i < bits; i++){ ret[bits-i-1] = (x & 1) ? '1' : '0'; x >>= 1;...
Describe the languages specified by the following regular expressions: 1. \\(_+)/ 2. (\(740\)...-...)|(...-...) (The alphabet is...
Describe the languages specified by the following regular expressions: 1. \\(_+)/ 2. (\(740\)...-...)|(...-...) (The alphabet is {1,2,3,4,5,6,7,8,9,0})
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
How many times is "invalid" displayed in the following loop? for (int k=o; k<3; K++) {...
How many times is "invalid" displayed in the following loop? for (int k=o; k<3; K++) { for(int j =0; j < 4; j++) { cout << "invalid"; } }
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
Write pseudocode (3 Marks) and program structure (4 Marks) for the problem given below; In a...
Write pseudocode and program structure (4 Marks) for the problem given below; In a college, students are awarded a pass grade if their total mark is between 50-59, credit grade if the mark is between 60-69, distinction for marks between 70-79. High distinction if the mark is above or equal to 80 and fail if the mark is below 50.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT