0% found this document useful (0 votes)
2 views

UNIT V

Uploaded by

Kore Ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UNIT V

Uploaded by

Kore Ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT V

1. Define bitfield.

A bitfield stores multiple values in a single integer, each occupying specific bits.

2. Write about Omega notation.

Omega notation (Ω) describes an algorithm's lower bound (best-case time


complexity).

3. Write about enumerated data type.

An enum is a custom data type with named integer constants.

4. How assignment operation can be performed on enumerated data type.

Assigning an enum constant to a variable is done directly.

5. Define union.

A union stores different data types in the same memory location.

6. Define searching.

Searching finds a specific element in a collection.

7. Write about binary search technique.

Binary search divides a sorted array to find a target value.

8. How UNION can be passed to function.

A union can be passed by value or reference to a function.

9. Define selection sort.

Selection sort finds the smallest element and places it in the correct position.
UNIT V
10. List the components of Space Complexity.

Components include input space, auxiliary space, and recursion stack space.

LONG

11. Define structure. Explain how a structure is declared.

Answer:

A structure in C is a user-defined data type that allows grouping variables of


different data types under a single name.

Declaration of Structure

sstruct StructureName {

data_type member1;

data_type member2;

...

};

Example:

#include <stdio.h>

struct Student {

int roll_no;

char name[50];

float marks;

};
UNIT V

int main( ) {

struct Student s1 = {1, "John", 90.5};

printf("Roll No: %d\nName: %s\nMarks: %.2f\n", s1.roll_no, s1.name, s1.marks);

return 0;

12. What do you understand by Union? How to access its members, and what
are its applications?

Answer:

A union in C is a user-defined data type where all members share the same
memory location. It is used to save memory by storing only one member's value at
a time.

Declaration and Access:

union UnionName {

data_type member1;

data_type member2;

...

};

Accessing Members:
Use the dot operator (.) for accessing union members.

Example:

#include <stdio.h>

union Data {

int i;

float f;

};
UNIT V

int main( ) {

union Data d;

d.i = 10;

printf("Integer: %d\n", d.i);

d.f = 20.5;

printf("Float: %.2f\n", d.f);

return 0;

Applications:

 Saving memory in embedded systems.

 Handling multiple data types for the same variable.

13. Explain the concept of Array of Structures.

Answer:

An array of structures is a collection of multiple structure variables stored in a


contiguous block of memory.

Declaration:

struct StructureName arrayName[arraySize];

Example:

#include <stdio.h>

struct Student {

int roll_no;

char name[50];

float marks;

};
UNIT V
int main( ) {

struct Student s[2] = {{1, "John", 85.5}, {2, "Alice", 90.0}};

for (int i = 0; i < 2; i++) {

printf("Roll No: %d\nName: %s\nMarks: %.2f\n", s[i].roll_no, s[i].name,


s[i].marks);

return 0;

14. Explain the enumerated data type.

Answer:

An enumerated data type (enum) in C defines a set of named integer constants,


making the code more readable.

Declaration:

enum EnumName {value1, value2, ...};

Example:

#include <stdio.h>

enum Weekdays {Mon, Tues, Wed, Thu, Fri, Sat, Sun};

(or)

enum Months{Jan,Feb,Mar,Aprl,May,Jun,July,Aug,Sep,Oct,Nov,Dec};

(or)

enum RainbowColors{Red,Green.White,Yellow,Blue,Black,Pink};

int main( ) {

enum Weekdays today = Wed;

printf("The value of today is: %d\n", today);

return 0;

} Output: 2
UNIT V

15. What is searching? Explain in detail about linear search algorithm with an
example.

Searching is the process of finding a specific element in a collection of elements.

Linear Search Algorithm:

1. Start from the first element of the array.

2. Compare the target element with the current element.

3. If a match is found, return the index.

4. If the end of the array is reached without a match, return -1.

Example Program:

#include <stdio.h>

int linearSearch(int arr[], int n, int target) {

for (int i = 0; i < n; i++) {

if (arr[i] == target) {

return i; // Element found

} }

return -1; // Element not found

int main( ) {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int target = 30;

int result = linearSearch(arr, n, target);

if (result != -1)

printf("Element found at index %d\n", result);

else

printf("Element not found\n"); return 0; }


UNIT V

16. Write an algorithm to find the roots of a quadratic equation.

Answer:

Algorithm to find the roots of ax2+bx+c=0:

Step 1: start

Step 2: read a, b, c values and declare r1, r2 and D

Step 3: calculate D=b*b-4*a*c

Step 4: if D=0, goto step 5 else goto step 9

Step 5: print roots are equal

Step 6: calculate r1=b/2*a

Step 7: calculate r2=b/2*a

Step 8: display roots r1 and r2 then stop

Step 9: if d>0 then goto step 10 else goto step 14

Step 10: display roots are distinct and real

Step 11: calculate r1=(-b+sqrt*D)/2*a

Step 12: calculate r2=(-b-sqrt*D)/2*a

Step 13: display roots r1 and r2 then stop (7M)

Step 14: else roots are imaginary Step

15: stop
UNIT V

17. Discuss briefly the asymptotic notations and study the complexity of
algorithms.

Answer:

Definition:

Asymptotic notations are mathematical tools used to describe the growth of an


algorithm’s time or space complexity relative to input size.

Types:

1. Big-O (O): Upper bound, worst-case complexity.

o Example: O(n^2) for Bubble Sort.

2. Omega (Ω): Lower bound, best-case complexity.

o Example: Ω(1) for accessing an array element.

3. Theta (Θ): Tight bound, average-case complexity.

o Example: Θ(n) for Linear Search.

Algorithm Best Case Average Case Worst Case

Linear Search Ω(1) Θ(n) O(n)

Binary Search Ω(1) Θ(log n) O(log n)

Bubble Sort Ω(n) Θ(𝑛 ) O(𝑛 )

Insertion Sort Ω(n) Θ(𝑛 ) O(𝑛 )

Selection Sort Ω(𝑛 ) Θ(𝑛 ) O(𝑛 )

Key Use Cases:

 Analyzing algorithm efficiency.

 Comparing two algorithms for performance


UNIT V

18. What is sorting? Explain bubble sort algorithm with an example.

Answer:

Sorting is arranging elements in a specific order (ascending or descending).

Bubble Sort Algorithm:

1. Compare adjacent elements.

2. Swap if the current element is greater than the next.

3. Repeat for all elements until the array is sorted.

Example Program:

#include <stdio.h>

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

} } } }

int main( ) {

int arr[] = {5, 3, 8, 4, 2};

int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: ");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

return 0;
UNIT V
}

19. Write in detail about selection sort.

Selection Sort Algorithm:

1. Divide the array into two parts: sorted and unsorted.

2. Find the smallest element in the unsorted part.

3. Swap it with the first unsorted element.

4. Repeat until the array is sorted.

Example Program:

#include <stdio.h>

void selectionSort(int arr[], int n) {

for (int i = 0; i < n - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

int main( ) {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);


UNIT V
selectionSort(arr, n);

printf("Sorted array: ");

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

return 0;

20. Describe space complexity and time complexity and analyze these for a
simple program.

Answer:

Time Complexity:

 It refers to the amount of time taken by an algorithm to complete as a


function of the input size (n).

 It measures the efficiency of an algorithm.

Space Complexity:

 It refers to the amount of memory consumed by an algorithm as a function of


the input size (n).

Example: Linear Search Analysis

#include <stdio.h>

int linearSearch(int arr[], int n, int target) {

for (int i = 0; i < n; i++) {

if (arr[i] == target)

return i; // Element found

return -1; // Element not found


UNIT V
}

int main( ) {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int target = 30;

int result = linearSearch(arr, n, target);

if (result != -1)

printf("Element found at index %d\n", result);

else

printf("Element not found\n");

return 0;

You might also like