Question

In: Computer Science

11. Read about how flexible for-loops are in C. Write two examples of "non-traditional" for-loops (ie,...

11. Read about how flexible for-loops are in C. Write two examples of "non-traditional" for-loops (ie, for-loops that don't look like for-loops you learned in introductory courses) and explain how they work.

12. Compare/contrast case/when in Ruby with a switch statement in another language of your choice.

13. What does the last keyword do in a loop in Perl?

14. Many contemporary PLs allow two kinds of comments: one in which delimiters are used on both ends (multiple-line comments) and one in which a delimiter marks only the beginning of the comment (one-line comment). Choose two of the criteria we have been using, and discuss the advantages and disadvantages of each type of comment with respect to these criteria.

15. Some PLs use curly braces to denote the beginning and ending of blocks, and other PLs use a different mechanisms. Choose two PLs which use different mechanisms for denoting blocks, and choose two of the criteria we have been using, and discuss the advantages and disadvantages of each mechanism.

Solutions

Expert Solution

Q.11

#include <stdio.h>

int main(void) {
   // your code goes here
   int i = 0;
   for(;;)
   {
   if(i>10)
   break;
   printf("%d ",i);
   i++;
   }
   printf("\n");
   for(int i = 0;i<10;)
   {
   printf("%d ",i);
   i++;
   }
   printf("\n");
   for(int i = 0;;)
   {
   if(i > 10)
   break;
   printf("%d ",i);
   i++;
   }
   printf("\n");
  
   return 0;
}

Illustruted above are the example of non traditional for-loops.

The normal flow of for loops is:

1) Initialize

2) Check if condition is true

i) If condition is true then execute the block

ii) If condition is false, then terminate the loop

3) If the condition was true of step 2 then increment the loop

Now lets have a look at the for loops, it is also doing the same thing, but the thing is it is performing the most of the parts inside the body like while loop:

1)

int i = 0

  for(;;)
   {
   if(i>10)
   break;
   printf("%d ",i);
   i++;
   }

i intialized outside the loop

entred to for loop

1)Do not found the intialization part, entered the loop body

2) i > 10 is the check inside body, saying if i > 10 then break the loop then we have loop normal body and at last i++ increases the loop by 1,

This does in the similar fashion as the normal for loop but un traditionally.

Similarly, all other cases can be justified

Q.12

Lets see the structure of switch case in Ruby and other programming language.

Ruby

case x

when 1..5

"The number is beteen 1 to 5"

when 6

"The number is 6"

when "cat","dog"

"It is either cat or dog"

when String

"String passed"

else

"Option not specified specifially"

end

Other Programming Language

switch(x){

case 1: printf("The number passed is 1");

break;

case 2: printf("The number passed is 2");

break;

default: print("The default case is passed");

break;

}

Diffferences

1) Ruby switch case compares the object in "when" clause with the object in the case clause using === operator. e.g 1..5 === x and not x === 1..5

The advantage over the normal for loop being Ranges, Classes, and all sorts of things can be easily tested unlike the other Programming lanhuage where the equality need to be provided for all cases.

2) The other major advantage observed is Ruby switch case do not have fall through (fall through means if you don not specify the break statment then all statement below the searched statment will print until it either found break or switch case ends). Thus overhead of writing break carefully in Ruby is savd.

3) If there are more than one discrete statment outputting same result, then we can specify multiple matches in a single case seperated by comma. e.g when cat,dog in the above example.

Q.13

In normal programmming what we do with "break" is what is the work of "last" in the perl i.e When a "last "statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop

e.g

$num = 10;
while( $num < 20 ) {
   if( $num == 15) {
      # break the loop.
      $num = $num + 1;
      last;
   }
   print "value of a: $num\n";
   $num = $num + 1;
}

Q.14

Advatages and disadvatnages of using single line comment and multi line comment under differnet criteria.

1) Single line comment

i) Single line comment is used to make the code readable by specifying the cooment above the line or by side of the line.

ii) Commenting the non usable codes.

Advantages:

1) Plain engish so easier to understand logic going

2) Some times comment more precise then code, in case of debugging.

3) Comments ease comprehension by collecting information into one place.

Disadvantages:

1) Take considerable time to write good comment

2) Less reliable then the code.

3) The encourage bad code, some time developer specifying comment writes bad code specifying comment, thinking that with comment it can later be rectified.

Multi line comment

Purpose of the multi line comment is also the same. But is written where broader expainatio of the code is required or it is required to specifty the TODO list in explained manner unlike the single line comment where comment below the flow statement is written.

Advantage:

1) Easy english easy to understand

2) Comments are persistant, even though the code had been modified for the performance or bug issue, but it's purpose is clearly specified bt the comment.

Disadvantage

1) Less precise then code

2) Commenting the code can sometime be disasetrous for future reference.. Actuallty it is never recommended to comment the running code.

Q.15

Two programiing language not using curly braces are : Python,Haskell

Without Curly Bracket

Advntages of using these languages:

1) These language uses identation to replace the curly brackets. So the code written are very much readbale and understandable.

2) Overhead of looking for curly bracket goes. If your code is properly idented you are ready to go.

Disadvantage

1) Identing the code is not an easy job for an individual form C,C++,Java. So one wrong Identation and the whole code is messed up.

2) Standard tab or spacing need to be followed strictly i.e if one is used for identation then throughout the whole program only one tab should be used.

Lanuages with Brackets

Advantages

1) No need of indenting the code.

2) Curly braces easily help identify each block unless the code is very much messed up.

Disadvantage

1) The code do not strictly require identation, So if the code is not idented then some time becomes hard to look.

2) The single missing bracket missing can some time cost hours of debugging.

  


Related Solutions

Pick a period in Western history OR a non-Western traditional society and write about the role...
Pick a period in Western history OR a non-Western traditional society and write about the role of women in that period or society. You could, for example, examine the role of women in ancient Greek society, Roman society, medieval society, and/or in American society in antebellum South or in the industrial North before the Civil War. Or you could examine the role of women in traditional Arabic tribal society, African society, Latin American tribal society, Inuit culture, and so forth....
Write a technical brief about non-traditional production processes of Hybrid Electrochemical processes and Express it very...
Write a technical brief about non-traditional production processes of Hybrid Electrochemical processes and Express it very technical aspects
How do write a proposed funding for non-profit. Are there any examples?
How do write a proposed funding for non-profit. Are there any examples?
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the...
C++. Write a program that uses for loops to perform the following steps: a. Prompt the user to input two positive integers. variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use for loop). b. Output all odd numbers between firstNum and secondNum. (use for loop). c. Output the sum of all even numbers between firstNum and secondNum. (use for loop). d. Output the...
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance....
Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance. Upon execution the program will request ether 1 Series or 2 Parallel then the number of resisters. The program will then request each resistor value. Upon entering the last resistor the program will print out the equivalent resistance. The program will ask if another run is requested otherwise it will exit.
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT