Unit II
Unit II
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Declaration and initialization
printf("Array elements are:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output:
Array elements are:
10 20 30 40 50
int main() {
int numbers[5] = {5, 10, 15, 20, 25}; // Array declaration and initialization
int sum = 0;
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // 2x3 matrix declaration and initialization
String Operations
References:
T1 Chapter 9, pp. 181–200 | T2 Chapter 5, pp. 107–110
1. Definition:
o A string is an array of characters terminated by a null character (\0).
2. Common String Operations:
o Finding Length (strlen):
o char str[] = "Hello";
o int len = strlen(str); // len = 5
o Comparing Strings (strcmp):
o char str1[] = "Apple";
o char str2[] = "Banana";
o int result = strcmp(str1, str2); // Returns -1, 0, or 1
o Concatenating Strings (strcat):
o char str1[20] = "Hello";
o char str2[] = " World";
o strcat(str1, str2); // str1 = "Hello World"
3. Program Example: String Operations
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
// String length
printf("Length of str1: %lu\n", strlen(str1));
// String compare
int cmp = strcmp(str1, str2);
printf("Comparison result: %d\n", cmp);
// String concatenate
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
// String copy
strcpy(str2, "Programming");
printf("Copied string: %s\n", str2);
return 0;
}
Sample Output:
Length of str1: 5
Comparison result: -1
Concatenated string: HelloWorld
Copied string: Programming
Selection Sort
References:
T1 Chapter 10, pp. 201–220
Program Example: Selection Sort
#include <stdio.h>
int main() {
int numbers[5] = {64, 25, 12, 22, 11};
selectionSort(numbers, 5);
printf("Sorted array:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output:
Sorted array:
11 12 22 25 64
Linear Search
References:
R3 Chapter 6, pp. 150–160
Program Example: Linear Search
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int key = 30;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
Sample Output:
Element 30 found at index 2
Binary Search
References:
R4 Chapter 6, pp. 55–70
Program Example: Binary Search
#include <stdio.h>
int main() {
int numbers[6] = {10, 20, 30, 40, 50, 60};
int key = 40;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
Sample Output:
Element 40 found at index 3
int main() {
// Declaration and initialization of an array
int marks[5] = {85, 90, 78, 92, 88};
return 0;
}
Explanation:
This program introduces array declaration and initialization.
It demonstrates how to access and print all elements of an array using a loop.
2. One-Dimensional Array
Program Example: Sum of Elements in One-Dimensional Array
Objective: To teach how to perform operations like summing elements in an array.
Code:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
3. Two-Dimensional Arrays
Program Example: Displaying a Two-Dimensional Array
Objective: To help students understand how to work with two-dimensional arrays (matrices) and
perform basic operations.
Code:
#include <stdio.h>
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
return 0;
}
Explanation:
This program introduces two-dimensional arrays.
It shows how to traverse a matrix and print each element using nested loops.
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
// String length
printf("Length of str1: %lu\n", strlen(str1));
// String comparison
if (strcmp(str1, str2) == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
// String concatenation
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
// String copy
strcpy(str2, "Programming");
printf("Copied string: %s\n", str2);
return 0;
}
Explanation:
This program demonstrates string operations using the string.h library.
It covers length, comparison, concatenation, and copying operations on strings.
5. Selection Sort
Program Example: Selection Sort Algorithm
Objective: To teach the selection sort algorithm by sorting an array of numbers in ascending order.
Code:
#include <stdio.h>
int main() {
int arr[5] = {64, 25, 12, 22, 11};
printf("Unsorted array:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
selectionSort(arr, 5);
printf("\nSorted array:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation:
This program demonstrates the selection sort algorithm to sort an array of integers in
ascending order.
Students will learn how to find the minimum element and swap it with the first unsorted
element.
6. Linear Search
Program Example: Linear Search
Objective: To teach how to search for an element in an array using linear search.
Code:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int key = 30;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
Explanation:
This program implements linear search to find an element in an array.
It searches sequentially from the beginning and returns the index if the element is found, or -1
if the element is not present.
7. Binary Search
Program Example: Binary Search
Objective: To teach how to search for an element in a sorted array using binary search.
Code:
#include <stdio.h>
int main() {
int arr[6] = {10, 20, 30, 40, 50, 60}; // Must be sorted
int key = 40;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
Explanation:
This program implements binary search to find an element in a sorted array.
It repeatedly divides the array into two halves and narrows the search space to find the
element.
int main() {
int arr[] = {1, 2, 4, 5, 6};
int n = 6;
return 0;
}
Practice Variations:
1. Find two missing elements in an array – Given an array of size n-2 where two elements are
missing, find them.
2. Find the duplicate element – Given an array of n elements where one element is repeated,
find the duplicate.
2. Reverse an Array
Problem:
Write a program to reverse an array in place.
Companies:
o Google (2020)
o Microsoft (2019)
Solution:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
reverseArray(arr, n);
return 0;
}
Practice Variations:
1. Reverse the elements of a 2D array – Reverse the rows or columns of a 2D array.
2. Rotate an array – Rotate the elements of an array k times to the right.
int main() {
char str[] = "madam";
if (isPalindrome(str)) {
printf("%s is a palindrome\n", str);
} else {
printf("%s is not a palindrome\n", str);
}
return 0;
}
Practice Variations:
1. Palindrome check using a stack – Use a stack data structure to check if a string is a
palindrome.
2. Longest Palindromic Substring – Given a string, find the longest palindromic substring.
4. Selection Sort
Problem:
Implement the selection sort algorithm to sort an array in ascending order.
Companies:
o Accenture (2019)
o TCS (2020)
Solution:
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
selectionSort(arr, n);
return 0;
}
Practice Variations:
1. Selection Sort in descending order – Modify the selection sort to sort in descending order.
2. Sort an array of strings – Sort an array of strings lexicographically.
5. Linear Search
Problem:
Implement linear search to find an element in an array.
Companies:
o IBM (2020)
o Wipro (2021)
Solution:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int key = 30;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
Practice Variations:
1. Search for a string in an array of strings – Implement linear search for string arrays.
2. Count occurrences of an element – Modify linear search to count how many times an
element appears in an array.
6. Binary Search
Problem:
Write a program to implement binary search on a sorted array.
Companies:
o Flipkart (2018)
o Amazon (2021)
Solution:
#include <stdio.h>
if (arr[mid] == key) {
return mid; // Element found at mid index
} else if (arr[mid] < key) {
low = mid + 1; // Narrow the search to the right half
} else {
high = mid - 1; // Narrow the search to the left half
}
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60}; // Must be sorted
int key = 40;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
Practice Variations:
1. Binary search for first/last occurrence of an element – Find the first or last occurrence of a
target element in a sorted array.
2. Binary search on a rotated array – Perform binary search on a rotated sorted array.
int temp[n];
int j = 0;
int main() {
int arr[] = {10, 20, 20, 30, 30, 30, 40};
int n = 7;
n = removeDuplicates(arr, n);
return 0;
}
Explanation:
This program removes duplicates from a sorted array by comparing consecutive elements and storing
unique ones in a new array.
1. What is an array in C?
Month/Year: May 2010
Answer:
An array in C is a collection of elements of the same data type stored in contiguous memory
locations. The elements of an array can be accessed using an index starting from 0.
Example:
int numbers[5] = {1, 2, 3, 4, 5};
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int main() {
char str[] = "Hello";
printf("Length of the string: %lu\n", strlen(str)); // 5
return 0;
}
int main() {
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2); // Concatenates str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
}
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
if (result == 0) {
printf("Strings are equal.\n");
} else if (result > 0) {
printf("str1 is greater.\n");
} else {
printf("str2 is greater.\n");
}
return 0;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
selectionSort(arr, n);
return 0;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int key = 30;
int index = linearSearch(arr, 5, key);
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
if (arr[mid] == key) {
return mid; // Element found at mid index
} else if (arr[mid] < key) {
low = mid + 1; // Narrow the search to the right half
} else {
high = mid - 1; // Narrow the search to the left half
}
}
int main() {
int arr[] = {10, 20, 30, 40, 50, 60}; // Must be sorted
int key = 40;
if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}
return 0;
}
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;
return 0;
}
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int max = arr[0]; // Initialize max with the first element
return 0;
}
15. Write a program to perform bubble sort on an array.
Month/Year: May 2019
Answer:
Write a C program to sort an array using the bubble sort algorithm.
Code:
#include <stdio.h>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
bubbleSort(arr, n);
return 0;
}
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int min = arr[0]; // Initialize min with the first element
for (int i = 1; i < 5; i++) {
if (arr[i] < min) {
min = arr[i]; // Update min
}
}
return 0;
}
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int temp, start = 0, end = 4;
start++;
end--;
}
return 0;
}
for (int i
= 0; i < 5; i++) { if (arr[i] == key) { count++; } }
printf("Element %d appears %d times.\n", key, count);
return 0;
}
---
**Code**:
```c
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
return 0;
}
int main() {
int arr[5] = {10, 40, 30, 20, 50};
int key = 30;
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Part b
4. Explain the concept of string operations in C (length, compare, concatenate, and copy). Write
a program to demonstrate these operations. November 2014
5. Explain two-dimensional arrays in C. Write a program to find the sum of diagonal elements
of a matrix. May 2015
7. Write a program to implement binary search on a sorted array. Explain the algorithm and its
efficiency. May 2017
9. Write a program to perform string reversal without using any built-in functions in C.2018
10. Explain the concept of sparse matrices and how they are represented using arrays. Write a C
program to convert a 2D array into a sparse matrix.Month/Year: 2019
Here are Part B and Part C questions based on Unit II: Arrays and Strings from Anna University
exams (2020-2024), with their respective months and years of asking:
4. Explain the concept of string operations in C (length, compare, concatenate, and copy). Write
a program to demonstrate these operations.
5. Explain two-dimensional arrays in C. Write a program to find the sum of diagonal elements
of a matrix.
7. Write a program to implement binary search on a sorted array. Explain the algorithm and its
efficiency.
10. Explain the concept of sparse matrices and how they are represented using arrays. Write a C
program to convert a 2D array into a sparse matrix.