UNIT-3
1. What is Array? Explain different types of arrays in c with an example each.
A. An array is a collection of elements of the same data type, stored in contiguous memory locations. Each
element is identified by its index, which starts from 0.
Different Types of Arrays in C
1. One-Dimensional Array:
o A simple array where elements are arranged in a linear sequence.
o Example:
int numbers[5] = {10, 20, 30, 40, 50};
o Here, numbers is an array of 5 integers.
2. Multi-Dimensional Array:
o An array where elements are arranged in rows and columns.
o Two-Dimensional Array:
A common type where elements are arranged in rows and columns like a matrix.
Example:
int matrix[3][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
This creates a 3x4 matrix.
o Higher-Dimensional Arrays:
Can have more than two dimensions.
Example:
int cube[2][3][4];
This creates a 2x3x4 cube.
3. Character Array:
o An array specifically used to store characters.
o Often used to represent strings.
o Example:
char name[10] = "Alice";
This creates a character array to store the name "Alice".
4. Pointer Array:
o An array where each element is a pointer to another data type.
o Used to store addresses of other variables or arrays.
o Example:
int numbers[5] = {10, 20, 30, 40, 50};
int *ptrs[5];
for (int i = 0; i < 5; i++) {
ptrs[i] = &numbers[i];
This creates an array ptrs where each element points to a corresponding element in
the numbers array.
Key Points:
Array elements are accessed using their index.
Array size is fixed at declaration.
Array indices start from 0.
Out-of-bounds access can lead to undefined behavior.
Character arrays are often used for string manipulation.
Pointer arrays are useful for dynamic memory allocation and indirect access.
2.Write a C program to calculate sum and average of array elements.
A. #include <stdio.h>
int main() {
int n, i;
float sum = 0, average;
printf("Enter the number of elements: ");
scanf("%d", &n)
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
average = sum / n;
prinf("Sum of elements: %.2f\n", sum);
printf("Average of elements: %.2f\n", average);
return 0;
}
3. With respect to 1-D arrays explain the following with examples.
a) Declaration of arrays
b) Initialization of arrays
c) Accessing elements of array
A. a) Declaration of Arrays
An array is declared by specifying its data type, name, and size (number of elements).
data_type array_name[size];
data_type: The type of elements the array will hold (e.g., int, float, char).
array_name: The name given to the array.
size: The number of elements the array can store.
Example:
int numbers[5]; // Declares an array of 5 integers
b) Initialization of Arrays
Arrays can be initialized with values at the time of declaration.
Static initialization:
int numbers[5] = {10, 20, 30, 40, 50};
Dynamic initialization:
int numbers[5];
numbers[0] = 10;
numbers[1] = 20;
// ...
Partial initialization:
int numbers[5] = {10, 20};
The remaining elements will be initialized to 0.
c) Accessing Elements of Array
Elements of an array are accessed using their index, which starts from 0.
array_name[index]
Example:
int numbers[5] = {10, 20, 30, 40, 50};
// Accessing the first element:
int firstElement = numbers[0];
// Accessing the last element:
int lastElement = numbers[4]; // or numbers[size - 1]
// Modifying an element:
numbers[2] = 100;
4.Derive an algorithm for linear search and explain with an illustration.
A. Linear Search Algorithm
Linear search is a simple algorithm that sequentially checks each element of an array until the desired element
is found or the end of the array is reached.
Algorithm:
1. Initialize:
o Set a variable found to false to indicate that the element has not been found yet.
o Set a variable i to 0 to represent the current index being examined.
2. Search:
o While i is less than the array size and found is false:
If the element at index i is equal to the target element:
Set found to true.
Increment i to move to the next element.
3. Return:
o If found is true, return the index i.
o If found is false, return -1 to indicate that the element was not found.
Illustration:
Consider an array arr = {10, 20, 30, 40, 50} and a target element x = 30.
i = 0, arr[0] = 10, not found
i = 1, arr[1] = 20, not found
i = 2, arr[2] = 30, found!
C implementation:
int linearSearch(int arr[], int n, int x) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
return -1;
}
Time complexity:
In the worst case, the entire array needs to be searched, resulting in a time complexity of O(n).
Space complexity:
The algorithm only uses constant extra space for variables, making the space complexity O(1).
5.Develop a program to search the key element in the given list.
A. #include <stdio.h>
int linearSearch(int arr[], int n, int x) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
return i; // Element found at index i
return -1; // Element not found
int main() {
int n, x, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("Enter
the element to search: ");
scanf("%d", &x);
int result = linearSearch(arr, n, x);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
return 0;
This program incorporates the following improvements:
Clear and concise function: The linear Search function is well-structured and easy to understand.
Meaningful variable names: The variable names are descriptive and enhance code readability.
Consistent formatting: The code is formatted consistently for better readability.
Comprehensive error handling: The program handles the case where the element is not found by
returning -1.
User-friendly output: The output messages are informative and easy to interpret.