Assignment-2
Assignment-2
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
arr[0] = element;
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;
arr[n] = element;
return 0;
}
<iostream> using
namespace std;
int main() {
int n, index, element;
cout << "Enter the index at which to insert the element (0 to " <<
n << "): "; cin >> index;
arr[index] = element;
return 0;
}
int main() {
int n, target;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
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>
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];
}
return 0;
}
int main() {
int n, index;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
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;
int main() {
int n, index, x;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
replaceElementAtIndex(arr, n, index,
";
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>
int main()
{ int 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;
}
int main()
{ int 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>
int main()
{ int n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
reverseArray(arr, n);
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;
int main()
{ int 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;
}
<iostream> using
namespace std;
int main() {
int n;
int arr[n];
cout << "Enter " << n << "
elements: "; for (int i = 0; i < n;
i++) {
cin >> arr[i];
}
rotateLeft(arr, n);
return 0;
}
// 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;
}
// 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;
}