END TERM EXAMINATION

SECOND SEMESTER [B.TECH.] MAY-JUNE-2014

Model Paper-II

Subject Facilitator: Shipra Varshney

Paper code: ETCS108Subject: Introduction to Programming

Time: 3 HrsMaximum Marks: 75

Note: Attempt five questions including Q.no.1 which is compulsory.

Select one question from each unit.

Q1: Short answer type: [5*5=25]

a)What is the difference between structure and unions in C?

Ans:

Structure / Union
i. Access Members
We can access all the members of structure at anytime. / Only one member of union can be accessed at anytime.
ii. Memory Allocation
Memory is allocated for all variables. / Allocates memory for variable which variable require more memory.
iii. Initialization
All members of structure can be initialized / Only the first member of a union can be initialized.
iv. Keyword
'struct' keyword is used to declare structure. / 'union' keyword is used to declare union.
v. Syntax
struct struct_name
{
structure element 1;
structure element 2;
------
------
structure element n;
}struct_var_nm; / union union_name
{
union element 1;
union element 2;
------
------
union element n;
}union_var_nm;
vi. Example
struct item_mst
{
int rno;
char nm[50];
}it; / union item_mst
{
int rno;
char nm[50];
}it;

b)What is an operator? List down different types of operators in C.

Ans: An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides the following types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Arithmetic Operators

Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then:

Operator / Description / Example
+ / Adds two operands / A + B will give 30
- / Subtracts second operand from the first / A - B will give -10
* / Multiplies both operands / A * B will give 200
/ / Divides numerator by de-numerator / B / A will give 2
% / Modulus Operator and remainder of after an integer division / B % A will give 0
++ / Increments operator increases integer value by one / A++ will give 11
-- / Decrements operator decreases integer value by one / A-- will give 9

Relational Operators

Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then:

Operator / Description / Example
== / Checks if the values of two operands are equal or not, if yes then condition becomes true. / (A == B) is not true.
!= / Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. / (A != B) is true.
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. / (A > B) is not true.
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. / (A < B) is true.
>= / Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. / (A >= B) is not true.
<= / Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. / (A <= B) is true.

Logical Operators

Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:

Operator / Description / Example
Called Logical AND operator. If both the operands are non-zero, then condition becomes true. / (A & B) is false.
|| / Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. / (A || B) is true.
! / Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. / !(A & B) is true.

Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:

p / q / p & q / p | q / p ^ q
0 / 0 / 0 / 0 / 0
0 / 1 / 0 / 1 / 1
1 / 1 / 1 / 1 / 0
1 / 0 / 0 / 1 / 1

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:

Operator / Description / Example
Binary AND Operator copies a bit to the result if it exists in both operands. / (A & B) will give 12, which is 0000 1100
| / Binary OR Operator copies a bit if it exists in either operand. / (A | B) will give 61, which is 0011 1101
^ / Binary XOR Operator copies the bit if it is set in one operand but not both. / (A ^ B) will give 49, which is 0011 0001
~ / Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. / (~A ) will give -61, which is 1100 0011 in 2's complement form.
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. / A < 2 will give 240 which is 1111 0000
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. / A > 2 will give 15 which is 0000 1111

Assignment Operators

There are following assignment operators supported by C language:

Operator / Description / Example
= / Simple assignment operator, Assigns values from right side operands to left side operand / C = A + B will assign value of A + B into C
+= / Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand / C += A is equivalent to C = C + A
-= / Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand / C -= A is equivalent to C = C - A
*= / Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand / C *= A is equivalent to C = C * A
/= / Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand / C /= A is equivalent to C = C / A
%= / Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand / C %= A is equivalent to C = C % A
<= / Left shift AND assignment operator / C <= 2 is same as C = C < 2
>= / Right shift AND assignment operator / C >= 2 is same as C = C > 2
&= / Bitwise AND assignment operator / C &= 2 is same as C = C & 2
^= / bitwise exclusive OR and assignment operator / C ^= 2 is same as C = C ^ 2
|= / bitwise inclusive OR and assignment operator / C |= 2 is same as C = C | 2

Misc Operators ↦ sizeof & ternary

There are few other important operators including sizeof and ? : supported by C Language.

Operator / Description / Example
sizeof() / Returns the size of an variable. / sizeof(a), where a is integer, will return 4.
Returns the address of an variable. / &a; will give actual address of the variable.
* / Pointer to a variable. / *a; will pointer to a variable.
? : / Conditional Expression / If Condition is true ? Then value X : Otherwise value Y

c): Give the general format of preprocessor statement. Define. How it is useful?

Ans: TheC Preprocessoris not part of the compiler, but is a separate step in the compilation process. In simplistic terms, a C Preprocessor is just a text substitution tool and they instruct compiler to do required pre-processing before actual compilation. We'll refer to the C Preprocessor as the CPP.

All preprocessor commands begin with a pound symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in first column. Following section lists down all important preprocessor directives:

Directive / Description
#define / Substitutes a preprocessor macro
#include / Inserts a particular header from another file
#undef / Undefines a preprocessor macro
#ifdef / Returns true if this macro is defined
#ifndef / Returns true if this macro is not defined
#if / Tests if a compile time condition is true
#else / The alternative for #if
#elif / #else an #if in one statement
#endif / Ends preprocessor conditional
#error / Prints error message on stderr
#pragma / Issues special commands to the compiler, using a standardized method

d)What are the advantages of pointer in C?

Ans: Advantages

Pointers are more efficient in handling arrays and data tables. They can be used to return multiple values from a function via function arguments. Pointers permit references to functions and thereby facilitating passing of functions as arguments to other functions. The use of po0inter arrays to character strings results in saving of data storage space in memory. pointers allow C to support dynamic memory management. Pointers provide an efficient tool for manipulating dynamic data structures such as structures, linked lists, queues, stacks and trees. Pointers reduce length and complexity of programs. They increase the execution speed and thus reduce the program execution time.

e)What are the different types of errors in running a C Program? Explain.

Ans:-Basically there are three types of errors in c programming:

  1. Runtime Errors
  2. Compile Errors
  3. Logical Errors

C Runtime Errors

C runtime errors are those errors that occur during the execution of a c program and generally occur due to some illegal operation performed in the program.

Examples of some illegal operations that may produce runtime errors are:

  • Dividing a number by zero
  • Trying to open a file which is not created
  • Lack of free memory space

It should be noted that occurrence of these errors may stop program execution, thus to encounter this, a program should be written such that it is able to handle such unexpected errors and rather than terminating unexpectedly, it should be able to continue operating. This ability of the program is known asrobustnessand the code used to make a program robust is known asguard codeas it guards program from terminating abruptly due to occurrence of execution errors.

Compile Errors

Compile errors are those errors that occur at the time of compilation of the program. C compile errors may be further classified as:

Syntax Errors

When the rules of the c programming language are not followed, the compiler will show syntax errors.

For example, consider the statement,

1 / inta,b:

The above statement will produce syntax error as the statement is terminated with : rather than ;

Semantic Errors

Semantic errors are reported by the compiler when the statements written in the c program are not meaningful to the compiler.

For example, consider the statement,

1 / b+c=a;

In the above statement we are trying to assign value of a in the value obtained by summation of b and c which has no meaning in c. The correct statement will be

1 / a=b+c;

Logical Errors

Logical errors are the errors in the output of the program. The presence of logical errors leads to undesired or incorrect output and are caused due to error in the logic applied in the program to produce the desired output.

Also, logical errors could not be detected by the compiler, and thus, programmers has to check the entire coding of a c program line by line.

Unit-I

Q2 a): Draw a flowchart or algorithm that input three numbers and print output the greatest amongst them. (6)

Ans: Flowchart of Greatest of three numbers:

Algorithm: of Greatest of three numbers

Step 1: Start

Step 2: Declare variables a,b and c.

Step 3: Read variables a,b and c.

Step 4: If a>b

If a>c

Display a is the largest number.

Else

Display c is the largest number.

Else

If b>c

Display b is the largest number.

Else

Display c is the greatest number.

Step 5: Stop

b): What are the characteristics of an algorithm?(4)

Ans: - The characteristics of a good algorithm are:

  • Precision – the steps are precisely stated(defined).
  • Uniqueness – results of each step are uniquely definedand only depend on the input and the result of the precedingsteps.
  • Finiteness – the algorithm stops after a finite number ofinstructions are executed.
  • Input – the algorithm receives input.
  • Output – the algorithm produces output.
  • Generality – the algorithm applies to a set ofinputs.

c)What are the advantages of drawing flow chart?(2.5)

Ans: -

a)Easy to analyze the program
b)Especially for c language where structure is important it helps a lot.
c)Focus in logic.
d) Without a good flowchart you cannot write software.
e) Conditional statements are easy to analyze.
f) Occupies less paper compared to a large programs having huge statements.

Q3 a) Differentiate between short integer, integer and long integer type of variable. (6)

Ans:

=> Integers , long and short:

  • The range of an integer constant depends upon the compiler.
  • For a 16 bit Compiler like Turbo C or Turbo C++ the range is -32768 to 32767.
  • For a 32 bit compiler like Visual Studio or gcc the range would be -2147483648 to 2147483647.
  • C offers a variation of the Integer data type i.e short and long integer values.
  • The intention of providing these variations is to provide Integers with different ranges whenever possible.

Each compiler can decide appropriate sizes of int , long and short depending on the operating system and hardware, for which it is being written, subject to following Rules:

  • shorts are atleast 2 bytes big.
  • longs are atleast 4 bytes big.
  • shorts are never bigger than ints.
  • ints are never bigger than longs.

Compiler / short / int / long
16-bit (Turbo C/C++) / 2 / 2 / 4
32-bit (Visual Studio ) / 2 / 4 / 4

b) List some vi editing command and corresponding action. (6.5)

Ans: Inserting or Adding Text

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

* / i / insert text before cursor, until <Esc> hit
I / insert text at beginning of current line, until <Esc> hit
* / a / append text after cursor, until <Esc> hit
A / append text to end of current line, until <Esc> hit
* / o / open and put text in a new line below current line, until <Esc> hit
* / O / open and put text in a new line above current line, until <Esc> hit
Changing Text

The following commands allow you to modify text.

* / r / replace single character under cursor (no <Esc> needed)
R / replace characters, starting with current cursor position, until <Esc> hit
cw / change the current word with new text,
starting with the character under cursor, until <Esc> hit
cNw / change N words beginning with character under cursor, until <Esc> hit;
e.g., c5w changes 5 words
C / change (replace) the characters in the current line, until <Esc> hit
cc / change (replace) the entire current line, stopping when <Esc> is hit
NccorcNc / change (replace) the next N lines, starting with the current line,
stopping when <Esc> is hit
Deleting Text

The following commands allow you to delete text.

* / x / delete single character under cursor
Nx / delete N characters, starting with character under cursor
dw / delete the single word beginning with character under cursor
dNw / delete N words beginning with character under cursor;
e.g., d5w deletes 5 words
D / delete the remainder of the line, starting with current cursor position
* / dd / delete entire current line
NddordNd / delete N lines, beginning with the current line;
Cutting and Pasting Text

The following commands allow you to copy and paste text.

yy / copy (yank, cut) the current line into the buffer
NyyoryNy / copy (yank, cut) the next N lines, including the current line, into the buffer
p / put (paste) the line(s) in the buffer into the text after the current line

UNIT II

Q4: a) Illustrate the order of precedence of operators in c?(6.5)

Ans:

b) : Explain in brief all bitwise operators in C? (5.5)

Ans: - The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:

Operator / Description / Example
Binary AND Operator copies a bit to the result if it exists in both operands. / (A & B) will give 12 which is 0000 1100
| / Binary OR Operator copies a bit if it exists in either operand. / (A | B) will give 61 which is 0011 1101
^ / Binary XOR Operator copies the bit if it is set in one operand but not both. / (A ^ B) will give 49 which is 0011 0001
~ / Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. / (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. / A < 2 will give 240 which is 1111 0000
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. / A > 2 will give 15 which is 0000 1111

Example

Try the following example to understand all the bitwise operators available in C programming language:

#include <stdio.h>

main()

{

unsigned int a = 60;/* 60 = 0011 1100 */

unsigned int b = 13;/* 13 = 0000 1101 */

int c = 0;

c = a & b; /* 12 = 0000 1100 */

printf("Line 1 - Value of c is %d\n", c );

c = a | b; /* 61 = 0011 1101 */

printf("Line 2 - Value of c is %d\n", c );

c = a ^ b; /* 49 = 0011 0001 */

printf("Line 3 - Value of c is %d\n", c );

c = ~a; /*-61 = 1100 0011 */

printf("Line 4 - Value of c is %d\n", c );

c = a < 2; /* 240 = 1111 0000 */

printf("Line 5 - Value of c is %d\n", c );

c = a > 2; /* 15 = 0000 1111 */

printf("Line 6 - Value of c is %d\n", c );

}

When you compile and execute the above program it produces the following result:

Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is -61

Line 5 - Value of c is 240

Line 6 - Value of c is 15

Q5: What do you understand by looping? Explain various loops in C with examples. (12.5)

Ans:-There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:

C programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.

Loop Type / Description
while loop / Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop / Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
do...while loop / Like a while statement, except that it tests the condition at the end of the loop body
nested loops / You can use one or more loop inside any another while, for or do..while loop.

Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

C supports the following control statements. Click the following links to check their detail.

Control Statement / Description
break statement / Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
continue statement / Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
goto statement / Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.

The Infinite Loop: