0% found this document useful (0 votes)
14 views29 pages

Module3 Solutions

The document is a question bank for a Computer Science and Engineering course, focusing on arrays in C programming. It covers definitions, declarations, initializations, accessing elements, and various operations like insertion, deletion, and searching. Additionally, it includes algorithms, flowcharts, and example C programs for practical understanding.

Uploaded by

akhilaappu16
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)
14 views29 pages

Module3 Solutions

The document is a question bank for a Computer Science and Engineering course, focusing on arrays in C programming. It covers definitions, declarations, initializations, accessing elements, and various operations like insertion, deletion, and searching. Additionally, it includes algorithms, flowcharts, and example C programs for practical understanding.

Uploaded by

akhilaappu16
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

DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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

int marks[4]={ 67, 87, 56, 77 }; //integer array initialization


float area[5]={ 23.4, 6.8, 5.5 }; //float array initialization
int marks[4]={ 67, 87, 56, 77, 59 }; //Compile time error

ACCESSING ELEMENTS OF AN ARRAY


Elements of an array can be accessed by index/indices. Array subscript (or index) can be
used to
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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.

• Arrays have 0 as the first index not 1. In this example, mark[0]


• If the size of an array is n, to access the last element, (n-1) index is used. In this
example, mark[4]
• Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will
be
2124d, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.

Input data into array:


Code:
for (x=0; x<=19;x++)
{
printf("enter the integer number %d\n", x);
scanf("%d", &num[x]);
}

Reading out data from an array:


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

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

4. Read key (element to search)


5. For i = 0 to n-1 do
If A[i] == key then
Print "Element found"
Stop
6. End For
7. If loop ends, print "Element not found"
8. Stop

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

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

// INSERT AT BEGINNING
printf("Enter value to insert at beginning: ");
scanf("%d", &value);

for(i=n; i>0; i--)


a[i] = a[i-1];

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

for(i=n; i>=pos; i--)


a[i] = a[i-1];

a[pos-1] = value;
n++;

printf("Updated array: ");


for(i=0; i<n; i++)
printf("%d ", a[i]);

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

3. Partial initialization of array


4. Array initialization without specifying the size
5. Run Time array Initialization

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

Initialization of array elements one by one:


Here the user has the liberty to select the locations and store values and the array. This type
of initialization is not used much practically.
Eg: int b[4];
b[0]= 10;
b[2]=14;

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.

Partial array initialization:


partial array initialization is possible in C language. If the number of values to be initialized
is less than the size of the array, then the elements are initialized in the order from 0th
location. The remaining locations will be initialized to zero automatically.
Ex : Consider the partial initilization
int a[5]={10,15};
Eventhough compiler allocates 5 memory locations, using this declaration
statement, the compiler initializes first two locations with 10 and 15, the next set of memory
locations are automatically initialized to zero.
Initialization Without Specifying Size:
• when you initialize an array at the time of declaration and provide all its elements,
you don't need to specify the size explicitly.
• 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
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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

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


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

return 0;
}

Run Time array Initialization :


• If the values are not known by the programmer in advance, then the user makes
use of run time initialization.
• It helps the programmer to read unknown values from the end users of the program
from keyboard by using input function scanf().
• Here we make use of a looping construct to read the input values from the keyboard
and store them sequentially in the array.

5 Explain how the elements of a one-dimensional array are accessed in C. Illustrate with an
example declaration and accessing the 5th element.

ACCESSING ELEMENTS OF AN ARRAY


Elements of an array can be accessed by index/indices. Array subscript (or index) can be
used to
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
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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.

• Arrays have 0 as the first index not 1. In this example, mark[0]


• If the size of an array is n, to access the last element, (n-1) index is used. In this
example, mark[4]
• Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will
be
2124d, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.

Illustrating an example to acess 5th element:


#include <stdio.h>

int main() {
int arr[10] = {5, 12, 7, 9, 20, 15, 3, 8, 11, 25};

// Accessing the 5th element (index 4)


printf("The 5th element is: %d", arr[4]);

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

printf("Enter numbers: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

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


for(j=0; j<n-i-1; j++) {
if(a[j] > a[j+1]) {
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}

printf("Sorted array: ");


for(i=0; i<n; i++)
printf("%d ", a[i]);
}
8 Write a c program to merge two unsorted array.
#include<stdio.h>

int main() {
int a[50], b[50], c[100];
int n1, n2, i, j;

printf("Enter size of first array: ");


scanf("%d", &n1);
printf("Enter elements: ");
for(i=0; i<n1; i++)
scanf("%d", &a[i]);

printf("Enter size of second array: ");


scanf("%d", &n2);
printf("Enter elements: ");
for(i=0; i<n2; i++)
scanf("%d", &b[i]);

// Merge
for(i=0; i<n1; i++)
c[i] = a[i];
for(j=0; j<n2; j++)
c[n1 + j] = b[j];

printf("Merged array: ");


for(i=0; i<n1+n2; i++)
printf("%d ", c[i]);

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");

// Delete first element


for(i = 0; i < n - 1; i++) {
numbers[i] = numbers[i + 1];
}
n--; // Reduce size

printf("Array after deleting the first element:\n");


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

return 0;
}

Deleting an Element from the End


• We simply reduce the size (n) by 1.
• No shifting is required.
Example Code
#include <stdio.h>

int main() {
int numbers[5] = {10, 20, 30, 40, 50};
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

int n = 5; // Current size


int i;

printf("Original array:\n");
for(i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n");

// Delete last element


n--; // Reduce size

printf("Array after deleting the last element:\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");

// Delete element at position 2 (third element)


for(i = pos; i < n - 1; i++) {
numbers[i] = numbers[i + 1];
}
n--; // Reduce size

printf("Array after deleting the element at index %d:\n", pos);


for(i = 0; i < n; i++) {
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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.

1 Develop a C program to print binary search.


1 #include<stdio.h>

int main() {
int a[100], n, key, low, high, mid, i;

printf("Enter n: ");
scanf("%d", &n);

printf("Enter sorted elements: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

printf("Enter key: ");


scanf("%d", &key);

low = 0;
high = n-1;

while(low <= high) {


mid = (low + high) / 2;

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

printf("Element not found");


return 0;
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

}
1 Illustrate different ways of inserting element in an array.
2 Solution:

Inserting an Element in an Array:


• Insertion means adding a new element at a specific position in the array.
• All subsequent elements are shifted to make space for the new element.
• Inserting can be done at 3 positions:
1. At the beginning
2. At the end
3. At the middle or at a specific location
Example:
Array: A = [10, 20, 30, 40]
Insert 25 at position 2 → A = [10, 20, 25, 30, 40]

Inserting an element at the beginning :


For inserting the element at the beginning, we should first shift all elements of the array to
left by 1 index and then insert an element at the 0th position/index
Example:
int arr[6] = {10, 20, 30, 40, 50};
int n = 5;
int value = 5;

for(int i = n - 1; i >= 0; i--)


{
arr[i + 1] = arr[i];
}
arr[0] = value;
n++;
}

Insertion at the End of an Array


Explanation
• This is the easiest operation.
• No shifting required.
• Just place the new element at index n.
• Increase the array size.

Inserting an element at a given location:


Explanation
• When inserting at a position (say position = 3), all elements from that position to the
end must be shifted one step right.
• Then the new element is placed at that position.
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

Note: Positions start from 1, but indexes start from 0.


So position X corresponds to index X-1.

Example to Implement all the insertions:

#include<stdio.h>

int main() {
int a[100], n, value, pos, i;

// Read number of elements


printf("Enter number of elements: ");
scanf("%d", &n);

// Read array elements


printf("Enter array elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);

// ------------------------------
// INSERTION AT BEGINNING
// ------------------------------
printf("\nEnter value to insert at BEGINNING: ");
scanf("%d", &value);

// Shift elements right


for(i = n; i > 0; i--)
a[i] = a[i-1];

// Insert at index 0
a[0] = value;
n++;

// ------------------------------
// INSERTION AT END
// ------------------------------
printf("\nEnter value to insert at END: ");
scanf("%d", &value);

a[n] = value; // directly insert


n++;

// ------------------------------
// INSERTION AT SPECIFIC POSITION
// ------------------------------
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

printf("\nEnter POSITION and VALUE to insert: ");


scanf("%d %d", &pos, &value);

// Validate position
if(pos < 1 || pos > n+1) {
printf("Invalid position!");
return 0;
}

// Shift elements right from position


for(i = n; i >= pos; i--)
a[i] = a[i-1];

// Insert at correct index


a[pos - 1] = value;
n++;

// ------------------------------
// 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() {

int a[100], n, key, i;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements: ");

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

scanf("%d", &a[i]);
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

printf("Enter element to search: ");

scanf("%d", &key);

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

if(a[i] == key) {

printf("Element found at position %d", i+1);

return 0;

printf("Element not found");

return 0;

1 Write a C program to find the sum and average of n integer numbers.


4 Solution:

#include<stdio.h>

int main() {

int a[100], n, i, sum = 0;

float avg;

printf("Enter n: ");

scanf("%d", &n);

printf("Enter numbers: ");

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

scanf("%d", &a[i]);

sum += a[i];
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

avg = (float)sum / n;

printf("Sum = %d\n", sum);

printf("Average = %.2f", avg);

return 0;

}
1 Write a C program to find the largest element in an array.
5 Solution:

#include<stdio.h>

int main() {

int a[100], n, i, max;

printf("Enter n: ");

scanf("%d", &n);

printf("Enter elements: ");

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

scanf("%d", &a[i]);

max = a[0];

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

if(a[i] > max)

max = a[i];

printf("Largest element = %d", max);

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("Enter numbers: ");


for(i=0; i<n; i++)
scanf("%d", &a[i]);

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

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

int marks[4]={ 67, 87, 56, 77 }; //integer array initialization

float area[5]={ 23.4, 6.8, 5.5 }; //float array initialization


DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

int marks[4]={ 67, 87, 56, 77, 59 }; //Compile time error

The values can be stored in array using following methods:


6. Static initialization
7. Initialization of array elements one by one.
8. Partial initialization of array
9. Array initialization without specifying the size
10. Run Time array Initialization

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

Initialization of array elements one by one:


Here the user has the liberty to select the locations and store values and the array. This type
of initialization is not used much practically.
Eg: int b[4];
b[0]= 10;
b[2]=14;

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.

Partial array initialization:


partial array initialization is possible in C language. If the number of values to be initialized
is less than the size of the array, then the elements are initialized in the order from 0th
location. The remaining locations will be initialized to zero automatically.
Ex : Consider the partial initilization
int a[5]={10,15};
Eventhough compiler allocates 5 memory locations, using this declaration
statement, the compiler initializes first two locations with 10 and 15, the next set of memory
locations are automatically initialized to zero.
Initialization Without Specifying Size:
• when you initialize an array at the time of declaration and provide all its elements,
you don't need to specify the size explicitly.
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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

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


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

return 0;
}

Run Time array Initialization :


• If the values are not known by the programmer in advance, then the user makes
use of run time initialization.
• It helps the programmer to read unknown values from the end users of the program
from keyboard by using input function scanf().
• Here we make use of a looping construct to read the input values from the keyboard
and store them sequentially in the array.

1 What are the different types of operations performed on array?


8 OPERATIONS ON ARRAYS:
Common Operations
1. Traversing – Accessing and printing all elements.
2. Searching – Finding a specific element.
3. Sorting – Arranging elements in ascending/descending order.
4. Inserting – Adding element in the beginning , end and at given postion
5. Deleting – Deleting element in the beginning , end and at given postion
6. Merging- Merging two sorted array or unsorted array
Traversing an Array
• This operation involves visiting each element of the array one by one.
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

• It is used to display the array contents or perform a specific action on


every element, such as computing the sum or printing the elements.
• Traversal is typically implemented using loops like for or while.
Example 1 – Traversing an Array
#include <stdio.h>

int main() {
int numbers[3] = {5, 10, 15};
int i;

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


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

Example 2 – Searching an Element:


#include <stdio.h>

int main() {
int numbers[5] = {2, 4, 6, 8, 10};
int search, i, found = 0;

printf("Enter a number to search: ");


scanf("%d", &search);

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


if(numbers[i] == search) {
found = 1;
break;
}
}

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;

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


for(j = i + 1; j < 5; j++) {
if(numbers[i] > numbers[j]) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}

printf("Sorted array in ascending order:\n");


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

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");

// Delete first element


for(i = 0; i < n - 1; i++) {
numbers[i] = numbers[i + 1];
}
n--; // Reduce size

printf("Array after deleting the first element:\n");


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

return 0;
}

Deleting an Element from the End


• We simply reduce the size (n) by 1.
• No shifting is required.
Example Code
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

#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");

// Delete last element


n--; // Reduce size

printf("Array after deleting the last element:\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");

// Delete element at position 2 (third element)


for(i = pos; i < n - 1; i++) {
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

numbers[i] = numbers[i + 1];


}
n--; // Reduce size

printf("Array after deleting the element at index %d:\n", pos);


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

1 Write a C program to perform binary search in an array


9 Solution:

#include<stdio.h>

int main() {

int a[100], n, key, low, high, mid, i;

printf("Enter n: ");

scanf("%d", &n);

printf("Enter sorted elements: ");

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

scanf("%d", &a[i]);
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

printf("Enter key: ");

scanf("%d", &key);

low = 0;

high = n-1;

while(low <= high) {

mid = (low + high)/2;

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;

printf("Element not found");

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:

ACCESSING ELEMENTS OF AN ARRAY


Elements of an array can be accessed by index/indices. Array
subscript (or index) can be used to
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.

• Arrays have 0 as the first index not 1. In this example, mark[0]


DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

• If the size of an array is n, to access the last element, (n-1)


index is used. In this
example, mark[4]
• Suppose the starting address of mark[0] is 2120d. Then, the
next address, a[1], will be
2124d, address of a[2] will be 2128d and so on. It's because the size
of a float is 4 bytes.

2 Define an array and explain a one-dimensional array with an example


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

int marks[4]={ 67, 87, 56, 77 }; //integer array initialization


float area[5]={ 23.4, 6.8, 5.5 }; //float array initialization
int marks[4]={ 67, 87, 56, 77, 59 }; //Compile time error
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

2 Write a C program to search an element in an array using linear search


2 technique.

2 Write short notes on one-dimensional arrays in C with syntax and an


3 example.
Refer Q21
2 Explain how the elements of a one-dimensional array are stored and
4 accessed in C.
DEPARTMENT OF COMPUTERSCIENCE & ENGINEERING

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;

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

// Traversing the array


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

You might also like