Friday, October 3, 2014

3.1a DECISION MAKING & BRANCHING

In practice, we have a number of situations where we may have to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met. This involves a kind of decision making.

C language possesses such decision-making capabilities by supporting the following statements:
Ø  if Statement
Ø  switch Statement
Ø  Conditional Operator Statement
Ø  goto Statement

 
IF STATEMENT
§  General form:
            if (this condition is true)
                    execute this statement;

§  Syntax:
            if (test-expression)
            {
                        statement-block;
            }
            statement-x;

§  The keyword ‘if’ tells the compiler that what follows is a decision control statement.
§  The condition following the keyword ‘if’ is always enclosed within a pair of parenthesis.
§  If the condition is true, then the statement is executed. If the statement is not true, then the statement is not executed.
§  To express a condition we use relational operators.
<          <=        >          >=        = =       !=
§  Flowchart:
 
IF-ELSE STATEMENT
§  General form:
            if (this condition is true)
                    execute this statement;
            else
                    execute this statement;   

§  Syntax:
            if (test-expression)
            {
                        true-block statement;
            }
            else
            {
                        false-block statement;
            }
            statement-x;

§  If the condition is true, then the statement(s) immediately following the ‘if’ statement are executed, otherwise the false-block statements are executed.
§  Flowchart:

NESTED IF-ELSE STATEMENT
§  When a series of decisions are involved, we may have to use more than one if…else statement in nested form.
§  General form:
            if (condition-1)
            {
                        if (condition-2)
                        {
                                    stmt-1;
                        }
                        else
                        {
                                    stmt-2;
                        }
            }
            else
            {
                        stmt-3;
            }
            stmt-x;

§  If condition-1 is false, stmt-3 will be executed, otherwise it continues to perform the second test.
§  If condition-2 is true, stmt-1 will be executed otherwise stmt-2 will be evaluated and then the control is transferred to stmt-x.
§  Flowchart:

ELSE-IF LADDER
§  A multipath decision is a chain of if’s in which the statement associated with each else is an if.
§  General form:
            if (condition-1)
                        stmt-1;
                else if (condition-2)
                        stmt-2;
                    else if (condition-3)
                           stmt-3;
                        else if (condition-4)
                                stmt-4;
                                    else
                                        default-stmt;
            stmt-x;       

§  This construct is known as the else-if ladder. The conditions are evaluated from the top, downwards.
§  As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the stmt-x (skipping the rest of the ladder).
§  When all the n-conditions become false, then the final else containing the default-stmt will be executed.
§  Flowchart:
 
RULES FOR INDENTATION
When using control structures, a statement often controls many other statements that follow it. In such situations it is good practice to use indentation to show that the indented statements are dependent on the preceding controlling statement. Some guidelines that could be followed while using indentation are listed below:
v  Indent statements that are dependent on the previous statements; provide at least three spaces of indentation.
v  Align vertically else clause with their matching if clause.
v  Use braces on separate lines to identify a block of statements.
v  Indent the statements in the block by at least three spaces to the right of the braces.
v  Align the opening and closing braces.
v  Use appropriate comments to signify the beginning and end of blocks.
v  Indent the nested statements as per the above rules.
v  Code only one clause or statements on each line.
 
SWITCH STATEMENT
§  When we have to select one of the many alternatives, we can use an if-statement to control the selection. However, the complexity of such a program increases as the number of alternatives increase.
§  C has a built-in multiway decision statement known as switch. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed.
§  General form:
switch (expression)
{
            case value-1:
                        block-1;
                        break;
            case value-2:
                        block-2;
                        break;
            …..
            …..
            default:
                        default-block;
                        break;
}
next-stmt;
                      
            where:       
                                i.            expression is an integer expression or character.
                              ii.            value-1, value-2, … are constants or constant expressions and are known as case labels.
      NOTE: Float values are not accepted.
      NOTE: Each of these values should be unique within a switch statement.
      NOTE: Case labels ends with a colon(:)
                            iii.            block-1, block-2,…. Are statement lists and may contain zero or more statements.
                            iv.            The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the next-stmt following the switch.
§  When the switch is executed, the value of the expression is successfully compared against the values value-1, value-2,… If a case is found whose value matches with the value of the expression, then the block of statements that follow are executed.
§  The default is an optional case. (It can be placed anywhere but usually is placed at the end).
§  It is permitted to nest switch statement.
§  Flowchart:


Switch versus if-else ladder
Switch works faster than an equivalent if-else ladder.
            This is because, the comp iler generates a jump table for a switch during compilation. As a result, during the execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied.
            If-else’s are slower because the conditions in them are evaluated at execution time.

NOTE: In case of questions having range, if-else is a better option than switch statement.

GOTO STATEMENT
§  The C language supports the goto statement to branch unconditionally from one point to another in the program.
§  The goto statement requires a label in order to identify the place where the branch is to be made.
A label is any valid variable name, and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred.

§  General form:
 
§  The label : can be anywhere in the program either before or after the goto label statement.
§  During the running of the program when a statement like:
goto begin;

is met, the flow of control will jump to the statement immediately following the label begin:. This happens unconditionally.

NOTE that a goto breaks the normal sequential execution of the program.

           If the label is before the statement goto label; a loop will be formed and some statements will            be executed repeatedly. Such a jump is known as a backward jump.
            On the other hand, if the label: is placed after the goto label; some statements will be skipped and the jump is known as a forward jump.

Thursday, October 2, 2014

2013-14 (EVEN SEM)

B.Tech.

(SEM. II) THEORY EXAMINATION 2013-14

COMPUTER PROGRAMMING

SECTION-A
 

1.      Attempt all parts of this section.                                                        (2 X 10 = 20)
a)      Why ‘C’ is called structured programming language?
b)      Explain the output of following:
#define SQUARE (x) x * x
void main ( ) {
int i;
i = 81/SQUARE (9);
printf(“%d”, i); }

c)      What is ASCII Code? Write ASCII Code in 8 bit for word “Hi”.
d)     Differentiate between conditional operator and switch.
e)      What is void pointer? How is it different from other pointers?
f)       What is function prototype? Why is it required?
g)      Write any four features of LINUX operating system.
h)      Find the output of the following code:
void main ( ) {
switch (0) {
case 0: printf(“0+3”);
case 0+1: printf(“0+5”);
default: printf(“Wrong Input”); } }         

i)        Define linker and explain its role in ‘C’ programming.
j)        Find the value of Y (assume Y is integer data type)
Y= 4 * 2 / 4 – 6 / 2 + 3 % 2 * 6 / 2 + 2 > 2 && 4 !=2

SECTION-B

2.      Attempt any three parts of this section. Each part carries 10 marks. (10 X 3 = 30)
a)      Draw a neat diagram of digital computer and explain the role of each functional unit.
b)      Convert the following:
                                                        i.            (B5C)16 – (92A)16 = ( ? )10
                                                      ii.            (10101011101.011)2 = ( ? )16
                                                    iii.            (916.125)10 = ( ? )4
                                                    iv.            (16)8 + (45)8 = ( ? )16
                                                      v.            (123)5 = ( ? )3

c)      Differentiate between pseudo code and algorithm. Write the characteristics of an algorithm. Draw a flow chart for printing Fibonacci series upto a term given by user.
d)     What is storage class in ‘C’? Explain different storage classes supported in C with suitable example.
e)      Write a program in ‘C’ to create a database of fifty students to store personal details such as Roll No., Name and Marks. Print all the details of student whose name is entered by user.

SECTION-C

3.      Attempt any two parts:                                                                       (5 X 2 = 10)
a)      Define number system. Why binary number system is used in computer?
b)      What is operating system? Explain main four functions of operating system.
c)      Define the following terms and give one example of each:
                                                              i.      Application Software
                                                            ii.      Symbolic Language
                                                          iii.      Data Error
                                                          iv.      Logical Error

4.      Attempt any two parts:                                                                       (5 X 2 = 10)
a)      Explain the different types of type conversion in ‘C’ with suitable example.
b)      Write a program in ‘C’ using conditional operator to find the largest among five numbers given by the user at run time.
c)      What is bitwise operator? Explain all bitwise operators in ‘C’ with suitable example.

5.      Attempt any two parts:                                                                       (5 X 2 = 10)
a)      Write a program in ‘C’ to print Pascal Triangle upto the rows given by user.
b)      Differentiate between break and continue with suitable example.
c)      Write a program in ‘C’ to convert a number in decimal system which is entered by the user to a number in hexadecimal system.

6.      Attempt any two parts:                                                                       (5 X 2 = 10)
a)      What is alternative to recursion? Write a program in ‘C’ to calculate the area and perimeter of a circle for a given radius using user defined single function which returns area and perimeter to the main function.
b)      Write a program in ‘C’ to read data from keyboard, write it to a file named PASCO, again read the same data from the file PASCO and display number of characters, number of vowels and number of lines contained in it on the screen.
c)      Write a program in ‘C’ to create a list of ten elements using dynamic memory allocation and display the sorted list.

7.      Attempt any two parts:                                                                       (5 X 2 = 10)
a)      Why C preprocessor is called preprocessor? Differentiate between statements
#include <facto.h> and
#include “facto.h”
Explain nested macros with suitable example.
b)      Write a program in ‘C’ to determine whether a string given by the user is palindrome or not without using strrev() function.
c)      Explain the architecture of LINUX operating system. Write any two file related and two directory related commands in LINUX with syntax.