0% found this document useful (0 votes)
5 views15 pages

Model QP 4

The document explains various operators in C, categorizing them into unary, binary, and ternary operators, along with detailed descriptions and examples for each type. It covers arithmetic, relational, logical, assignment, and bitwise operators, as well as the ternary operator. Additionally, it mentions looping structures, string operations, matrix multiplication, insertion sort, and bubble sort algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Model QP 4

The document explains various operators in C, categorizing them into unary, binary, and ternary operators, along with detailed descriptions and examples for each type. It covers arithmetic, relational, logical, assignment, and bitwise operators, as well as the ternary operator. Additionally, it mentions looping structures, string operations, matrix multiplication, insertion sort, and bubble sort algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MODELPAPER4 > Section 4

13) Explain various operators available in C.


An operator indicates an operation to be performed on data that yields a value.
The data items on which operators act are called operands.
Depending on the number of operands that an operator can act upon, operators can be classified as
follows:
 Unary Operators: Those operators that require only a single operand to act upon are known as
unary operators.
Assume variable A holds 10 and variable B holds 20 then:

Operator Description Example

++ Increment operator increases the integer value by A++ = 11


one.
-- Decrement operator decreases the integer value by A-- = 9
one.
- The unary minus operator ( – ) changes the sign of its int a = 10;
argument. A positive number becomes negative, and a int b = -a;
negative number becomes positive.
& The addressof operator ( & ) gives an address of a int a;
variable. It is used to return the memory address of a int *ptr;
variable. These addresses returned by the address-of ptr = &a;
operator are known as pointers because they “point”
to the variable in memory.
sizeof() This operator returns the size of its operand, in bytes. sizeof(int)
The sizeof() operator always precedes its operand. The
operand is an expression, or it may be a cast. returns 4bytes

 Binary Operators: Those operators that require two operands to act upon are called binary
operators. Binary operators can further are classified into:
i. Arithmetic Operators in C
Operato
r Name of the Operator Arithmetic Operation Syntax

+ Addition Add two operands. x+y

Subtract the second operand from the first


– Subtraction x–y
operand.

* Multiplication Multiply two operands. x*y

/ Division Divide the first operand by the second operand. x/y

Calculate the remainder when the first operand


% Modulus x%y
is divided by the second operand.
ii. Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then

Operator Description Example


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

iii. Logical Operators


The Logical Operators are used to combine two or more relational expressions. A logical
relationship between the two expressions are checked with logical operators. After checking the
condition it gives logical true(1) or false(0).
iv. Assignment Operators
These operators are combination of an assignment operator and other arithmetic operators like +,-, *,/,
%.

Operator Description Example


= Simple assignment operator. Assigns values from right C = A + B will assign the
side operands to left side operand value of A + B to C
+= Add AND assignment operator. It adds the right C += A is equivalent to
operand to the left operand and assigns the result to C=C+A
the left operand.
-= Subtract AND assignment operator. It subtracts the C -= A is equivalent to
right operand from the left operand and assigns the C=C-A
result to the left operand.
*= Multiply AND assignment operator. It multiplies the C *= A is equivalent to C = C
right operand with the left operand and assigns the *A
result to the left operand.
/= Divide AND assignment operator. It divides the left C /= A is equivalent to C =
operand with the right operand and assigns the result C/A
to the left operand.
%= Modulus AND assignment operator. It takes modulus C %= A is equivalent to C = C
using two operands and assigns the result to the left %A
operand.

v. Bitwise Operators
These operators allow us to perform various operation on the individual bits of a memory location.
Bitwise operators allow you to perform AND, OR and to shift left and shift right. The bitwise operators
are:
a) The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two
numbers. The result of AND is 1 only if both bits are 1.
b) The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of two
numbers. The result of OR is 1 if any of the two bits is 1.
c) The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit of two
numbers. The result of XOR is 1 if the two bits are different.
d) The << (left shift) in C takes two numbers, the left shifts the bits of the first operand, and the
second operand decides the number of places to shift.
e) The >> (right shift) in C takes two numbers, right shifts the bits of the first operand, and the
second operand decides the number of places to shift.
f) The ~ (bitwise NOT) in C takes one number and inverts all bits of it.

 Ternary Operator: The operator that requires three operands to act upon is called the ternary operator.
Conditional Operator (? :) is also called the ternary operator
Syntax: (condition) ? expression1: expression2

Example: largest = (num1>num2)? num1 : num2;

Here the value after the "?" is assigned to the variable "largest" if the condition (num1 > num2) is true
otherwise the value after the ":" is assigned to the variable.
14) Various forms of looping structures avlb in C.
15) explain various operations performed on strings with example for each
16)C program to find the multiplication of two matrices.
17)Explain insertion sort with example.
The Insertion sort inserts each element in appropriate position. If an array A contains n elements and we
place each element of an array at proper place in the previously sorted element list.
18) Write the Bubble sort algorithm to sort the given set of elements. Trace the algorithm for the
following elements 28,20,1,30,8,15,05.

Algorithm : bubbleSort(A[0………n-1])
Step 1 : Start
Step 2 : Read n, array A of n elements to be sorted
Step 3 : for i = (pass - 1) to n-1
for j = 0 to n-pass-1
if(A[j] > A[j+1]) then
temp = A[j]
A[j] =A[j+1]
A[j+1] = temp
End for
End for
Step 4: Print the array A of n elements after sorting
Step 5: Stop

Trace the algorithm for the following elements 28,20,1,30,8,15,05.

You might also like