Exercise 1: Find the Average of Elements in an Array
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
double arr[n], sum = 0.0;
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
sum += arr[i];
}
double average = sum / n;
cout << "The average is: " << average << endl;
return 0;
}
Exercise 2: Find the Largest Element
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " integers: ";
for (int i = 0; i < n; ++i) { cin >> arr[i]; }
int max = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] > max) { max = arr[i]; }
}
cout << "The largest element in the array is: " << max << endl;
return 0;
}
Exercise 3: Find the Second Largest Element in an Array
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " integers: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
int largest = INT_MIN, secondLargest = INT_MIN;
for (int i = 0; i < n; ++i) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
if (secondLargest == INT_MIN) {
cout << "There is no second largest element." << endl;
} else {
cout << "The second largest element is: " << secondLargest << endl;
}
return 0;
}
Exercise 4: Find the Sum of Even and Odd Numbers Separately in an Array
Write a program that finds the sum of even and odd numbers separately in an array.
#include <iostream>
using namespace std;
int main() {
int n; int evenSum = 0, oddSum = 0; int arr[n];
cout << "Enter the number of elements: "; cin >> n;
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) cin >> arr[i];
for (int i = 0; i < n; ++i) {
if (arr[i] % 2 == 0) evenSum += arr[i];
else oddSum += arr[i];
}
cout << "Sum of even numbers: " << evenSum << "\nSum of odd numbers: " << oddSum;
return 0;
}
Exercise 5: Calculate the Sum of Diagonal Elements of a Matrix
Write a program that calculates the sum of the diagonal elements of a matrix.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the matrix (n x n): ";
cin >> n;
int matrix[n][n];
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> matrix[i][j];
}
}
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += matrix[i][i] + matrix[i][n - 1 - i];
}
// If the matrix is of odd size, subtract the middle element once as it was added twice.
if (n % 2 == 1) {
sum -= matrix[n / 2][n / 2];
}
cout << "Sum of diagonal elements: " << sum << endl;
return 0;
}
Exercise 6: Matrix Addition
#include <iostream>
using namespace std;
int main() {
int rows, cols; int matrixA[rows][cols], matrixB[rows][cols], matrixC[rows][cols];
cout << "Enter the number of rows and columns of the matrices: ";
cin >> rows >> cols;
cout << "Enter elements of the first matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrixA[i][j];
}
}
cout << "Enter elements of the second matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrixB[i][j];
}
}
// Add corresponding elements of both matrices
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
cout << "The resultant matrix after addition is:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << matrixC[i][j] << " ";
}
cout << endl;
}
return 0;
}
Exercise 7: Matrix Multiplication
#include <iostream>
using namespace std;
int main() {
int r1, c1, r2, c2;
cout << "Enter the number of rows and columns of the first matrix: ";
cin >> r1 >> c1;
cout << "Enter the number of rows and columns of the second matrix: ";
cin >> r2 >> c2;
// Check if multiplication is possible
if (c1 != r2) {
cout << "Matrix multiplication is not possible. Number of columns of first matrix must be equal
to the number of rows of second matrix." << endl;
return 0;
}
int matrixA[r1][c1], matrixB[r2][c2], matrixC[r1][c2];
cout << "Enter elements of the first matrix:" << endl;
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c1; ++j) {
cin >> matrixA[i][j];
}
}
cout << "Enter elements of the second matrix:" << endl;
for (int i = 0; i < r2; ++i) {
for (int j = 0; j < c2; ++j) {
cin >> matrixB[i][j];
}
}
// Initialize the result matrix with zeros
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
matrixC[i][j] = 0;
}
}
// Multiply the matrices
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
cout << "The resultant matrix after multiplication is:" << endl;
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
cout << matrixC[i][j] << " ";
}
cout << endl;
}
return 0;
}
Exercise 8:Transpose the Matrix
This program transposes the matrix (flips it over its diagonal).
#include <iostream>
using namespace std;
int main() {
int rows, cols;
// Get the dimensions of the matrix
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
int matrix[rows][cols], transpose[cols][rows];
// Input elements of the matrix
cout << "Enter elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrix[i][j];
}
}
// Transpose the matrix
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
transpose[j][i] = matrix[i][j];
}
}
// Display the resultant transposed matrix
cout << "The transposed matrix is:" << endl;
for (int i = 0; i < cols; ++i) {
for (int j = 0; j < rows; ++j) {
cout << transpose[i][j] << " ";
}
cout << endl;
}
return 0;
}