0% found this document useful (0 votes)
5 views

C++ LAB MANUAL

The document contains multiple C++ programs demonstrating basic array operations, matrix operations including addition, inverse, transpose, and multiplication, as well as statistical functions such as mean, median, mode, standard deviation, variance, covariance, and skewness. Each program is accompanied by example outputs illustrating their functionality. The programs utilize standard libraries and functions to perform calculations and display results.

Uploaded by

yesudossj
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 views

C++ LAB MANUAL

The document contains multiple C++ programs demonstrating basic array operations, matrix operations including addition, inverse, transpose, and multiplication, as well as statistical functions such as mean, median, mode, standard deviation, variance, covariance, and skewness. Each program is accompanied by example outputs illustrating their functionality. The programs utilize standard libraries and functions to perform calculations and display results.

Uploaded by

yesudossj
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/ 14

1.

WRITE A C ++ PROGRAM TO EXECUTE THE BASIC COMMANDS OF AN ARRAY

#include <iostream>

using namespace std;

int main() {

// Declare an array of 5 integers

int arr[5] = {1, 2, 3, 4, 5};

// Accessing and printing array elements

cout << "Array elements: ";

for(int i = 0; i < 5; i++) {

cout << arr[i] << " ";

cout << endl;

// Modify an array element

arr[2] = 10; // Changing the third element to 10

// Print array after modification

cout << "Array after modification: ";

for(int i = 0; i < 5; i++) {

cout << arr[i] << " ";

cout << endl;

// Find sum of array elements

int sum = 0;

for(int i = 0; i < 5; i++) {

sum += arr[i];
}

// Find average of array elements

double average = sum / 5.0;

// Print sum and average

cout << "Sum of array elements: " << sum << endl;

cout << "Average of array elements: " << average << endl;

return 0;

Output:

Array elements: 1 2 3 4 5

Array after modification: 1 2 10 4 5

Sum of array elements: 22

Average of array elements: 4.4

2.Write a C++ Program to create a Matrix and Perform the operations Addition ,Inverse, Transpose,and
multiplication operations.

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

// Function to display a matrix

void displayMatrix(double matrix[3][3], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

cout << setw(5) << matrix[i][j] << " ";

}
cout << endl;

// Function to add two matrices

void addMatrices(double matrix1[3][3], double matrix2[3][3], double result[3][3], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Function to find the transpose of a matrix

void transposeMatrix(double matrix[3][3], double result[3][3], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[j][i] = matrix[i][j];

// Function to calculate the determinant of a matrix

double determinant(double matrix[3][3], int n) {

double det = 0;

if (n == 2) {

det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];

} else if (n == 3) {

det = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])


- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])

+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);

return det;

// Function to find the inverse of a matrix

bool inverseMatrix(double matrix[3][3], double inverse[3][3], int n) {

double det = determinant(matrix, n);

if (det == 0) {

cout << "Inverse cannot be calculated as determinant is zero!" << endl;

return false;

double adjoint[3][3];

// Finding adjoint

adjoint[0][0] = matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1];

adjoint[0][1] = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]);

adjoint[0][2] = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];

adjoint[1][0] = -(matrix[0][1] * matrix[2][2] - matrix[0][2] * matrix[2][1]);

adjoint[1][1] = matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0];

adjoint[1][2] = -(matrix[0][0] * matrix[2][1] - matrix[0][1] * matrix[2][0]);

adjoint[2][0] = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];

adjoint[2][1] = -(matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0]);

adjoint[2][2] = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];

// Finding inverse
double invDet = 1.0 / det;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

inverse[i][j] = adjoint[i][j] * invDet;

return true;

// Function to multiply two matrices

void multiplyMatrices(double matrix1[3][3], double matrix2[3][3], double result[3][3], int rows, int cols)
{

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[i][j] = 0;

for (int k = 0; k < cols; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

int main() {

// Define two 3x3 matrices

double matrix1[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

double matrix2[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

double result[3][3];

double inverse[3][3];
// Display matrices

cout << "Matrix 1: " << endl;

displayMatrix(matrix1, 3, 3);

cout << "Matrix 2: " << endl;

displayMatrix(matrix2, 3, 3);

// Matrix Addition

addMatrices(matrix1, matrix2, result, 3, 3);

cout << "\nMatrix Addition (Matrix1 + Matrix2): " << endl;

displayMatrix(result, 3, 3);

// Matrix Transpose

transposeMatrix(matrix1, result, 3, 3);

cout << "\nMatrix1 Transpose: " << endl;

displayMatrix(result, 3, 3);

// Matrix Inverse

if (inverseMatrix(matrix1, inverse, 3)) {

cout << "\nInverse of Matrix1: " << endl;

displayMatrix(inverse, 3, 3);

// Matrix Multiplication

multiplyMatrices(matrix1, matrix2, result, 3, 3);

cout << "\nMatrix Multiplication (Matrix1 * Matrix2): " << endl;

displayMatrix(result, 3, 3);

return 0;
}

Output:

Matrix 1:

1 2 3

4 5 6

7 8 9

Matrix 2:

9 8 7

6 5 4

3 2 1

Matrix Addition (Matrix1 + Matrix2):

10 10 10

10 10 10

10 10 10

Matrix1 Transpose:

1 4 7

2 5 8

3 6 9

Inverse of Matrix1:

Inverse cannot be calculated as determinant is zero!

Matrix Multiplication (Matrix1 * Matrix2):

30 24 18

84 69 54

138 114 90
3.Write a C ++ Program to execute the statistical function:mean,median,mode

#include <iostream>

#include <vector>

#include <algorithm>

#include <unordered_map>

using namespace std;

// Function to calculate mean

double mean(vector<int>& data) {

double sum = 0;

for (int i = 0; i < data.size(); i++) {

sum += data[i];

return sum / data.size();

// Function to calculate median

double median(vector<int>& data) {

sort(data.begin(), data.end());

int n = data.size();

if (n % 2 == 0) {

return (data[n / 2 - 1] + data[n / 2]) / 2.0; // Average of two middle elements

} else {

return data[n / 2]; // Middle element

// Function to calculate mode

vector<int> mode(vector<int>& data) {


unordered_map<int, int> frequencyMap;

vector<int> result;

int maxFrequency = 0;

// Calculate frequency of each element

Output:

Enter the number of elements in the data set: 7

Enter the data elements: 1 2 2 3 3 3 4

Mean: 2.57143

Median: 3

Mode: 3

4. Write a C ++ Program to execute the statistical functions : Std deviation,Varience and covariance

#include <iostream>

#include <vector>

#include <cmath>

using namespace std;

// Function to calculate mean

double mean(const vector<double>& data) {

double sum = 0;

for (double num : data) {

sum += num;

return sum / data.size();

// Function to calculate variance

double variance(const vector<double>& data) {


double meanValue = mean(data);

double var = 0;

for (double num : data) {

var += pow(num - meanValue, 2);

return var / data.size(); // population variance

// Function to calculate standard deviation

double stdDeviation(const vector<double>& data) {

return sqrt(variance(data)); // Standard deviation is the square root of variance

// Function to calculate covariance between two datasets

double covariance(const vector<double>& data1, const vector<double>& data2) {

if (data1.size() != data2.size()) {

cout << "Datasets must have the same number of elements for covariance calculation!" << endl;

return -1;

double mean1 = mean(data1);

double mean2 = mean(data2);

double cov = 0;

for (size_t i = 0; i < data1.size(); ++i) {

cov += (data1[i] - mean1) * (data2[i] - mean2);

return cov / data1.size(); // population covariance

int main() {
// Input the data sets

vector<double> data1, data2;

int n;

cout << "Enter the number of elements in the data set: ";

cin >> n;

cout << "Enter the elements of the first dataset: ";

for (int i = 0; i < n; i++) {

double value;

cin >> value;

data1.push_back(value);

cout << "Enter the elements of the second dataset: ";

for (int i = 0; i < n; i++) {

double value;

cin >> value;

data2.push_back(value);

// Calculate Standard Deviation, Variance, and Covariance

double stdDev1 = stdDeviation(data1);

double variance1 = variance(data1);

double stdDev2 = stdDeviation(data2);

double variance2 = variance(data2);

double cov = covariance(data1, data2);

// Output the results


cout << "\nFirst Dataset Statistics:" << endl;

cout << "Standard Deviation: " << stdDev1 << endl;

cout << "Variance: " << variance1 << endl;

cout << "\nSecond Dataset Statistics:" << endl;

cout << "Standard Deviation: " << stdDev2 << endl;

cout << "Variance: " << variance2 << endl;

cout << "\nCovariance between the two datasets: " << cov << endl;

return 0;

Output:

Enter the number of elements in the data set: 5

Enter the elements of the first dataset: 1 2 3 4 5

Enter the elements of the second dataset: 5 4 3 2 1

First Dataset Statistics:

Standard Deviation: 1.41421

Variance: 2

Second Dataset Statistics:

Standard Deviation: 1.41421

Variance: 2

Covariance between the two datasets: -2

5 Write a C ++ Program to draw the skewness.

#include <iostream>

#include <vector>
#include <cmath>

using namespace std;

// Function to calculate mean

double mean(const vector<double>& data) {

double sum = 0;

for (double num : data) {

sum += num;

return sum / data.size();

// Function to calculate standard deviation

double standardDeviation(const vector<double>& data) {

double m = mean(data);

double sum = 0;

for (double num : data) {

sum += pow(num - m, 2);

return sqrt(sum / data.size());

// Function to calculate skewness

double skewness(const vector<double>& data) {

double m = mean(data);

double sd = standardDeviation(data);

double sum = 0;

for (double num : data) {


sum += pow((num - m) / sd, 3);

return (data.size() * sum) / ((data.size() - 1) * (data.size() - 2));

int main() {

vector<double> data;

int n;

cout << "Enter the number of data points: ";

cin >> n;

cout << "Enter the data points: ";

for (int i = 0; i < n; i++) {

double value;

cin >> value;

data.push_back(value);

double skew = skewness(data);

cout << "Skewness: " << skew << endl;

return 0;

You might also like