Module3 Solutions
Module3 Solutions
Question Bank:
MODULE 3
1 Define an array. Explain the declaration, initialization and accessing of 1-Dimensional array
with program.
Solution :
Definiton:
An array is a group (or collection) of same data types.
Or
An array is a collection of data that holds fixed number of values of same type.
Or
Array is a collection or group of elements (data). All the elements of array
are homogeneous (similar). It has contiguous memory location. Or
An array is a data structured that can store a fixed size sequential collection of elements of
same
data type.
DECLARATION OF AN ARRAY
data-type variable-name[size/length of array];
For example:
int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-1 i.e
first element of arr array will be stored at arr[0] address and last element will occupy arr[9].
INITIALIZATION OF AN ARRAY
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
random value). An array can be initialized at either compile time or at runtime.
Compile time array initialization:
Compile time initializtion of array elements is same as ordinary variable initialization.
Syntax : data_type array_name[size]={v1,v2,...vn/list of values ;
Example
int age[5]={22,25,30,32,35};
access any element stored in array. Subscript starts with 0, which means array_name[0]
would be
used to access first element in an array.
In general array_name[n-1] can be used to access nth element of an array. where n is any
integer
Number.
Example:
float mark[5];
Suppose you declared an array mark as above. The first element is mark[0], second element
is mark[1] and so on.
2 Write an algorithm and flowchart to search an integer from N numbers in ascending order
using Linear Search technique.
Solution:
Algorithm:
1. Start
2. Read n (size of array)
3. Read n elements into array A
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
Flowchart:
3 Write a C program to insert an element in the beginning, end and at a particular position in
an 1D array.
Solution:
#include<stdio.h>
int main() {
int a[100], n, pos, value, i;
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
// INSERT AT BEGINNING
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
a[0] = value;
n++;
// INSERT AT END
printf("Enter value to insert at end: ");
scanf("%d", &value);
a[n] = value;
n++;
// INSERT AT POSITION
printf("Enter position and value: ");
scanf("%d %d", &pos, &value);
a[pos-1] = value;
n++;
return 0;
}
4 Explain four different ways of initializing arrays in C.
Solution:
The values can be stored in array using following methods:
1. Static initialization
2. Initialization of array elements one by one.
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
Static Initialisation:
• We can initialize the array in the same way as the ordinary values when they are
declared.
• The general syntax of initialization of array is
data_type array_name[array_size]= {List of values};
Eg: int b[4]={10,12,14,16};
Suppose if we try to insert more values then the size of the array it will give us an error
“Excess elements in array initializer”
Example:
int b[4]={10,12,14,16,18};
Here the size of the array b is 4 but we are trying to store 5 values hence we will be getting
the error in this case
Only the array locations specified by the user will contain the values which the user wants
the other locations of array will either be 0 or some garbage value.
return 0;
}
5 Explain how the elements of a one-dimensional array are accessed in C. Illustrate with an
example declaration and accessing the 5th element.
Number.
Example:
float mark[5];
Suppose you declared an array mark as above. The first element is mark[0], second element
is mark[1] and so on.
int main() {
int arr[10] = {5, 12, 7, 9, 20, 15, 3, 8, 11, 25};
return 0;
}
6 Write a C program for Bubble sort.
#include<stdio.h>
int main() {
int a[100], n, i, j, temp;
printf("Enter n: ");
scanf("%d", &n);
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
int main() {
int a[50], b[50], c[100];
int n1, n2, i, j;
// Merge
for(i=0; i<n1; i++)
c[i] = a[i];
for(j=0; j<n2; j++)
c[n1 + j] = b[j];
return 0;
}
9 Illustrate different ways of deleting element in an array.
Deleting Elements
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
• Deletion involves removing an element and shifting the subsequent elements to fill
the gap.
• Like insertion, it requires extra effort to maintain the array structure after deletion.
Let us assume we have an array:
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size of the array
Deleting an Element from the Beginning:
• We delete the first element (numbers[0]).
• All other elements are shifted one position to the left.
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size
int i;
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Deleting an Element from the Middle
• We delete an element at a specific position (say index 2, which is 30).
• All elements after that position are shifted one place to the left.
Example Code
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size
int i, pos = 2; // Index of element to delete
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
• Deleting from the beginning requires shifting all elements to the left.
• Deleting from the end is simple; you just reduce the logical size.
• Deleting from the middle requires shifting elements from the deletion point
onwards.
int main() {
int a[100], n, key, low, high, mid, i;
printf("Enter n: ");
scanf("%d", &n);
low = 0;
high = n-1;
if(a[mid] == key) {
printf("Element found at position %d", mid+1);
return 0;
}
else if(key < a[mid])
high = mid - 1;
else
low = mid + 1;
}
}
1 Illustrate different ways of inserting element in an array.
2 Solution:
#include<stdio.h>
int main() {
int a[100], n, value, pos, i;
// ------------------------------
// INSERTION AT BEGINNING
// ------------------------------
printf("\nEnter value to insert at BEGINNING: ");
scanf("%d", &value);
// Insert at index 0
a[0] = value;
n++;
// ------------------------------
// INSERTION AT END
// ------------------------------
printf("\nEnter value to insert at END: ");
scanf("%d", &value);
// ------------------------------
// INSERTION AT SPECIFIC POSITION
// ------------------------------
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
// Validate position
if(pos < 1 || pos > n+1) {
printf("Invalid position!");
return 0;
}
// ------------------------------
// DISPLAY UPDATED ARRAY
// ------------------------------
printf("\nUpdated Array:\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
1 Write a C program to read N numbers into an array & perform Linear search.
3 Solution:
#include<stdio.h>
int main() {
scanf("%d", &n);
scanf("%d", &a[i]);
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
scanf("%d", &key);
if(a[i] == key) {
return 0;
return 0;
#include<stdio.h>
int main() {
float avg;
printf("Enter n: ");
scanf("%d", &n);
scanf("%d", &a[i]);
sum += a[i];
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
avg = (float)sum / n;
return 0;
}
1 Write a C program to find the largest element in an array.
5 Solution:
#include<stdio.h>
int main() {
printf("Enter n: ");
scanf("%d", &n);
scanf("%d", &a[i]);
max = a[0];
max = a[i];
return 0;
}
1 Write a C program to read and print an array of n numbers.
6 #include<stdio.h>
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
int main() {
int a[100], n, i;
printf("Enter n: ");
scanf("%d", &n);
printf("Array elements:\n");
for(i=0; i<n; i++)
printf("%d ", a[i]);
return 0;
}
1 Explain the declaration and initialization of one-dimensional arrays with examples .
7
INITIALIZATION OF AN ARRAY
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
Example
int age[5]={22,25,30,32,35};
Static Initialisation:
• We can initialize the array in the same way as the ordinary values when they are
declared.
• The general syntax of initialization of array is
data_type array_name[array_size]= {List of values};
Eg: int b[4]={10,12,14,16};
Suppose if we try to insert more values then the size of the array it will give us an error
“Excess elements in array initializer”
Example:
int b[4]={10,12,14,16,18};
Here the size of the array b is 4 but we are trying to store 5 values hence we will be getting
the error in this case
Only the array locations specified by the user will contain the values which the user wants
the other locations of array will either be 0 or some garbage value.
• The compiler counts the number of elements and automatically sets the size.
Example – Array Initialization Without Size
#include <stdio.h>
int main() {
// Size is not specified, but initialized with 5 elements
int numbers[] = {10, 20, 30, 40, 50};
return 0;
}
int main() {
int numbers[3] = {5, 10, 15};
int i;
return 0;
}
Searching an Element
• Searching involves checking if a particular value exists in the array.
• The most common technique is linear search, where each element is
compared until a match is found or the end of the array is reached.
• Searching helps in locating elements by value and knowing their position
within the array.
int main() {
int numbers[5] = {2, 4, 6, 8, 10};
int search, i, found = 0;
if(found) {
printf("%d found at index %d\n", search, i);
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
} else {
printf("%d not found in the array.\n", search);
}
return 0;
}
Sorting the Array
• Sorting rearranges the elements in a particular order, typically ascending
or descending.
• It helps in organizing data and optimizing searching operations (like
binary search, which requires sorted data).
• Simple sorting techniques include bubble sort, selection sort, and
insertion sort.
Example 3 – Sorting an Array (Ascending Order):
#include <stdio.h>
int main() {
int numbers[5] = {50, 20, 40, 10, 30};
int i, j, temp;
return 0;
}
Inserting Elements
• Arrays are fixed in size, so inserting a new element involves shifting
existing elements to make space at the required position.
• It is a time-consuming process since multiple elements may need to be
moved.
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
Deleting Elements
• Deletion involves removing an element and shifting the subsequent
elements to fill the gap.
• Like insertion, it requires extra effort to maintain the array structure after
deletion.
Let us assume we have an array:
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size of the array
Deleting an Element from the Beginning:
• We delete the first element (numbers[0]).
• All other elements are shifted one position to the left.
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size
int i;
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size
int i;
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Deleting an Element from the Middle
• We delete an element at a specific position (say index 2, which is 30).
• All elements after that position are shifted one place to the left.
Example Code
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int n = 5; // Current size
int i, pos = 2; // Index of element to delete
printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
• Deleting from the beginning requires shifting all elements to the left.
• Deleting from the end is simple; you just reduce the logical size.
• Deleting from the middle requires shifting elements from the deletion
point onwards.
Merging 2 arrays:
• Merging means combining two arrays into one.
• The resulting array contains all elements from both arrays.
• Merging can be done on two types of Arrays
1. Merging two unsorted arrays.
2. Merging two sorted array
#include<stdio.h>
int main() {
printf("Enter n: ");
scanf("%d", &n);
scanf("%d", &a[i]);
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
scanf("%d", &key);
low = 0;
high = n-1;
if(a[mid] == key) {
return 0;
high = mid - 1;
else
low = mid + 1;
return 0;
}
2 Explain how elements of an array are stored and accessed in C.
0 Solution:
• An array must be declared before it is being used. Declaring
the array means specifying 3 things
• 1. Datatype- What kind of value it can store? Eg: int, char,
float..
• 2. Name – name of the array
• 3. Size – maximum number of values that the array can hold
• Syntax: type name[size];
• Eg: int marks[10];
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING
int marks[10];
This means
Example 2:
DECLARATION OF AN ARRAY
data-type variable-name[size/length of array];
For example:
int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-1 i.e
first element of arr array will be stored at arr[0] address and last element will occupy arr[9].
INITIALIZATION OF AN ARRAY
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
random value). An array can be initialized at either compile time or at runtime.
Compile time array initialization:
Compile time initializtion of array elements is same as ordinary variable initialization.
Syntax : data_type array_name[size]={v1,v2,...vn/list of values ;
Example
int age[5]={22,25,30,32,35};
Refer Q1
2 Write a C program to traverse an array and display all its elements.
5 #include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int i;
return 0;
}
2 What are the basic opera8ons that can be carried out on arrays in C?
6 Explain briefly.
Basic Operations:
1. Traversing
2. Inserting
3. Deleting
Refer this from Q18
2 Explain the syntax and examples for declaring and ini8alizing a one
7 dimensional array.
Refer Q1