Experiment 1: Basic Operations on a One-Dimensional Integer Array
Objectives:
•To understand and implement various insert operations in an array.
•To learn the fundamental concepts of array manipulation such
as inserting elements at different positions.
Tasks:
1. Insert an element at the beginning of the array:
Write a program to insert an element at the start of a one-dimensional integer
array. #include <iostream>
using namespace
std; int main() {
int n, element;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n + 1];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
cout << "Enter the element to insert at the
start: "; cin >> element;
arr[0] = element;
for (int i = 1; i <= n; i+
+) { arr[i] = arr[i - 1];
}
cout << "Array after
insertion: "; for (int i = 0; i
<= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
2. Insert an element at the end of the array:
Write a program to insert an element at the end of a one-
dimensional integer array.
#include
<iostream> using
namespace std;
int main() {
int n, element;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n + 1];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
cout << "Enter the element to insert at the
end: "; cin >> element;
arr[n] = element;
cout << "Array after
insertion: "; for (int i = 0; i
<= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
3. Insert an element at a specific position in the array:
Write a program to insert an element at a user-specified index
in a one- dimensional integer array.
#include
<iostream> using
namespace std;
int main() {
int n, index, element;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n + 1];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
cout << "Enter the index at which to insert the element (0 to " <<
n << "): "; cin >> index;
if (index < 0 || index > n) {
cout << "Invalid index!" <<
endl; return 1;
}
cout << "Enter the element to
insert: "; cin >> element;
for (int i = n; i > index;
i--) { arr[i] = arr[i - 1];
}
arr[index] = element;
cout << "Array after
insertion: "; for (int i = 0; i
<= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Experiment 2: Advanced Search and Update Operations on a One-
Dimensional Integer Array
Objectives:
● To perform various search and update operations on a one-dimensional array.
● To explore how to efficiently manipulate arrays with different search
and update algorithms.
Tasks:
1. Linear Search (LinearSearch(s)):
Implement a program that searches for an element s using
linear search and returns its index.
#include <iostream>
using namespace std;
int linearSearch(int arr[], int size, int
target) { for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index if the target is found
}
}
return -1; // Return -1 if the target is not found
}
int main() {
int n, target;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
cout << "Enter the element to search
for: "; cin >> target;
int result = linearSearch(arr, n,
target); if (result != -1) {
cout << "Element " << target << " found at index: " << result << endl;
} else {
cout << "Element " << target << " not found in the array." << endl;
}
return 0;
}
2. Binary Search (BinarySearch(x)):
Write a program to search for an element x using binary search (ensure the
array is sorted).
#include
<iostream>
#include
<algorithm>
using namespace std;
int binarySearch(int arr[], int size, int
target) { int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid; // Return the index if the target is found
}
if (arr[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Return -1 if the target is not found
}
int main() {
int n, target;
cout << "Enter the number of elements in the sorted array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " sorted
elements: "; for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the element to search
for: "; cin >> target;
int result = binarySearch(arr, n,
target); if (result != -1) {
cout << "Element " << target << " found at index: " << result << endl;
} else {
cout << "Element " << target << " not found in the array." << endl;
}
return 0;
}
3. Retrieve an element by index (Get(index)):
Implement a function that retrieves the element at a specified
index. #include <iostream>
using namespace std;
int getElementAtIndex(int arr[], int size, int
index) { if (index < 0 || index >= size) {
cout << "Index out of bounds!" <<
endl; return -1; // Return -1 to
indicate an error
}
return arr[index]; // Return the element at the specified index
}
int main() {
int n, index;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
cout << "Enter the index of the element to
retrieve: "; cin >> index;
int result = getElementAtIndex(arr, n,
index); if (result != -1) {
cout << "Element at index " << index << " is: " << result << endl;
}
return 0;
}
4. Set an element at a specific index (Set(index, x)):
Write a function that replaces the element at a given index
with the specified value x.
#include
<iostream> using
namespace std;
void replaceElementAtIndex(int arr[], int size, int
index, int x) { if (index < 0 || index >= size) {
cout << "Index out of bounds!" <<
endl; return; // Exit the function if the
index is invalid
}
arr[index] = x; // Replace the element at the specified index with x
}
int main() {
int n, index, x;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
cout << "Enter the index of the element to
replace: "; cin >> index;
cout << "Enter the new
value: "; cin >> x;
replaceElementAtIndex(arr, n, index,
x); cout << "Array after replacement:
";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
5. Find the Maximum element (Max()):
Write a function that returns the maximum element in the
array. #include <iostream>
using namespace std;
int findMaxElement(int arr[], int
size) { if (size <= 0) {
cout << "Array is empty!" << endl;
return -1; // Return -1 to indicate an error
}
int maxElement = arr[0]; // Assume the first element is the
maximum for (int i = 1; i < size; i++) {
if (arr[i] > maxElement) {
maxElement = arr[i]; // Update maxElement if a larger element is found
}
}
return maxElement; // Return the maximum element found
}
int main()
{ int n;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i+
+) { cin >> arr[i];
}
int maxElement =
findMaxElement(arr, n); if
(maxElement != -1) {
cout << "The maximum element in the array is: " << maxElement << endl;
}
return 0;
}
6. Find the Minimum element (Min()):
Implement a function that returns the minimum element in the
array. #include <iostream>
using namespace std;
int findMinElement(int arr[], int
size) { if (size <= 0) {
cout << "Array is empty!" << endl;
return -1; // Return -1 to indicate an error
}
int minElement = arr[0]; // Assume the first element is the
minimum for (int i = 1; i < size; i++) {
if (arr[i] < minElement) {
minElement = arr[i]; // Update minElement if a smaller element is found
}
}
return minElement; // Return the minimum element found
}
int main()
{ int n;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
int minElement =
findMinElement(arr, n); if
(minElement != -1) {
cout << "The minimum element in the array is: " << minElement << endl;
}
return 0;
}
7. Reverse the array (Reverse()):
Implement a function to reverse the order of elements in the
array. #include <iostream>
using namespace std;
void reverseArray(int arr[], int
size) { int start = 0;
int end = size - 1;
while (start < end) {
swap(arr[start], arr[end]); // Swap the
elements start++;
end--;
}
}
int main()
{ int n;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
reverseArray(arr, n);
cout << "Array after
reversal: "; for (int i = 0; i <
n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
8. Shift elements to the left (Shift()):
Write a function that shifts all elements of the array one position to
the left, setting the last element to 0.
#include
<iostream> using
namespace std;
void shiftLeft(int arr[], int
size) { if (size <= 0) {
return; // No elements to shift
}
for (int i = 1; i < size; i++) {
arr[i - 1] = arr[i]; // Shift elements to the left
}
arr[size - 1] = 0; // Set the last element to 0
}
int main()
{ int n;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
shiftLeft(arr, n);
cout << "Array after shifting
left: "; for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
9. Rotate elements to the left (Rotate()):
Write a function that rotates all elements of the array one
position to the left, placing the first element at the end.
#include
<iostream> using
namespace std;
void rotateLeft(int arr[], int
size) { if (size <= 0) {
return; // No elements to rotate
}
int firstElement = arr[0]; // Store the first
element for (int i = 1; i < size; i++) {
arr[i - 1] = arr[i]; // Shift elements to the left
}
arr[size - 1] = firstElement; // Place the first element at the end
}
int main() {
int n;
cout << "Enter the number of elements in the
array: "; cin >> n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
rotateLeft(arr, n);
cout << "Array after rotation to the
left: "; for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Experiment 3: Pyramid Patterns Using 2D Array
Objective:
• Understand the concept of arrays in C++.
• Implement pyramid patterns using arrays.
• Practice handling two-dimensional arrays for complex patterns.
// Simple Pyramid
#include <iostream>
using namespace std;
int main() {
// Simple Pyramid
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
// Flipped Simple
Pyramid for (int i = 5; i
>= 1; i--) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
// Inverted Pyramid
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) cout
<< "* "; cout << endl;
}
// Flipped Inverted
Pyramid for (int i = 0; i
< 5; i++) {
for (int j = 1; j <= i; j++) cout << " ";
for (int j = 1; j <= 5 - i; j++) cout
<< "* "; cout << endl;
}
// Triangle
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) cout
<< "* "; cout << endl;
}
// Inverted Triangle
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
// Half Diamond
Pattern for (int i = 1; i
<= 5; i++) {
for (int j = 1; j <= i; j++) cout
<< "* "; cout << endl;
}
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++) cout
<< "* "; cout << endl;
}
// Flipped Half Diamond
Pattern for (int i = 1; i <= 5;
i++) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
// Diamond Pattern
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
// Hourglass Pattern
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
for (int i = 2; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++)
cout << " "; for (int j = 1; j <= i;
j++) cout << "* "; cout << endl;
}
// Number Pyramid
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) cout <<
i << " "; cout << endl;
}
// Rotated Number
Pyramid for (int i = 1; i
<= 4; i++) {
for (int j = 1; j <= 4 - i; j++) cout
<< " "; for (int j = 1; j <= i; j++)
cout << i << " "; cout << endl;
}
// Palindrome
Triangle for (int i = 1;
i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) cout
<< " "; for (int j = 1; j <= i; j++)
cout << j << " "; for (int j = i - 1; j
>= 1; j--) cout << j << " "; cout
<< endl;
}
// Alphabet Pyramid
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) cout << (char)(64 + i)
<< " "; cout << endl;
}
// Continuous Alphabet
Pyramid char alphabet = 'A';
for (int i = 1; i <= 5; i+
+) { for (int j = 1; j
<= i; j++) {
cout << alphabet << " ";
alphabet++;
}
cout << endl;
}
return 0;
}