C++ Assignment 1001
C++ Assignment 1001
1
Course Code: Math-310 Course Title: Math Lab III
Basic Problems
1. Sum of Two Numbers: Write a program that takes two integers and
prints
their sum.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "Sum: " << a + b << endl;
return 0;
}
Output:
Enter two integers: 5 3
Sum: 8
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "Difference: " << a - b << endl;
return 0;
}
2
Course Code: Math-310 Course Title: Math Lab III
Output:
Enter two integers: 8 3
Difference: 5
3. Product of Two Numbers: Write a program that takes two integers and
prints their product.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "Product: " << a * b << endl;
return 0;
}
Output:
Enter two integers: 4 5
Product: 20
4. Division of Two Numbers: Write a program that takes two integers and
prints their division (check for division by zero).
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
if (b == 0)
cout << "Error: Division by zero is not allowed." << endl;
else
cout << "Division: " << static_cast<double>(a) / b << endl;
return 0;
}
3
Course Code: Math-310 Course Title: Math Lab III
Output 1:
Enter two integers: 10 2
Division: 5
Output 2:
Enter two integers: 5 0
Error: Division by zero is not allowed.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
if (b == 0)
cout << "Error: Modulus by zero is undefined." << endl;
else
cout << "Modulus: " << a % b << endl;
return 0;
}
Output 1:
Enter two integers: 10 3
Modulus: 1
Output 2:
Enter two integers: 5 0
Error: Modulus by zero is undefined.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Square: " << num * num << endl;
return 0;
4
Course Code: Math-310 Course Title: Math Lab III
Output:
Enter a number: 4
Square: 16
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Cube: " << num * num * num << endl;
return 0;
}
Output:
Enter a number: 3
Cube: 27
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << "Positive" << endl;
else if (num < 0)
cout << "Negative" << endl;
else
cout << "Zero" << endl;
return 0;
}
5
Course Code: Math-310 Course Title: Math Lab III
Output 1:
Enter a number: 5
Positive
Output 2:
Enter a number: -3
Negative
Output 3:
Enter a number: 0
Zero
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << (num % 2 == 0 ? "Even" : "Odd") << endl;
return 0;
}
Output 1:
Enter a number: 4
Even
Output 2:
Enter a number: 7
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
cout << "Absolute value: " << abs(num) << endl;
return 0;
6
Course Code: Math-310 Course Title: Math Lab III
Output 1:
Enter an integer: -5
Absolute value: 5
Output 2:
Enter an integer: 3
Absolute value: 3
7
Course Code: Math-310 Course Title: Math Lab III
8
Course Code: Math-310 Course Title: Math Lab III
Object-oriented Programming
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
BankAccount(double initial_balance = 1000) {
balance = initial_balance;
}
int main() {
BankAccount account;
account.deposit(500);
account.withdraw(200);
cout << "Final balance: " << account.check_balance() << endl;
return 0;
9
Course Code: Math-310 Course Title: Math Lab III
}
Output:
Final Balance: 1300
2. Write a program that defines a Student class with name and ID. Create a
student object with your name and ID, and display the student details.
#include <iostream>
using namespace std;
class Student {
private:
string name;
int id;
public:
Student(string studentName, int studentID) {
name = studentName;
id = studentID;
}
int main() {
Student student("Khan Mohaimenul Islam", 20203111);
student.displayDetails();
return 0;
}
Output:
Student Name: Khan Mohaimenul Islam
Student ID: 20203111
3. Design a class that stores course grades and credit hours. Implement a
method to calculate GPA using the formula:
10
Course Code: Math-310 Course Title: Math Lab III
(grade×credit)
GPA=
credit
Add two courses with grades and credits and print the GPA.
#include <iostream>
#include <vector>
using namespace std;
class Course {
private:
vector<double> grades;
vector<int> credits;
public:
void addCourse(double grade, int credit) {
grades.push_back(grade);
credits.push_back(credit);
}
if (totalCredits == 0)
return 0;
11
Course Code: Math-310 Course Title: Math Lab III
int main() {
Course studentCourses;
studentCourses.addCourse(3.7, 3); // Grade A- (3 credit hours)
studentCourses.addCourse(3.0, 4); // Grade B (4 credit hours)
return 0;
}
Output:
GPA: 3.32857
#include <iostream>
using namespace std;
class Book {
private:
string title;
bool isCheckedOut;
public:
Book(string bookTitle) {
title = bookTitle;
isCheckedOut = false;
}
void checkOut() {
if (!isCheckedOut) {
isCheckedOut = true;
cout << title << " has been checked out." << endl;
} else {
cout << title << " is already checked out." << endl;
12
Course Code: Math-310 Course Title: Math Lab III
}
}
int main() {
Book book1("1984");
Book book2("The Alchemist");
Book book3("To Kill a Mockingbird");
return 0;
}
Output:
1984 is Available
The Alchemist is Available
To Kill a Mockingbird is Available
13
Course Code: Math-310 Course Title: Math Lab III
1984 is Available
The Alchemist is Checked Out
To Kill a Mockingbird is Available
#include <iostream>
using namespace std;
class Calculator {
public:
double add(double a, double b) {
return a + b;
}
int main() {
Calculator calc;
double a = 5, b = 3;
14
Course Code: Math-310 Course Title: Math Lab III
return 0;
}
Output:
Addition: 8
Subtraction: 2
Multiplication: 15
Division: 1.66667
6. Create a Ticket class for events. Reserve a ticket and display whether it
is reserved or available.
#include <iostream>
using namespace std;
class Ticket {
private:
bool isReserved;
public:
Ticket() {
isReserved = false;
}
void reserve() {
if (!isReserved) {
isReserved = true;
cout << "Ticket reserved successfully." << endl;
} else {
15
Course Code: Math-310 Course Title: Math Lab III
int main() {
Ticket eventTicket;
return 0;
}
Output:
Ticket is Available
Ticket reserved successfully.
Ticket is Reserved
7. Write a class that calculates the salary of an employee given the hourly
rate and hours worked. Test with an employee working 40 hours at
₹20.5/hour.
#include <iostream>
using namespace std;
class Employee {
private:
double hourlyRate;
double hoursWorked;
16
Course Code: Math-310 Course Title: Math Lab III
public:
Employee(double rate, double hours) {
hourlyRate = rate;
hoursWorked = hours;
}
int main() {
Employee emp(20.5, 40); // ₹20.5/hour, 40 hours
cout << "Employee Salary: ₹" << emp.calculateSalary() << endl;
return 0;
}
Output:
Employee Salary: ₹820
#include <iostream>
#include <vector>
using namespace std;
class Product {
private:
string name;
double price;
public:
Product(string n, double p) {
name = n;
price = p;
17
Course Code: Math-310 Course Title: Math Lab III
class ShoppingCart {
private:
vector<Product> items;
public:
void addProduct(const Product& product) {
items.push_back(product);
cout << product.getName() << " added to cart." << endl;
}
ShoppingCart cart;
cart.addProduct(p1);
cart.addProduct(p2);
cart.addProduct(p3);
return 0;
18
Course Code: Math-310 Course Title: Math Lab III
Output:
Book added to cart.
Pen added to cart.
Notebook added to cart.
Total Cost: ₹330
9. Create a class to track hours spent on a project. Add hours and display
total time spent.
#include <iostream>
using namespace std;
class Project {
private:
double totalHours;
public:
Project() {
totalHours = 0;
}
int main() {
Project myProject;
myProject.addHours(5);
myProject.addHours(3.5);
19
Course Code: Math-310 Course Title: Math Lab III
myProject.addHours(2);
return 0;
}
Output:
Total hours spent on project: 10.5 hours
10. Create an Item class with stock quantity. Implement methods to sell
items and add stock. Test the program with an example item.
#include <iostream>
using namespace std;
class Item {
private:
string name;
int stock;
public:
Item(string itemName, int initialStock) {
name = itemName;
stock = initialStock;
}
20
Course Code: Math-310 Course Title: Math Lab III
int main() {
Item item("Pen", 50); // Example item with 50 in stock
item.displayStock();
return 0;
}
Output:
Current stock of Pen: 50
Current stock of Pen: 70
30 Pen(s) sold.
Current stock of Pen: 40
Not enough stock to sell.
Current stock of Pen: 40
21
Course Code: Math-310 Course Title: Math Lab III
22
Course Code: Math-310 Course Title: Math Lab III
const int N = 3;
// Back-substitution
float x[N];
for (int i = N - 1; i >= 0; i--) {
x[i] = a[i][N];
for (int j = i + 1; j < N; j++) {
x[i] -= a[i][j] * x[j];
23
Course Code: Math-310 Course Title: Math Lab III
}
x[i] /= a[i][i];
}
int main() {
float a[N][N+1] = {
{3, 2, -1, 1},
{2, -2, 4, -2},
{-1, 0.5, -1, 0}
};
gaussElimination(a);
return 0;
}
Output:
x1 = 1
x2 = -2
x3 = -2
#include <iostream>
#include <iomanip>
using namespace std;
24
Course Code: Math-310 Course Title: Math Lab III
mat[i][j] /= diag;
}
// Print result
cout << fixed << setprecision(2);
for (int i = 0; i < N; i++) {
cout << "Variable " << (char)('x' + i) << " = " << mat[i][N] << endl;
}
}
int main() {
// Augmented matrix for:
// 3x + 2y - z = 1
// 2x - 2y + 4z = -2
// -x + 0.5y - z = 0
float matrix[N][N + 1] = {
{3, 2, -1, 1},
{2, -2, 4, -2},
{-1, 0.5, -1, 0}
}
gaussJordan(matrix);
return 0;
}
Output:
Variable x = 1.00
Variable y = -2.00
Variable z = -2.00
25
Course Code: Math-310 Course Title: Math Lab III
3
3. Find a root of the equation f(x)= x - 5x + 1 using the Newton-Raphson
Method (initial guess: x = 1).
0
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double x0 = 1.0; // Initial guess
double x1;
int max_iter = 10;
double tol = 1e-6;
x1 = x0 - fx / fpx;
cout << "Iteration " << i << ": x = " << x1 << endl;
26
Course Code: Math-310 Course Title: Math Lab III
x0 = x1;
}
x
4. (Regula Falsi Method) Find a root of f(x)= e - 3x using the Regula Falsi
(False Position) Method.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(double x) {
return exp(x) - 3 * x;
}
int main() {
double a = 0, b = 1, c;
double fa = f(a), fb = f(b), fc;
double tol = 1e-6;
int max_iter = 20;
if (fa * fb >= 0) {
27
Course Code: Math-310 Course Title: Math Lab III
cout << "Invalid initial guesses. f(a) and f(b) must have opposite signs." <<
endl;
return -1;
}
cout << "Iteration " << i << ": c = " << c << ", f(c) = " << fc << endl;
Output:
Iteration 1: c = 0.778112, f(c) = 0.099866
Iteration 2: c = 0.802894, f(c) = 0.004527
Iteration 3: c = 0.804068, f(c) = 0.000203
Iteration 4: c = 0.804120, f(c) = 0.000009
3
5. Implement the Bisection Method to find a root of f(x)= x + x - 1 in the
interval [0,1] with an accuracy of 0.0001 .
28
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(double x) {
return pow(x, 3) + x - 1;
}
int main() {
double a = 0, b = 1, c;
double tol = 0.0001;
int max_iter = 100;
cout << "Iteration " << i << ": c = " << c << ", f(c) = " << fc << endl;
if (f(a) * fc < 0) {
b = c;
} else {
a = c;
}
}
29
Course Code: Math-310 Course Title: Math Lab III
Output:
Iteration 1: c = 0.500000, f(c) = -0.375
Iteration 2: c = 0.750000, f(c) = 0.578125
Iteration 3: c = 0.625000, f(c) = 0.121094
...
Iteration 12: c = 0.682617, f(c) = -0.000101
30
Course Code: Math-310 Course Title: Math Lab III
31
Course Code: Math-310 Course Title: Math Lab III
2 -x
6. Compute the first derivative of f(x)= x e at x = 2 using Newton’s
Forward Difference Method with h = 0.1.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double h = 0.1;
double x0 = 2.0;
return 0;
}
Output:
Approximate f'(2.0) = -0.308270
32
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(double x) {
return sin(x) + x * x;
}
int main() {
double h = 0.1;
double x0 = 3.0;
#include <iostream>
#include <cmath>
#include <iomanip>
33
Course Code: Math-310 Course Title: Math Lab III
double f(double x) {
return log(x); // ln(x)
}
int main() {
double h = 0.1;
double x0 = 2.0;
Output:
Approximate f''(2.0) = -0.500000
34
Course Code: Math-310 Course Title: Math Lab III
35
Course Code: Math-310 Course Title: Math Lab III
Part C: Interpolation
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Given data points
double x[] = {1, 2, 3, 4, 5};
double f_x[] = {2, 8, 18, 32, 50}; // f(x) values
int n = 5; // Number of data points
double h = 1.0; // Step size
36
Course Code: Math-310 Course Title: Math Lab III
return 0;
}
Output:
f(2.5) = 10.500000
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Given data points
double x[] = {1, 2, 3, 4, 5};
double f_x[] = {2, 8, 18, 32, 50}; // f(x) values
int n = 5; // Number of data points
double h = 1.0; // Step size (as x-values are 1, 2, 3, 4, 5)
37
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <iomanip>
using namespace std;
38
Course Code: Math-310 Course Title: Math Lab III
int main() {
// Given data points
double x[] = {1, 3, 4, 6};
double f_x[] = {2, 6, 12, 24}; // f(x) values
39
Course Code: Math-310 Course Title: Math Lab III
40
Course Code: Math-310 Course Title: Math Lab III
41
Course Code: Math-310 Course Title: Math Lab III
5
12. Compute the integral: (x²+2x+1)dx using the Trapezoidal Rule with
n = 4.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 1, b = 5; // Interval [a, b] = [1, 5]
int n = 4; // Number of subintervals
double h = (b - a) / n; // Step size
// Final result
double result = (h / 2) * sum;
cout << "Approximate integral using the Trapezoidal Rule: " << result << endl;
return 0;
}
Output:
Approximate integral using the Trapezoidal Rule: 56
42
Course Code: Math-310 Course Title: Math Lab III
13. Compute the integral: sinx dx using Simpson’s 1/3 Rule with n = 6.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 0, b = M_PI; // Interval [a, b] = [0, pi]
int n = 6; // Number of subintervals (n must be even)
// Step size h
double h = (b - a) / n;
// Final result
double result = (h / 3) * sum;
cout << "Approximate integral using Simpson's 1/3 rule: " << result << endl;
return 0;
}
Output:
43
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 1, b = 4; // Interval [a, b] = [1, 4]
int n = 6; // Number of subintervals (n must be multiple of 3)
double h = (b - a) / n; // Step size
// Final result
double result = (3 * h / 8) * sum;
cout << "Approximate integral using Simpson's 3/8 rule: " << result << endl;
return 0;
}
44
Course Code: Math-310 Course Title: Math Lab III
Output:
Approximate integral using Simpson's 3/8 rule: 58.3053
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 2, b = 8; // Interval [a, b] = [2, 8]
int n = 6; // Number of subintervals (n must be 6 for Weddle's Rule)
double h = (b - a) / n; // Step size
// Final result
double result = (3 * h / 10) * sum;
cout << "Approximate integral using Weddle's Rule: " << result << endl;
45
Course Code: Math-310 Course Title: Math Lab III
return 0;
}
Output:
Approximate integral using Weddle's Rule: 194.666666
46
Course Code: Math-310 Course Title: Math Lab III
47
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Function f(x, y) = x + y
double f(double x, double y) {
return x + y;
}
48
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <iomanip>
using namespace std;
// f(x, y) = x + y
double f(double x, double y) {
return x + y;
}
int main() {
double x0 = 0.0, y0 = 1.0; // initial condition
double h = 0.2; // step size
int n = 5; // number of steps (from 0 to 1 with h=0.2)
49
Course Code: Math-310 Course Title: Math Lab III
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x = 0.0, y = 1.0; // Initial condition
double h = 0.5; // Step size
int n = 4; // Number of steps (0 to 2 with h = 0.5)
50
Course Code: Math-310 Course Title: Math Lab III
51
Course Code: Math-310 Course Title: Math Lab III
52
Course Code: Math-310 Course Title: Math Lab III
19. (Heat Transfer Problem) A thin metal rod has a temperature distribution
-0.1x
given by: T(x)=100 e cos2x. Compute dT at x = 2 using Newton’s
dx
Solution:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double x = 2.0;
double h = 0.1;
Output:
Approximate dT at x = 2 is: -124.188284
dx
53
Course Code: Math-310 Course Title: Math Lab III
Load). Use Simpson’s 1/3 Rule to compute the total deflection area from x
= 0 to x = L using n = 8.
Solution:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Constants
const double L = 8.0;
const double E = 200e9;
const double I = 4e-6;
const double w = 1500;
int main() {
int n = 8; // Must be even
double a = 0.0;
double b = L;
double h = (b - a) / n;
area *= h / 3.0;
54
Course Code: Math-310 Course Title: Math Lab III
return 0;
}
Output:
2
Total deflection area under the beam: 0.00000384 m
21. (Projectile Motion) Given the initial velocity and angle of a projectile,
use Euler’s Method to approximate the position and velocity of the
projectile at time intervals. Initial velocity: 50m/s, Launch angle: 45
degrees, Acceleration due to gravity: 9.81 m/s², Time step: 0.1s. Find the
maximum height and range of the projectile.
int main() {
// Constants
double v0 = 50.0;
double angle_deg = 45.0;
double g = 9.81;
double h = 0.1;
// Initial position
double x = 0.0, y = 0.0;
double maxHeight = 0.0;
double time = 0.0;
55
Course Code: Math-310 Course Title: Math Lab III
time += h;
}
cout << "\nMaximum Height: " << maxHeight << " m\n";
cout << "Range: " << x << " m\n";
return 0;
}
Output:
Time(s) X(m) Y(m) Vx(m/s) Vy(m/s)
0.0000 0.0000 0.0000 35.3553
35.3553
0.1000 3.5355 3.5355 35.3553
34.3743
0.2000 7.0711 6.9729 35.3553
33.3933
...
3.5000 123.7437 1.5145 35.3553 -33.9993
3.6000 127.2792 -1.8854 35.3553 -34.9803
56
Course Code: Math-310 Course Title: Math Lab III
time t, r=0.1 is the growth rate, K = 500 is the carrying capacity, Initial
population P(0) = 50. Use Runge-Kutta (RK4) method to approximate the
population for 50 time steps.
Solution:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Constants
double r = 0.1; // growth rate
double K = 500; // carrying capacity
double P0 = 50; // initial population
double h = 1.0; // time step size
int steps = 50; // number of time steps
// Initial conditions
double P = P0;
double t = 0.0;
57
Course Code: Math-310 Course Title: Math Lab III
t += h;
}
return 0;
}
Output:Time(t)Population(P)
0.0000 50.0000
1.0000 51.5202
2.0000 53.0786
3.0000 54.6753
4.0000 56.3102
...
46.0000 499.9492
47.0000 499.9504
48.0000 499.9516
49.0000 499.9528
50.0000 499.9540
23. (Thermal Conduction in a Rod) Solve the heat equation for a rod with
2
thermal conductivity k, length L, and initial temperature distribution d T =
2
dx
solve for the temperature distribution after 50 seconds for a rod with L = 1
meter.
Solution:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main() {
// Constants
58
Course Code: Math-310 Course Title: Math Lab III
// Discretization parameters
double dx = 0.05; // Space step (meters)
double dt = 0.01; // Time step (seconds)
int N = L / dx; // Number of spatial grid points
int M = 50 / dt; // Number of time steps (for 50 seconds)
// Time-stepping loop
for (int n = 0; n < M; ++n) {
// Copy the current temperature to the next
for (int i = 1; i < N; ++i) {
// Apply the finite difference formula
T_next[i] = T_current[i] + r * (T_current[i - 1] - 2 * T_current[i] + T_current[i
+ 1]);
}
// Boundary conditions
T_next[0] = T_boundary;
T_next[N] = T_boundary;
T_current = T_next; // Update the current temperature distribution
59
Course Code: Math-310 Course Title: Math Lab III
Solution:
#include <iostream>
#include <vector>
#include <cmath>
60
Course Code: Math-310 Course Title: Math Lab III
#include <iomanip>
using namespace std;
// Parameters
const double L = 100.0; // Length of the bridge (m)
const double E = 2e11; // Young's Modulus (Pa)
const double I = 1e-4; // Moment of Inertia (m^4)
const double f = 1000.0; // Load (N)
const int n = 10; // Number of elements (higher for more accuracy)
61
Course Code: Math-310 Course Title: Math Lab III
return displacement;
}
int main() {
// Solve for displacement
vector<double> displacement = solve_FEM();
25. (Economics - Supply and Demand) Given the supply and demand
functions: S(p) = 100 + 5p, D(p) = 200 - 10p. Find the equilibrium price p
62
Course Code: Math-310 Course Title: Math Lab III
Solution:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
// Initial guess for the equilibrium price
double p0 = 10.0;
double tolerance = 1e-6; // Set the tolerance for convergence
double p = p0;
int max_iterations = 100;
// Iteration loop
for (int i = 0; i < max_iterations; ++i) {
// Apply the Newton-Raphson formula
double p_next = p - f(p) / f_prime(p);
p = p_next;
}
63
Course Code: Math-310 Course Title: Math Lab III
return 0;
}
Output:
Equilibrium price p = 6.666667
int main() {
// Parameters
double k = 0.1; // Rate constant (mol/L·s)
double A0 = 1.0; // Initial concentration (mol/L)
double h = 0.1; // Time step (seconds)
double t_max = 10.0; // Maximum time (seconds)
// Number of steps
int num_steps = t_max / h;
// Initial concentration
double A = A0;
double t = 0.0;
64
Course Code: Math-310 Course Title: Math Lab III
return 0;
}
Output:
Time (s) [A] (mol/L)
0.0000 1.0000
0.1000 0.9900
0.2000 0.9801
0.3000 0.9703
0.4000 0.9606
0.5000 0.9510
0.6000 0.9415
0.7000 0.9321
0.8000 0.9228
0.9000 0.9136
1.0000 0.9045
...
9.9000 0.4066
10.0000 0.4012
65
Course Code: Math-310 Course Title: Math Lab III
Solution:
#include <iostream>
#include <cmath>
using namespace std;
return (h / 3) * sum;
}
int main() {
double P = 1000.0;
double r = 0.05;
double n = 12.0;
double t0 = 0.0;
double t1 = 10.0;
int intervals = 100;
66
Course Code: Math-310 Course Title: Math Lab III
cout << "Approximate Future Value using Simpson's Rule: $" << approxFV <<
endl;
cout << "Exact Future Value using formula: $" << exactFV << endl;
return 0;
}
Output:
Approximate Future Value using Simpson's Rule: $1643.62
Exact Future Value using formula: $1647.01
28. (Fluid Flow in a Pipe) Solve the equation for fluid flow in a pipe using the
Darcy-Weisbach Equation : f = 8 * L * V / (π * d⁴) Where: f is the Darcy
friction factor, L is the pipe length, V is the flow velocity, d is the pipe
diameter. Use Gauss-Seidel Iteration to solve for f given L = 50 m, V = 5
m/s, and d = 0.2 m.
Solution:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double L = 50.0;
double V = 5.0;
double d = 0.2;
double f_old = 0.0, f_new = 0.0;
int max_iter = 100;
double tol = 1e-6;
int iter = 0;
do {
67
Course Code: Math-310 Course Title: Math Lab III
f_old = f_new;
f_new = computeF(L, V, d);
iter++;
} while (fabs(f_new - f_old) > tol && iter < max_iter);
cout << "Darcy friction factor f after " << iter << " iterations: " << f_new << endl;
return 0;
}
Output:
Darcy friction factor f after 1 iterations: 39810.71706
Solution:
#include <iostream>
#include <cmath>
using namespace std;
// Trapezoidal Rule
double trapezoidalRule(double P, double E, double I, double a, double b, int n) {
double h = (b - a) / n;
double sum = f(a, P, E, I) + f(b, P, E, I);
68
Course Code: Math-310 Course Title: Math Lab III
}
int main() {
double P = 1000.0; // N
double L = 10.0; // m
double E = 200e9; // Pa
double I = 1e-6; // m^4
int n = 100; // intervals for Trapezoidal Rule
cout << "Approximated Deflection using Trapezoidal Rule: " << approx_delta
<< " meters" << endl;
cout << "Exact Deflection using formula: " << exact_delta << "
meters" << endl;
return 0;
}
Output:
Approximated Deflection using Trapezoidal Rule: 0.0104167 meters
Exact Deflection using formula: 0.0104167 meters
Solution:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
69
Course Code: Math-310 Course Title: Math Lab III
return B;
}
int main() {
double I = 10.0; // current in Amperes
double r_start = 0.01; // starting distance in meters (to avoid division by 0)
double r_end = 0.1; // ending distance in meters
double h = 0.01; // step size
int steps = (r_end - r_start) / h;
return 0;
}
70
Course Code: Math-310 Course Title: Math Lab III
Output:
r (m) B (T) using RK4 B (T) exact
0.01 0.00019947 0.00019947
0.02 0.00009974 0.00009974
0.03 0.00006649 0.00006649
.... .... ....
71