False Position and Newton Raphson Method
False Position and Newton Raphson Method
#include <iostream>
using namespace std;
const int P = 5; // Number of processes
const int R = 3; // Number of resources
int available[R];
int maxMatrix[P][R];
int allocation[P][R];
int need[P][R];
// Safety Algorithm
bool isSafeState() {
bool finish[P] = {false};
int work[R];
for (int i = 0; i < R; i++) work[i] = available[i];
int safeSequence[P];
int count = 0;
int main() {
cout << "Enter Allocation Matrix: \n";
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
cin >> allocation[i][j];
}
}
calculateNeed();
if (!isSafeState()) {
cout << "System is in an unsafe state!" << endl;
}
return 0;
}Output:
False Position Method Implementation in C++ (Equation: x^2 - x - 2 = 0)
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x1 = 1, x2 = 3;
cout << "Applying False Position Method...\n";
falsePosition(x1, x2);
return 0;
}
Output:
False Position Method Implementation in C++ (Equation: x^2 - x - 2 = 0)
#include <iostream>
#include <cmath>
using namespace std;
// Newton-Raphson Method
void newtonRaphson(double x) {
double h = f(x) / df(x);
while (fabs(h) >= EPSILON) {
h = f(x) / df(x);
x = x - h;
}
cout << "Root found by Newton-Raphson Method: " << x << endl;
}
int main() {
double x0 = 2.5;
cout << "Applying Newton-Raphson Method...\n";
newtonRaphson(x0);
return 0;
}
Output: