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

Unit II

The document provides an overview of arrays and strings in programming, detailing their definitions, declarations, initializations, and common operations. It includes examples of one-dimensional and two-dimensional arrays, string operations, and sorting/searching algorithms like selection sort, linear search, and binary search. Additionally, it presents a problem-solving approach for finding a missing element in an array.

Uploaded by

sivapriyamms
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)
6 views

Unit II

The document provides an overview of arrays and strings in programming, detailing their definitions, declarations, initializations, and common operations. It includes examples of one-dimensional and two-dimensional arrays, string operations, and sorting/searching algorithms like selection sort, linear search, and binary search. Additionally, it presents a problem-solving approach for finding a missing element in an array.

Uploaded by

sivapriyamms
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
You are on page 1/ 28

Unit II: Arrays and Strings

Introduction to Arrays: Declaration and Initialization


References:
T1 Chapter 8, pp. 151–165 | T2 Chapter 5, pp. 96–98 | R4 Chapter 5, pp. 45–55
1. Definition:
o An array is a data structure used to store multiple elements of the same type in
contiguous memory locations. It allows random access to elements via an index.
2. Declaration:
3. data_type array_name[array_size];
o Example:
o int numbers[5]; // Declares an integer array of size 5
o float marks[10]; // Declares a float array of size 10
4. Initialization:
o During declaration:
o int numbers[5] = {1, 2, 3, 4, 5};
o At runtime (using loops):
o for (int i = 0; i < 5; i++) {
o numbers[i] = i + 1;
o }
5. Accessing Elements:
o Array elements can be accessed using indices starting from 0.
o Example:
o printf("%d", numbers[0]); // Outputs the first element
6. Program Example: Declare and Initialize an Array
#include <stdio.h>

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

One Dimensional Array


References:
T1 Chapter 8, pp. 166–180
1. Definition:
o A 1D array is a list of elements arranged in a single row.
2. Key Operations:
o Declaration:
o data_type array_name[array_size];
o Initialization:
o int numbers[3] = {10, 20, 30};
o Accessing Elements:
o printf("%d", numbers[1]); // Accesses the second element
3. Program Example: Find the Sum of Array Elements
#include <stdio.h>

int main() {
int numbers[5] = {5, 10, 15, 20, 25}; // Array declaration and initialization
int sum = 0;

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


sum += numbers[i]; // Add each element to sum
}

printf("Sum of array elements: %d\n", sum);


return 0;
}
Sample Output:
Sum of array elements: 75
4. Applications:
o Storing data like marks, salaries, or prices.
o Used in searching and sorting algorithms.

Two Dimensional Arrays


References:
T2 Chapter 5, pp. 99–105
1. Definition:
o A 2D array is a grid or matrix of rows and columns.
2. Declaration:
3. data_type array_name[rows][columns];
Example:
int matrix[3][3];
4. Program Example: Print a 2D Array
#include <stdio.h>

int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // 2x3 matrix declaration and initialization

printf("Matrix elements are:\n");


for (int i = 0; i < 2; i++) { // Iterate over rows
for (int j = 0; j < 3; j++) { // Iterate over columns
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next row
}
return 0;
}
Sample Output:
Matrix elements are:
123
456

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>

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[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

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 linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index of the element
}
}
return -1; // Element not found
}

int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int key = 30;

int index = linearSearch(numbers, 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;
}
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 binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1; // Element not found
}

int main() {
int numbers[6] = {10, 20, 30, 40, 50, 60};
int key = 40;

int index = binarySearch(numbers, 6, key);

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

1. Introduction to Arrays: Declaration, Initialization

Program Example: Declare and Initialize an Array


Objective: To help students understand how to declare and initialize arrays in C.
Code:
#include <stdio.h>

int main() {
// Declaration and initialization of an array
int marks[5] = {85, 90, 78, 92, 88};

printf("Array elements are:\n");


for (int i = 0; i < 5; i++) {
printf("%d ", marks[i]); // Accessing and printing array elements
}

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;

// Calculating the sum of array elements


for (int i = 0; i < 5; i++) {
sum += numbers[i]; // Add each element to sum
}

printf("Sum of array elements: %d\n", sum);


return 0;
}
Explanation:
 This program calculates the sum of elements in a one-dimensional array using a loop.
 Students will learn how to traverse and process array elements.

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}};

printf("Matrix elements are:\n");


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]); // Accessing matrix elements
}
printf("\n");
}

return 0;
}
Explanation:
 This program introduces two-dimensional arrays.
 It shows how to traverse a matrix and print each element using nested loops.

4. String Operations: Length, Compare, Concatenate, Copy


Program Example: String Operations
Objective: To teach the most common string operations in C such as finding length, comparison,
concatenation, and copying.
Code:
#include <stdio.h>
#include <string.h>

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>

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; // Finding the minimum element
}
}
// Swapping the minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

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 linearSearch(int arr[], int size, int key) {


for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return the index if element is found
}
}
return -1; // Element not found
}

int main() {
int arr[5] = {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;
}
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 binarySearch(int arr[], int size, int key) {


int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
return mid; // Return the index if element is found
} else if (arr[mid] < key) {
low = mid + 1; // Ignore the left half
} else {
high = mid - 1; // Ignore the right half
}
}
return -1; // Element not found
}

int main() {
int arr[6] = {10, 20, 30, 40, 50, 60}; // Must be sorted
int key = 40;

int index = binarySearch(arr, 6, key);

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.

1. Find the Missing Element in an Array


Problem:
Given an array containing n-1 distinct integers in the range 1 to n, find the missing element.
 Companies:
o Amazon (2021)
o Google (2020)
Solution:
#include <stdio.h>

int findMissingElement(int arr[], int n) {


int total = (n * (n + 1)) / 2; // Sum of numbers from 1 to n
int sum = 0;

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


sum += arr[i]; // Calculate sum of array elements
}

return total - sum; // The missing element


}

int main() {
int arr[] = {1, 2, 4, 5, 6};
int n = 6;

int missing = findMissingElement(arr, n);


printf("The missing element is %d\n", missing);

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>

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


int temp;
for (int i = 0; i < n / 2; i++) {
// Swap elements from both ends
temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;

reverseArray(arr, n);

printf("Reversed array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\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.

3. Palindrome Check for a String


Problem:
Write a program to check if a given string is a palindrome.
 Companies:
o Microsoft (2021)
o Accenture (2020)
Solution:
#include <stdio.h>
#include <string.h>

int isPalindrome(char str[]) {


int start = 0, end = strlen(str) - 1;

while (start < end) {


if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1; // Is a palindrome
}

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>

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; // Finding the minimum element
}
}
// Swapping the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
selectionSort(arr, n);

printf("Sorted array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\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 linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index if element is found
}
}
return -1; // Element not found
}

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;
}
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>

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;

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
}
}

return -1; // Element not found


}

int main() {
int arr[] = {10, 20, 30, 40, 50, 60}; // Must be sorted
int key = 40;

int index = binarySearch(arr, 6, key);

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.

7. Remove Duplicates from an Array


Problem:
Write a program to remove duplicate elements from a sorted array.
 Companies:
o TCS (2021)
o Cognizant (2020)
Solution:
#include <stdio.h>

int removeDuplicates(int arr[], int n) {


if (n == 0 || n == 1) {
return n;
}

int temp[n];
int j = 0;

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


if (arr[i] != arr[i + 1]) {
temp[j++] = arr[i];
}
}
temp[j++] = arr[n - 1];

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


arr[i] = temp[i];
}

return j; // New size of the array


}

int main() {
int arr[] = {10, 20, 20, 30, 30, 30, 40};
int n = 7;

n = removeDuplicates(arr, n);

printf("Array after removing duplicates: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\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};

2. How do you declare an array in C?


 Month/Year: November 2011
 Answer:
To declare an array in C, specify the data type, the name of the array, and the number of
elements (size) the array can hold.
Syntax:
data_type array_name[array_size];
Example:
int marks[5]; // Declares an integer array of size 5

3. What is the purpose of initializing an array in C?


 Month/Year: May 2011
 Answer:
Initializing an array in C means assigning initial values to its elements at the time of
declaration. It helps to prevent accessing uninitialized memory, which can lead to
unpredictable behavior.
Example:
int arr[5] = {1, 2, 3, 4, 5}; // Array initialization

4. Write a C program to access the elements of an array.


 Month/Year: December 2012
 Answer:
Question: Write a program to print all elements of an array using a loop.
Code:
#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};

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


printf("%d ", arr[i]); // Access and print each element
}
return 0;
}

5. What is a One-Dimensional Array?


 Month/Year: April 2014
 Answer:
A one-dimensional array is a list of elements arranged in a single row or column. It is a
basic array structure that allows the storage of a sequence of elements.
Example:
int arr[5] = {10, 20, 30, 40, 50};

6. What is a Two-Dimensional Array?


 Month/Year: November 2011
 Answer:
A two-dimensional array is an array of arrays, represented in a matrix form with rows and
columns. It allows storing data in a grid-like structure.
Example:
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

7. Explain how to find the length of a string in C.


 Month/Year: May 2012
 Answer:
In C, the length of a string (excluding the null character \0) can be determined using the
strlen() function from the string.h library.
Example:
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello";
printf("Length of the string: %lu\n", strlen(str)); // 5
return 0;
}

8. How do you concatenate two strings in C?


 Month/Year: December 2013
 Answer:
To concatenate two strings in C, we use the strcat() function from the string.h library, which
appends the second string to the end of the first string.
Example:
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2); // Concatenates str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
}

9. How do you compare two strings in C?


 Month/Year: November 2012
 Answer:
To compare two strings in C, use the strcmp() function from the string.h library. It compares
two strings lexicographically and returns:
 0 if the strings are equal,
 A positive value if the first string is greater,
 A negative value if the second string is greater.
Example:
#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Apple";
char str2[] = "Banana";

int result = strcmp(str1, str2); // Compare strings

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;
}

10. What is the Selection Sort Algorithm?


 Month/Year: December 2013
 Answer:
Selection Sort is a simple sorting algorithm that divides the list into two parts: the sorted part
and the unsorted part. It repeatedly selects the smallest (or largest) element from the unsorted
part and swaps it with the first unsorted element.
Example:
#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; // Find minimum element
}
}
// Swap the minimum element with the first unsorted element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;

selectionSort(arr, n);

printf("Sorted array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

11. What is Linear Search?


 Month/Year: May 2015
 Answer:
Linear Search is a simple search algorithm that checks each element in a list sequentially until
the desired element is found or the list ends.
Example:
#include <stdio.h>

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


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Element found at index
}
}
return -1; // Element not found
}

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;
}

12. How does Binary Search work?


 Month/Year: December 2016
 Answer:
Binary Search is an efficient algorithm for finding an element in a sorted array. It works by
repeatedly dividing the search interval in half. If the value of the search key is less than the
item in the middle of the interval, the search continues in the left half, otherwise in the right
half.
Example:
#include <stdio.h>

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;

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
}
}

return -1; // Element not found


}

int main() {
int arr[] = {10, 20, 30, 40, 50, 60}; // Must be sorted
int key = 40;

int index = binarySearch(arr, 6, key);

if (index != -1) {
printf("Element %d found at index %d\n", key, index);
} else {
printf("Element %d not found\n", key);
}

return 0;
}

13. Program to find the sum of elements in an array


 Month/Year: May 2017
 Answer:
Write a C program to find the sum of elements of an array.
Code:
#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;

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


sum += arr[i]; // Add each element to sum
}

printf("Sum of elements: %d\n", sum);

return 0;
}

14. Program to find the maximum element in an array


 Month/Year: May 2018
 Answer:
Write a C program to find the maximum element of an array.
Code:
#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int max = arr[0]; // Initialize max with the first element

for (int i = 1; i < 5; i++) {


if (arr[i] > max) {
max = arr[i]; // Update max
}
}

printf("Maximum element: %d\n", max);

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>

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]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;

bubbleSort(arr, n);

printf("Sorted array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

16. Program to find the minimum element in an array


 Month/Year: May 2020
 Answer:
Write a C program to find the minimum element of an array.
Code:
#include <stdio.h>

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
}
}

printf("Minimum element: %d\n", min);

return 0;
}

17. Write a program to reverse an array.


 Month/Year: May 2021
 Answer:
Write a C program to reverse the elements of an array.
Code:
#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};
int temp, start = 0, end = 4;

while (start < end) {


// Swap the elements
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

start++;
end--;
}

printf("Reversed array: ");


for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

return 0;
}

18. Write a program to count the frequency of an element in an array.


 Month/Year: May 2022
 Answer:
Write a C program to count how many times an element appears in an array.
Code:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 20, 50};
int key = 20;
int count = 0;

for (int i
= 0; i < 5; i++) { if (arr[i] == key) { count++; } }
printf("Element %d appears %d times.\n", key, count);

return 0;
}

---

### **19. Program to find the sum of diagonal elements in a 2D array.**


- **Month/Year**: May 2023
- **Answer**:
Write a C program to find the sum of diagonal elements in a two-dimensional array.

**Code**:
```c
#include <stdio.h>

int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;

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


sum += matrix[i][i]; // Sum of diagonal elements
}

printf("Sum of diagonal elements: %d\n", sum);

return 0;
}

20. Program to implement linear search on an unsorted array.


 Month/Year: May 2024
 Answer:
Write a C program to perform a linear search on an unsorted array.
Code:
#include <stdio.h>

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


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}

int main() {
int arr[5] = {10, 40, 30, 20, 50};
int key = 30;

int result = linearSearch(arr, 5, key);

if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}

return 0;
}

Part b

1. Explain the concept of arrays in C with examples. November 2011

2. Write a program to implement a linear search on an array. May 2012

3. Write a C program to perform bubble sort on an array of integers. December 2013

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

6. Explain the concept of Selection Sort with a C program implementation. 2016

7. Write a program to implement binary search on a sorted array. Explain the algorithm and its
efficiency. May 2017

8. Write a program to implement a matrix multiplication using two-dimensional arrays.


December 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:

1. Explain the concept of arrays in C with examples.

 Month/Year: May 2020

2. Write a program to implement a linear search on an array.

 Month/Year: November 2020

3. Write a C program to perform bubble sort on an array of integers.

 Month/Year: May 2021

4. Explain the concept of string operations in C (length, compare, concatenate, and copy). Write
a program to demonstrate these operations.

 Month/Year: November 2021

5. Explain two-dimensional arrays in C. Write a program to find the sum of diagonal elements
of a matrix.

 Month/Year: May 2022

6. Explain the concept of Selection Sort with a C program implementation.

 Month/Year: November 2022

7. Write a program to implement binary search on a sorted array. Explain the algorithm and its
efficiency.

 Month/Year: May 2023

8. Write a program to implement matrix multiplication using two-dimensional arrays.

 Month/Year: November 2023


9. Write a program to perform string reversal without using any built-in functions in C.

 Month/Year: May 2024

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: November 2024

You might also like