Name Debanjan Deb
Roll No 2330024
5th
Semester Group 6
Date of Submission 24-07-2025
Subject Algorithm Lab
Experiment Number 1
Aim of the experiment:-
1) Aim of the program: Write a program to find out the
second smallest and second largest element stored in an array of n integers.
Input: Size of the array is ‘n’ and read ‘n’ number of elements
from a disc file.
Output: Second smallest, Second largest
2)Aim of the program: Given an array arr[] of size N, find
the prefix sum of the array. A prefix sum array is another array prefixSum[] of
the same size, such that the value of prefixSum[i] is arr[0] + arr[1] + arr[2]
. . . arr[i].
Input Array: 3 4 5 1
2
Output Array: 3 7 12
13 15
3)Aim of the program: Write a program to read ‘n’
integers from a disc file that must contain some duplicate values and store
them into an array. Perform the following operations on the array.
a)Find out the total number of duplicate elements.
b)Find out the most repeating element in the array.
4) Aim of the program: Write a function to ROTATE_RIGHT (p1,
p2) right an array for first p2 elements by 1 position using EXCHANGE (p, q)
function that swaps/exchanges the numbers p & q. Parameter p1 be the
starting address of the array and p2 be the number of elements to be
rotated.
Input:
Enter an array A of size N (9): 11 22 33 44 55 66 77 88 99
Call the function ROTATE_RIGHT (A, 5)
Output:
Before ROTATE: 11 22 33 44 55 66 77 88 99
After ROTATE: 55 11 22 33 44 66 77 88 99
Solution:-
1) Aim of the program: Write a program to find out the
second smallest and second largest element stored in an array of n integers.
Input: Size of the array is ‘n’ and read ‘n’ number of elements
from a disc file.
Output: Second smallest, Second largest
/*Second Largest and Second Smallest*/
#include <stdio.h>
#include <limits.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
if(n < 2) {
printf("Need at least 2 elements to find second smallest and second largest.\n");
return 1;
}
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int smallest = INT_MAX, second_smallest = INT_MAX;
int largest = INT_MIN, second_largest = INT_MIN;
for(i = 0; i < n; i++) {
if(arr[i] < smallest) {
second_smallest = smallest;
smallest = arr[i];
} else if(arr[i] < second_smallest && arr[i] != smallest) {
second_smallest = arr[i];
}
if(arr[i] > largest) {
second_largest = largest;
largest = arr[i];
} else if(arr[i] > second_largest && arr[i] != largest) {
second_largest = arr[i];
}
}
if(second_smallest == INT_MAX)
printf("No second smallest element found (all elements may be equal).\n");
else
printf("Second Smallest: %d\n", second_smallest);
if(second_largest == INT_MIN)
printf("No second largest element found (all elements may be equal).\n");
else
printf("Second Largest: %d\n", second_largest);
printf("Debanjan Deb 2330024");
return 0;
}
2) Aim of the program: Given an array arr[] of size N, find
the prefix sum of the array. A prefix sum array is another array prefixSum[] of
the same size, such that the value of prefixSum[i] is arr[0] + arr[1] + arr[2]
. . . arr[i].
Input Array: 3 4 5
1
2
Output Array: 3 7 12
13 15
/*Prefix Sum*/
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n], prefixSum[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
prefixSum[0] = arr[0];
for (int i = 1; i < n; i++) {
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
printf("Prefix Sum Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", prefixSum[i]);
}
printf("Debanjan Deb");
return 0;
}
3) )Aim of the program: Write a program to read ‘n’
integers from a disc file that must contain some duplicate values and store
them into an array. Perform the following operations on the array.
a)Find out the total number of duplicate elements.
b)Find out the most repeating element in the array.
/*Duplicate Array Operations*/
#include <stdio.h>
int main() {
int n, i, j, count;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n], freq[n];
printf("Enter %d elements (duplicates allowed):\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
freq[i] = -1;
}
for(i = 0; i < n; i++) {
count = 1;
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}
if(freq[i] != 0)
freq[i] = count;
}
int duplicateCount = 0;
for(i = 0; i < n; i++) {
if(freq[i] > 1)
duplicateCount++;
}
printf("Total number of duplicate elements: %d\n", duplicateCount);
int maxFreq = 0, mostRepeated;
for(i = 0; i < n; i++) {
if(freq[i] > maxFreq) {
maxFreq = freq[i];
mostRepeated = arr[i];
}
}
printf("Most repeating element: %d (repeated %d times)\n", mostRepeated, maxFreq);
printf(" \n Debanjan Deb 2330024");
return 0;
}
4)Aim of the program: Write a function to ROTATE_RIGHT (p1,
p2) right an array for first p2 elements by 1 position using EXCHANGE (p, q)
function that swaps/exchanges the numbers p & q. Parameter p1 be the
starting address of the array and p2 be the number of elements to be
rotated.
Input:
Enter an array A of size N (9): 11 22 33 44 55 66 77 88 99
Call the function ROTATE_RIGHT (A, 5)
Output:
Before ROTATE: 11 22 33 44 55 66 77 88 99
After ROTATE: 55 11 22 33 44 66 77 88 99
/*Rotate left and right*/
#include <stdio.h>
void EXCHANGE(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}
void ROTATE_RIGHT(int *p1, int p2) {
int i;
for(i = p2 - 1; i > 0; i--) {
EXCHANGE(&p1[i], &p1[i - 1]);
}
}
int main() {
int A[9] = {11, 22, 33, 44, 55, 66, 77, 88, 99};
printf("Before ROTATE:\t");
for(int i = 0; i < 9; i++) {
printf("%d ", A[i]);
}
ROTATE_RIGHT(A, 5);
printf("\nAfter ROTATE:\t");
for(int i = 0; i < 9; i++) {
printf("%d ", A[i]);
}
printf("\n Debanjan Deb 2330024");
return 0;
}
Conclusion:-
Through the development of these C programs, we have explored several fundamental
operations on arrays, reinforcing key concepts in procedural programming:
1. Duplicate Detection and Frequency Counting:
We implemented a program to count duplicate elements and identify the most frequently
occurring value in an array. This helped in understanding nested loops, frequency arrays, and
conditional logic for comparison and tracking.
2. Prefix Sum Calculation:
The prefix sum program introduced the concept of cumulative summation, showcasing how
to build a new array based on progressive addition. It is a crucial technique in solving range
query problems efficiently.
3. Second Smallest and Second Largest Element Finder:
This program focused on identifying the second smallest and second largest elements in an
array using comparison logic. It emphasized careful handling of edge cases like duplicates
and sorting logic or traversal patterns.
4. Array Rotation Using Swap Function:
The final program demonstrated how to rotate a segment of an array to the right using a
custom EXCHANGE() (swap) function. It highlighted how modular programming improves
clarity and reusability by breaking the logic into small, manageable functions.
Overall, these programs not only reinforce array manipulation skills in C but also instill best
practices like modular design, input validation, and problem decomposition—crucial elements for
writing efficient and maintainable code.