1D and 2D Array Operations
a) Sorting, b) Searching, c) Addition & multiplication of
Matrix, d) mean and median of array
a)Sorting: ascending order
#include <iostream>
using namespace std;
int main() {
int n, temp;
int a[10];
cout << "Enter the size of array: ";
cin >> n;
cout << "Enter the elements of array:" << endl;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// Sorting in ascending order
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout << "After sorting, the array is: ";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
Bubble sort:
#include <iostream>
using namespace std;
int main() {
int n;
int a[50]; // array size (max 50 elements for demo)
cout << "Enter the size of array: ";
cin >> n;
cout << "Enter the elements of array:" << endl;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int count=1;
// Bubble Sort
while(count<n)
{
for (int i = 0; i < n - count; i++) {
if (a[i] > a[i + 1]) {
// swap
int temp = a[i];
a[i] = a[i + 1];
a[ + 1] = temp;
}
}
count++;
}
cout << "Array after Bubble Sort (Ascending Order): ";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
B) searching :
1)linear: program
#include <iostream>
using namespace std;
int main() {
int n, num;
cout << "Enter size of array: ";
cin >> n;
int a[n]; // array of size n
cout << "Enter " << n << " elements:\n";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << "Enter number to search: ";
cin >> num;
bool found = false;
for (int i = 0; i < n; i++) {
if (a[i] == num) {
cout << "Found at position " << i + 1;
found = true;
break;
}
}
if (!found) {
cout << "Element not found";
}
return 0;
}
2)Binary: program
#include <iostream>
using namespace std;
int main() {
int a[5] = {1, 8, 18, 21, 30};
int left = 0, right = 4, mid; // right = size-1
int num = 21;
bool found = false;
do {
mid = (left + right) / 2;
if (a[mid] == num) {
cout << "Found at position " << mid + 1;
found = true;
break;
}
else if (a[mid] < num) {
left = mid + 1;
}
else {
right = mid - 1;
}
} while (left <= right);
if (!found) {
cout << "Element not found";
}
return 0;
}
c) Addition of matrix
#include <iostream>
using namespace std;
int main() {
int m, n;
cout << "Enter rows and columns of matrices: ";
cin >> m >> n;
int A[10][10], B[10][10], C[10][10];
cout << "Enter elements of Matrix A:\n";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
cout << "Enter elements of Matrix B:\n";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> B[i][j];
}
}
cout << "Resultant Matrix after Addition:\n";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout<<( C[i][j] = A[i][j] + B[i][j]);
}
cout << endl;
}
return 0;
}
Multiplication of matrix
#include <iostream>
using namespace std;
int main() {
int m, n, p;
cout << "Enter rows and columns of Matrix A: ";
cin >> m >> n;
cout << "Enter columns of Matrix B: ";
cin >> p; // rows of B = n
int A[10][10], B[10][10], C[10][10];
cout << "Enter elements of Matrix A:\n";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> A[i][j];
}
}
cout << "Enter elements of Matrix B:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
cin >> B[i][j];
}
}
// Multiplication
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
C[i][j] = 0;
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
cout << "Resultant Matrix after Multiplication:\n";
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}