Model 4
SectionA
Here are the answers to all Section A questions, suitable for 2 marks each:
1, What is an Algorithm?
An algorithm isa set of step-by-step instructions used to solve a specific problem or perform a
computation, typically ina finite number of steps
2. What is a variable? Give an example.
A variable is a symbolic name given to a memory location that stores data values; its value can be changed
during program execution
Example: In C language, int age = 25; declares an integer variable called age and assigns it the value 25
3. What are escape sequences?
Escape sequences are special combinations of characters starting with a backslash (\), representing
characters that cannot be typed directly, such as newline (\n), tab (\t), or double quote (\")
4. Find the prime factor of 72.
‘The prime factorization of 72 is 2 x 2 x 2x 3 x 3 or 2? x 3?
5. What is sorting? Mention different sorting methods.
Sorting refers to arranging the elements of a list or array in a specific order (ascending or descending).
Common sorting methods include Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort, Heap
Sort, Counting Sort, and Radix Sort.
6. What is an array? How is it initialized?
Anarray is a collection of elements of the same data type stored in contiguous memory locationsli,
It can be initialized at the time of declaration, for example: int arr[‘2_3] = {2, 4, 64:
section Bn of set of numbers.
7. Write an algorithm for summa
Algorithm Steps:
1. Start
2. Initialize Sum = 0
3. Read total number of elements, say n
4. Repeat steps 5 to 6 fori= 1 ton:
5. Read element, say num
6, Add num to Sum: Sum = Sum + num.
7. After loop ends, print Sum
8. Stop
This isa simple algorithm that can sum any given set of numbers by iterating through each number
and accumulating the total
8. Explain asymptotic notations,
Asymptotic notations are mathematical tools to represent the time and space complexity of algorithms as
the input size approaches infinity. The most common notations:
* Big 0 (0): Describes the worst-case upper bound, Example: O(n?) for Bubble Sort,
* Big Omega (0): Represents the best-case lower bound. Example: 2(n) for Linear Search.
* Big Theta (6): Specifies the tight bound or average case, Example: 6(n) for simple for-loops.
‘These notations help compare and analyze algorithm efficiencies and scalability independent of
machine or coding language
9. What is datatype? Explain different datatypes with examples.
‘A datatype determines the kind of value a variable can store and how the system will interpret that value,
Common datatypes include:
* Integer (int): Stores whole numbers. Example: int a = 25;
* Float (float): Stores decimal numbers. Example: float salary = 8750, 50;
Character (chaz): Stores single characters. Example: char grade
2String (str/char arzay): Stores collection of characters. Example: chax name{*3_22]
Boolean: Stores logical values, true or false, Example: bool isValid = tru
Other datatypes include double, long, short, date, and more, each applicable indifferent scenarios
depending on size and precision needs.
10. Write a program to find the factorial of a number.
#include
int main() {
int n, fact = 1, i;
printf("Enter the number: *);
scan£("%d", &n);
for (i= 4; i
a; ise)
fact = fact + i;
i
print£("Factorial of Rd = Rd\n", n, fact);
return 0;
‘This program asks for a number, uses a loop to multiply integers from 1 to n, and displays the factorial
result.
11. Mention any 5 string Ii
Common C string library functions:
rary functions.
strlen(): Returns length of the string,
;0 another.
strepy(): Copies one strin
strcat (): Concatenates (joins) two strings.
stromp(): Compares two strings.
strchr(): Finds first occurrence of a character in a string.
Other useful functions are stzncpy(), strncat(), strnemp(), and strrchr()412. Write an algorithm to perform binary search on the given set of elements.
Algorithm Steps:
1. Start
2. Initialize variables 1ow = @,high = n-1
3. Repeat until 1ow> high:
© Calculate mid = (Low + high) / 2
© Ifarr{mid] equals target, print position and stop
© Marr{mid] target, set high
4. Ifnot found, print "Element not found”
5. Stop
This algorithm efficiently searches for an element in a sorted array by repeatedly dividing the
search interval in half
section C
13(a). Explain loop control structures in C with general syntax.
Loop control structures in C are used to execute a block of code repeatedly until a certain condition is met.
‘The three main types are:
‘© For Loop: Executes a block for a known number of iterations.
Syntax:
for(initialization; condition; increnent/decrement) {
// Statements
+ While Loop: Executes as long as the condition is true.
Syntax:
while(condition) {
JJ Statenents* Po-While Loop: Similar to while, but executes at least once before checking the condition,
Syntax:
do i
// Statements
¥ while (condition) ;
Loops provide control over program flow and are used extensively for iterations over arrays,
calculations,
and data processing,
13(b). What is the
ference between break and continue statements?
* Break Statement: Immediately terminates the current loop (for, while, do-while) or switch
statement and transfers control to the statement following the loop or switch,
Example:
for(int i=0;i
int main() {
int a, b, ged, i;
print£("Enter two numbers
scan£("®d%d", &a, &b);
for(i = 1; i <= a GG i <= b; ++i) f
if(a % i == 0 && b % i == 0)
ged = i;
}
print£("GCD of %d and %d is %d", a, b, ged);
return 0;
i
This program calculates GCD by checking common divisors up to the minimum of the two numbers/#8153),
16(b). Write an algorithm to compute a prime factors of an integer.
7Algorithm Steps’
1, Start
2. Read number n
3. Setp=2
4. Whilen>=p*p:
o Ifn%p
«= Printp
* n=n/p
© Else:
© p=pet
5. Printn (ifn > 2, itisa prime factor)
6. Stop
This algorithm repeatedly divides n by its smallest prime factor until n becomes 1, listing the factors
as it goes
17(a). Expl:
in the algorithm to find the maximum element in a set.
Algorithm Steps:
1. Start
2, Read n elements into array arr[]
3. Set max = arr
4. Fori=1ton-1:
© Mfarr(i] > max: set max = arr(i]
5. Print max
6. Stop
‘This finds the largest value by comparing each element in the arrays,eo
17(b). Sort the following array using insertion sort: 43,75, 21,
+ 43,75, 21, 37,12
Insertion Sort Steps:
1. Start with 2nd element (75), compare with previous and place correctly
2. Continue for each nes i
ha xt element, shifting larger elements to the right and inserting current element
Sorting Steps:
+ Initial:
© Insert 75:
© Insert 21:
© Insert 37:
© Insert 12:
Final sorted array:
18(a). Write an algorithm to sort the set of elements using selection sort.
Selection Sort Algorithm:
1. Start
2, Fori=0ton-2:
© Set mininde:
0 Forj=itlton-t:
+ Ifarr{j]