Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Code for practice

Uploaded by

tommyassefa95
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code for practice

Uploaded by

tommyassefa95
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Array operation

#include <iostream>
using namespace std;

void addArrays(int arr1[], int arr2[], int result[], int size) {


for (int i = 0; i < size; i++) {
result[i] = arr1[i] + arr2[i];
}
}

void subtractArrays(int arr1[], int arr2[], int result[], int size) {


for (int i = 0; i < size; i++) {
result[i] = arr1[i] - arr2[i];
}
}

void multiplyArrays(int arr1[], int arr2[], int result[], int size) {


for (int i = 0; i < size; i++) {
result[i] = arr1[i] * arr2[i];
}
}

void displayArray(int arr[], int size) {


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
const int size = 5;
int arr1[size] = {1, 2, 3, 4, 5};
int arr2[size] = {5, 4, 3, 2, 1};
int result[size];

cout << "Array 1: ";


displayArray(arr1, size);

cout << "Array 2: ";


displayArray(arr2, size);

// Addition
addArrays(arr1, arr2, result, size);
cout << "Addition Result: ";
displayArray(result, size);

// Subtraction
subtractArrays(arr1, arr2, result, size);
cout << "Subtraction Result: ";
displayArray(result, size);

// Multiplication
multiplyArrays(arr1, arr2, result, size);
cout << "Multiplication Result: ";
displayArray(result, size);

return 0;
}
Explanation
addArrays: Adds corresponding elements of arr1 and arr2.
subtractArrays: Subtracts corresponding elements of arr2 from arr1.
multiplyArrays: Multiplies corresponding elements of arr1 and arr2.
displayArray: Outputs the array elements.
Sample Output
plaintext
Copy code
Array 1: 1 2 3 4 5
Array 2: 5 4 3 2 1
Addition Result: 6 6 6 6 6
Subtraction Result: -4 -2 0 2 4
Multiplication Result: 5 8 9 8 5

matrix operation using array

#include <iostream>
using namespace std;

const int N = 3; // Dimension of the matrices

// Function for Matrix Addition


void addMatrices(int mat1[][N], int mat2[][N], int result[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

// Function for Matrix Subtraction


void subtractMatrices(int mat1[][N], int mat2[][N], int result[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}

// Function for Matrix Multiplication


void multiplyMatrices(int mat1[][N], int mat2[][N], int result[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = 0;
for (int k = 0; k < N; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

// Function for Element-wise Division


void divideMatrices(int mat1[][N], int mat2[][N], float result[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (mat2[i][j] != 0) {
result[i][j] = static_cast<float>(mat1[i][j]) / mat2[i][j];
} else {
result[i][j] = 0; // Handle division by zero
cout << "Warning: Division by zero at position (" << i << ", " << j
<< ")\n";
}
}
}
}

// Function to Display an Integer Matrix


void displayMatrix(int mat[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mat[i][j] << "\t";
}
cout << endl;
}
}

// Function to Display a Float Matrix


void displayMatrix(float mat[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << mat[i][j] << "\t";
}
cout << endl;
}
}

int main() {
int mat1[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int mat2[N][N] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int resultInt[N][N]; // For integer results of addition, subtraction,
multiplication
float resultFloat[N][N]; // For float results of division

cout << "Matrix 1:\n";


displayMatrix(mat1);

cout << "\nMatrix 2:\n";


displayMatrix(mat2);

// Addition
addMatrices(mat1, mat2, resultInt);
cout << "\nAddition Result:\n";
displayMatrix(resultInt);

// Subtraction
subtractMatrices(mat1, mat2, resultInt);
cout << "\nSubtraction Result:\n";
displayMatrix(resultInt);

// Multiplication
multiplyMatrices(mat1, mat2, resultInt);
cout << "\nMultiplication Result:\n";
displayMatrix(resultInt);

// Division (element-wise)
divideMatrices(mat1, mat2, resultFloat);
cout << "\nDivision Result:\n";
displayMatrix(resultFloat);

return 0;
}
Explanation
Addition (addMatrices): Adds each element of mat1 with the corresponding element of
mat2.
Subtraction (subtractMatrices): Subtracts each element of mat2 from the
corresponding element of mat1.
Multiplication (multiplyMatrices): Performs matrix multiplication by taking the dot
product of rows and columns.
Division (divideMatrices): Performs element-wise division and handles division by
zero by printing a warning and setting the result to zero.
Sample Output
plaintext
Copy code
Matrix 1:
1 2 3
4 5 6
7 8 9

Matrix 2:
9 8 7
6 5 4
3 2 1

Addition Result:
10 10 10
10 10 10
10 10 10

Subtraction Result:
-8 -6 -4
-2 0 2
4 6 8

Multiplication Result:
30 24 18
84 69 54
138 114 90

Division Result:
0.111111 0.25 0.428571
0.666667 1 1.5
2.33333 4 9

Structure
#include <iostream>
#include <string>
using namespace std;

// Define the structure for Student


struct Student {
int id;
string name;
int age;
float marks[5];
// Function to calculate average marks
float calculateAverage() {
float sum = 0;
for (int i = 0; i < 5; i++) {
sum += marks[i];
}
return sum / 5;
}

// Function to display student details


void displayDetails() {
cout << "\nStudent ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Marks: ";
for (int i = 0; i < 5; i++) {
cout << marks[i] << " ";
}
cout << "\nAverage Marks: " << calculateAverage() << endl;
}
};

int main() {
Student student;

// Accept input from the user


cout << "Enter Student ID: ";
cin >> student.id;

cin.ignore(); // To ignore newline after entering ID


cout << "Enter Student Name: ";
getline(cin, student.name);

cout << "Enter Student Age: ";


cin >> student.age;

cout << "Enter marks in 5 subjects:\n";


for (int i = 0; i < 5; i++) {
cout << "Subject " << i + 1 << ": ";
cin >> student.marks[i];
}

// Display Student details


student.displayDetails();

return 0;
}

You might also like