KEMBAR78
C++ Lab Programs | PDF | Matrix (Mathematics) | Computer Programming
0% found this document useful (0 votes)
5 views6 pages

C++ Lab Programs

The document presents two C++ programs: one for implementing a single-dimensional array with functionalities for input, sorting, and displaying elements in both ascending and descending order, and another for performing matrix addition on two user-defined square matrices. The first program uses a vector for dynamic array handling and implements bubble sort for sorting, while the second program uses static 2D arrays for matrix operations. Both programs include user input for data and display the results after computation.

Uploaded by

dsheela5575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

C++ Lab Programs

The document presents two C++ programs: one for implementing a single-dimensional array with functionalities for input, sorting, and displaying elements in both ascending and descending order, and another for performing matrix addition on two user-defined square matrices. The first program uses a vector for dynamic array handling and implements bubble sort for sorting, while the second program uses static 2D arrays for matrix operations. Both programs include user input for data and display the results after computation.

Uploaded by

dsheela5575
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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 ===

You might also like