IMPLEMENTATION OF SINGLE DIMENSIONAL ARRAY
#include <iostream> // Use standard iostream
#include <vector> // For dynamic array handling
using namespace std;
class Number {
private:
vector<int> a; // Dynamic array
int n;
public:
void getdata();
void sorting();
void display();
};
void Number::getdata() {
cout << "\nEnter the number of elements: ";
cin >> n;
cout << "\nEnter the array elements:\n";
a.resize(n); // Resize vector to accommodate n elements
for (int i = 0; i < n; i++) {
cin >> a[i];
}
}
void Number::sorting() {
// Sorting using bubble sort logic
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
swap(a[i], a[j]); // Use standard library swap
}
}
}
}
void Number::display() {
cout << "\nThe array in ascending order:\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
cout << "\nThe array in descending order:\n";
for (int i = n - 1; i >= 0; i--) {
cout << a[i] << " ";
}
cout << endl;
}
int main() {
Number num1;
num1.getdata();
num1.sorting();
num1.display();
return 0;
}
OUTPUT :
Enter the number of elements: 5
Enter the array elements:
25 12 24 26 32
The array in ascending order:
12 24 25 26 32
The array in descending order:
32 26 25 24 12
=== Code Execution Successful ===
IMPLEMENTATION OF MULTIPLE DIMENSIONAL ARRAY
#include <iostream> // Standard input-output library
using namespace std;
class Mat {
private:
int a[10][10], b[10][10], c[10][10]; // Matrices
int n; // Matrix size
public:
void getdata();
void matrix();
void display();
};
void Mat::getdata() {
cout << "ENTER THE ORDER OF MATRIX: ";
cin >> n;
cout << "ENTER THE A MATRIX:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cout << "ENTER THE B MATRIX:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> b[i][j];
}
}
}
void Mat::matrix() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
void Mat::display() {
cout << "\nRESULTANT MATRIX (A + B):\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << c[i][j] << "\t";
}
cout << endl;
}
}
int main() {
Mat a;
cout << "\t\tMATRIX ADDITION:\n";
a.getdata();
a.matrix();
a.display();
return 0;
}
OUTPUT:
MATRIX ADDITION:
ENTER THE ORDER OF MATRIX: 1
ENTER THE A MATRIX:
26
ENTER THE B MATRIX:
45
RESULTANT MATRIX (A + B):
71
=== Code Execution Successful ===