Question 1: (25 marks)
a. Draw a neat labelled block diagram of computer and explain the following.
CPU
Memory
I/O devices.
A central processing unit (CPU), also called a central processor or main processor, is the
electronic circuitry within a computer that executes instructions that make up a computer
program. The CPU performs basic arithmetic, logic, controlling, and input/output (I/O)
operations specified by the instructions.
Control Unit
This unit controls the operations of all parts of the computer but does not carry out any actual
data processing operations.
Functions of this unit are −
It is responsible for controlling the transfer of data and instructions among other units
of a computer.
It manages and coordinates all the units of the computer.
It obtains the instructions from the memory, interprets them, and directs the operation
of the computer.
It communicates with Input/Output devices for transfer of data or results from storage.
It does not process or store data.
ALU (Arithmetic Logic Unit)
This unit consists of two subsections namely,
Arithmetic Section
Logic Section
Arithmetic Section
Function of arithmetic section is to perform arithmetic operations like addition, subtraction,
multiplication, and division. All complex operations are done by making repetitive use of the
above operations.
Logic Section
Function of logic section is to perform logic operations such as comparing, selecting,
matching, and merging of data
MEMORY
Computer memory is any physical device capable of storing information temporarily, like
RAM (random access memory), or permanently, like ROM (read-only memory). Memory
devices utilize integrated circuits and are used by operating systems, software, and hardware.
Primary Memory (Main Memory)
Primary memory holds only those data and instructions on which the computer is currently
working. It has a limited capacity and data is lost when power is switched off. It is generally
made up of semiconductor device. These memories are not as fast as registers. The data and
instruction required to be processed resides in the main memory. It is divided into two
subcategories RAM and ROM.
Secondary Memory
This type of memory is also known as external memory or non-volatile. It is slower than the
main memory. These are used for storing data/information permanently. CPU directly does
not access these memories, instead they are accessed via input-output routines. The contents
of secondary memories are first transferred to the main memory, and then the CPU can access
it. For example, disk, CD-ROM, DVD, etc.
IO DEVICES
I/O devices are the pieces of hardware used by a human to communicate with a computer. For
instance, a keyboard or computer mouse is an input device for a computer, while monitors and
printers are output devices. Devices for communication between computers, such as modems
and network cards, typically perform both input and output operations.
The designation of a device as either input or output depends on perspective. Mouse and
keyboards take physical movements that the human user outputs and convert them into input
signals that a computer can understand; the output from these devices is the computer's input.
Similarly, printers and monitors take signals that a computer outputs as input, and they convert
these signals into a representation that human users can understand. From the human user's
perspective, the process of reading or seeing these representations is receiving output; this type
of interaction between computers and humans is studied in the field of human–computer
interaction.
b. Write an algorithm to convert celsius to Fahrenheit and vice-versa.
Algorithm to convert celsius to Fahrenheit
Step1. Start
Step2. Read the input of temperature in Celsius (say C)
Step3. F=(9*C)/5+32
Step4. Print temperature in fahrenheit is F
Step5. Stop
b) Algorithm to convert Fahrenheit to Celsius.
Step1. Start
Step2. Read the input of temperature in Fahrenheit (say F)
Step3. C=5/9*(F-32)
Step4. Print temperature in Celsius is C
Step5. Stop
c. Write a c program to covert Decimal to Binary.
PROGRAM:
#include <stdio.h>
Int main ()
int n, e, k;
printf ("Enter an integer in decimal number system\n");
scanf ("%d", &n);
printf ("%d in binary number system is:\n", n);
for (e = 31; e >= 0; e--)
k = n >> e;
if (k & 1)
printf ("1");
else
printf ("0");
printf ("\n");
return 0;
}
OUTPUT:
d. Explain floating point binary addition using an appropriate example.
Floating point is derived from the fact that there is no fixed number of digits before and after
the decimal point; that is, the decimal point can float. There are also representations in which
the number of digits before and after the decimal point is set, called fixed-point representations.
In general, floating-point representations are slower and less accurate than fixed-point
representations, but they can handle a larger range of numbers.
Given two 32-bit binary floating members:
X=0100 0010 0000 1111 0000 0000 0000 0000
Y=0100 0001 1010 0100 0000 0000 0000 0000
For X:
Sign= 0 (+ve)
Exponent = 1000 0100 = 132-127=5=1.0001111 x 25
For Y:
Sign = 0(+ve)
Exponent = 1000 0011 = 131-127=4=1.01001 x 24
1.01001 x24 = 0.101001 x 25
1.0001111 x 25
0.101001 x 25
1.1100001 x 25
5+127=132
X+Y = 0 1000 0100 1100001 00000000000000000
Positive until occupy all remaining hits
Question 2: (25 marks)
a. Discuss in detail any FIVE (5) bitwise operators with an appropriate example.
I. Bitwise AND & operator
Bitwise AND is a binary operator. It sets each bit of the resultant value as 1 if corresponding
bit in both operands is 1.
Suppose a and b are two integer variables with initial value
int a=10, b=11;
Let us re-write integers in 8-bit binary representation
a = 0000 1010
b = 0000 1011
c= a & b
The above expression a & b will evaluate to 0000 1010 which is 10 in decimal.
a 0000 1010
b 0000 1011
-----------------
a & b 0000 1010
Bitwise AND operator is used extensively to check whether a particular bit of data is on (1) or
off (0)
Example #1: Bitwise AND
1. #include <stdio.h>
2. int main()
3. { int a = 12, b = 25;
4. printf("Output = %d", a&b);
5. return 0; }
6. output=8
Bitwise OR | operator
Bitwise OR is a binary operator. It sets each bit of the resultant value as 1 if corresponding bit
of any of its two operand is 1.
Bitwise OR operator is commonly used to set flag bit values.
Suppose a and b are two integer variables with initial value as.
int a=2, b=13;
Let us re-write the integer in 8-bit binary representation.
a = 0000 0010
b = 0000 1101
c=a|b
The above expression a|b will evaluate to 0000 1111 which is 15 in decimal.
a 0000 0010
b 0000 1101
-----------------
a | b 0000 1111
Example #2: Bitwise OR
1. #include <stdio.h>
2. int main()
3. { int a = 12, b = 25;
4. printf("Output = %d", a|b);
5. return 0;}
6. Output = 29
Bitwise XOR ^ operator
Bitwise XOR operator is also binary operator. It sets each bit of the resultant value to 1
whenever the corresponding bits of the two operands differ.
Suppose a and b are two integer variables with initial value as.
int a=6, b=13;
Let us re-write the integers in 8-bit binary representation:
a = 0000 0110
b = 0000 1101
c=a^b
The above expression a^b will evaluate to 0000 1011 which is 11 in decimal.
a 0000 0110
b 0000 1101
-----------------
a^b 0000 1011
Example #3: Bitwise XOR
1. #include <stdio.h>
2. int main()
3. { int a = 12, b = 25;
4. printf("Output = %d", a^b);
5. return 0; }
Bitwise complement ~ operator
Bitwise complement is a unary operator. It sets each bit of the resultant value to 1 if
corresponding bit of the operand is 0 and vice versa. In other words, it flips all bit values.
Suppose a is an integer variable with initial value as.
int a=2;
In 8-bit binary representation:
a = 0000 0010
c = ~a
The above expression ~a will evaluate to 1111 1101 which is -3 (Twos complement) in
decimal.
a 0000 0010
-----------------
~a 1111 1101
Example #4: Bitwise complement
1. #include <stdio.h>
2. int main()
3. { printf("Output = %d\n",~35);
4. printf("Output = %d\n",~-12);
5. return 0; }
Bitwise left shift << operator
Bitwise left shift is a binary operator. It is used to shift bits to left n times. Consider the below
example:
int a=15;
Which in 8-bit binary will be represented as:
a = 0000 1111
c = a << 3;
The above expression a << 3; shifts bits of a three times to left and evaluates to 0111 1000
which is 120 in decimal.
a 0000 1111
a << 1 0001 1110
a << 2 0011 1100
a << 3 0111 1000
Shifting bits to left causes insertion of zero from right and shifting each bit to one position left
from its current position. The most significant bit (the left most bit) is dropped off on every left
shift.
Bitwise right shift >> operator
Bitwise right shift is binary operator used to shift bits to right. Consider the below example:
int a=15;
Which in 8-bit binary will be represented as:
a = 0000 1111
c = a >> 3
The above expression a >> 3 shifts bits of variable a three times right and will evaluate to 0000
0001 which is 1 in decimal
a 0000 1111
a >> 1 0000 0111
a >> 2 0000 0011
a >> 3 0000 0001
After each right shift, the least significant bit (left most bit) is dropped off. In bitwise right shift
1 is inserted from right if it is 1; otherwise 0 is inserted from right. However, in case of bitwise
left shift always 0 is inserted from left.
b) List and explain escape characters in C with examples.
In computing and telecommunication, an escape character is a character which invokes an
alternative interpretation on subsequent characters in a character sequence. An escape character
is a particular case of metacharacters. Generally, the judgment of whether something is an
escape character or not depends on context.
1 \t
Inserts a tab in the text at this point.
2 \b
Inserts a backspace in the text at this point.
3 \n
Inserts a newline in the text at this point.
4 \r
Inserts a carriage return in the text at this point.
5 \f
Inserts a form feed in the text at this point.
6 \’
Inserts a single quote character in the text at this point.
7 \”
Inserts a double quote character in the text at this point.
8 \\
Inserts a backslash character in the text at this point.
c) Write a program to display the following pattern.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Program:
#include <stdio.h>
void main()
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",j);
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
Output:
d)What is operator precedence. Explain in detail.
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated . In C, precedence of arithmetic operators( *, %, /, +, -) is higher than
relational operators(==, != , >, <, >=, <=) and precedence of relational operator is higher than
logical operators(&&, || and !). The table below shows all the operators in C with precedence
and associativity.
Operator Meaning of operator Associativity
() Functional call
[] Array element reference
-> Indirect member selection
. Direct member selection Left to right
! Logical negation
~ Bitwise(1 's) complement
+ Unary plus
- Unary minus
++ Increment
-- Decrement
& Dereference Operator(Address)
* Pointer reference
sizeof Returns the size of an object
(type) Type cast(conversion) Right to left
* Multiply
/ Divide
% Remainder Left to right
+ Binary plus(Addition)
- Binary minus(subtraction) Left to right
<< Left shift
>> Right shift Left to right
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal Left to right
Operator Meaning of operator Associativity
== Equal to
!= Not equal to Left to right
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Operator Right to left
Simple assignment
= Assign product
*= Assign quotient
/= Assign remainder
%= Assign sum
-= Assign difference
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift Right to left
, Separator of expressions Left to right
Question 3: (25 marks)
a. Write a C program to do the following.
Read an unsorted list and sort using any sorted technique.
Perform binary search on the sorted list.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j, n, val, *data, index = -1, temp;
printf("Enter the no of elements:");
scanf("%d", &n);
data = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) {
printf("data[%d]:", i);
scanf("%d", &data[i]);
}
printf("Enter the element to be searched:");
scanf("%d", &val);
printf("Unsorted Array:\n");
for (i = 0; i < n; i++) {
printf("%d ", data[i]);
if (data[i] == val)
index = i + 1;
}
printf("\n");
if (index == -1) {
printf("Given value is not present in the given Array\n");
exit(0);
} else {
printf("%d is at the postion %d\n", data[index - 1], index);
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (data[i] > data[j]) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
printf("\nSorted Array:\n");
for (i = 0; i < n; i++) {
printf("%d ", data[i]);
if (data[i] == val)
index = i + 1;
printf("\n%d is at the position %d in sorted array\n", data[index - 1], index);
return 0;
}
Output:
b) Implement a C program to pass array as a parameter to function.
(Take your own use case)
#include<stdio.h>
#include<conio.h>
//---------------------------------
void fun(int arr[])
int i;
for(i=0;i< 5;i++)
arr[i] = arr[i] + 10;
//--------------------------------
void main()
{
int arr[5],i;
printf("\nEnter the array elements : ");
for(i=0;i< 5;i++)
scanf("%d",&arr[i]);
printf("\nPassing entire array .....");
fun(arr); // Pass only name of array
for(i=0;i< 5;i++)
printf("\nAfter Function call a[%d] : %d",i,arr[i]);
getch();
OUTPUT
C).Demonstrate the towers_of_hanoi problem for Three (3) disks. And implement the
solution using recursion of N disks.
#include <stdio.h>
#include <stdlib.h>
int disc;
char s='A',a='B',d='C';
int hanoi(int,char,char,char);
int main()
printf("\n This is a simple program for the tower of Hanoi");
printf("\n Enter the no. of discs you wanna move from ---->Source:A to Destination:C\n");
scanf("%d",&disc);
hanoi(disc,s,a,d);
return 0;
int hanoi(int disc,char s,char a,char d)
if(disc==1)
printf("\n move disc %d from %c to %c", disc,s,d);
return;
hanoi(disc-1,s,d,a);
printf("\n Move Disc %d From %c to %c",disc,s,d);
hanoi(disc-1,a,s,d);
return 0;
Output:
Question 4: (25 marks)
a. Differentiate the terms static and dynamic. List any Three (3) dynamic memory
allocation functions. Write the syntax and usage of those functions.
Key Difference – Static vs Dynamic Memory Allocation
In programming, it is necessary to store computational data. These data are stored in memory.
The memory locations for storing data in computer programming is known as variables. The
variables have a specific data type. Therefore, the memory is allocated to run the programs.
Memory can be allocated in two ways. They are Static memory allocation and Dynamic
memory allocation. In static memory allocation, once the memory is allocated it cannot be
changed. The memory is not reusable. But in dynamic memory allocation, once the memory is
allocated it can be changed. The key difference between static and dynamic memory allocation
is that in static memory allocation once the memory is allocated, the memory size is fixed while
in dynamic memory allocation, once the memory is allocated, the memory size can be changed.
In programming, Static memory allocation and dynamic memory allocation are two
mechanisms for allocating memory. The difference between static and dynamic memory
allocation is that in static memory allocation once the memory is allocated, the memory size is
fixed while in dynamic memory allocation, once the memory is allocated, the memory size can
be changed. The programmer can decide whether the memory should be static or dynamic
depending on the application.
Static vs Dynamic Memory Allocation
Static memory allocation is a
Dynamic memory allocation is a method of
method of allocating memory,
allocating memory, and once the memory is
and once the memory is
allocated, it can be changed.
allocated, it is fixed.
Modification
In static memory allocation, it is
In dynamic memory allocation, the memory
not possible to resize after initial
can be minimized or maximize accordingly.
allocation.
Implementation
Static memory allocation is easy Dynamic memory allocation is complex to
to implement. implement.
Speed
In static memory, allocation
In dynamic memory, allocation execution is
execution is faster than dynamic
slower than static memory allocation.
memory allocation.
Memory Utilization
Dynamic memory allocation allows reusing
In static memory allocation,
the memory. The programmer can allocate
cannot reuse the unused
more memory when required . He can release
memory.
the memory when necessary.
C malloc()
The name "malloc" stands for memory allocation.
The malloc() function reserves a block of memory of the specified number of bytes. And, it
returns a pointer of void which can be casted into pointers of any form.
Syntax of malloc()
1. ptr = (castType*) malloc(size);
Example
1. ptr = (float*) malloc(100 * sizeof(float));
The above statement allocates 400 bytes of memory. It's because the size of float is 4 bytes.
And, the pointer ptr holds the address of the first byte in the allocated memory.
C calloc()
The name "calloc" stands for contiguous allocation.
The malloc() function allocates memory and leaves the memory uninitialized. Whereas, the
calloc() function allocates memory and initializes all bits to zero.
Syntax of calloc()
1. ptr = (castType*)calloc(n, size);
Example:
1. ptr = (float*) calloc(25, sizeof(float));
The above statement allocates contiguous space in memory for 25 elements of type float.
C realloc()
If the dynamically allocated memory is insufficient or more than required, you can change the
size of previously allocated memory using the realloc() function.
Syntax of realloc()
1. ptr = realloc(ptr, x);
Here, ptr is reallocated with a new size x.
Example 3: realloc()
#include <stdio.h>
#include <stdlib.h>
int main()
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u\n",ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
// rellocating the memory
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory: ");
for(i = 0; i < n2; ++i)
printf("%u\n", ptr + i);
free(ptr);
return 0;
b) Write a program to convert an infix expression to postfix.
Program:
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
} main()
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
while((x = pop()) != '(')
printf("%c", x);
}
else
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
e++;
while(top != -1)
printf("%c",pop());
Output:
Passing Pointer to a Function in C Programming
In this example, we are passing a pointer to a function. When we pass a pointer as an argument
instead of a variable then the address of the variable is passed instead of the value. So any
change made by the function using the pointer is permanently made at the address of passed
variable. This technique is known as call by reference in C.
Program:
#include <stdio.h>
void salaryhike(int *var, int b)
*var = *var+b;
int main()
int salary=0, bonus=0;
printf("Enter the employee current salary:");
scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d", salary);
return 0;
Output:
Passing Pointer to a Structure in C Programming
Pointer is a variable which points to the address of another variable of any data type like int,
char, float etc. Similarly, we can have a pointer to structures, where a pointer variable can point
to the address of a structure variable.
Program:
#include<stdio.h>
struct dog
char name[10];
char breed[10];
int age;
char color[10];
};
int main()
struct dog my_dog = {"tyke", "Bulldog", 5, "white"};
struct dog *ptr_dog;
ptr_dog = &my_dog;
printf("Dog's name: %s\n", ptr_dog->name);
printf("Dog's breed: %s\n", ptr_dog->breed);
printf("Dog's age: %d\n", ptr_dog->age);
printf("Dog's color: %s\n", ptr_dog->color);
// changing the name of dog from tyke to jack
strcpy(ptr_dog->name, "jack");
// increasing age of dog by 1 year
ptr_dog->age++;
printf("Dog's new name is: %s\n", ptr_dog->name);
printf("Dog's age is: %d\n", ptr_dog->age);
return 0;
Output: