This code solves the program : Create an algorithm in the form of a flowchart, write and debug the task using recursive and ordinary functions. Corresponding results.
What is dot used for here "x = (1. / 2) * (f + (a / f));" ?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k, e, x, a, d, n, f, s;
d = 1;
do {
e = 1;
do {
cout << "Press 1 to use recurrent solution method, press 2 to use nun recurrent solution
method" << endl;
cin >> k;
if (k == 1 || k == 2) e = 2;
} while (e == 1);
switch (k)
{
case 1:
{
cout << "Enter the numder a:" << endl;
cin >> a;
cout << "Enter the numder n:" << endl;
cin >> n;
x = (1. / 2)*(1 + a);
f = x;
s = f;
for (int i = 1; i < n; i++)
{
x = (1. / 2) * (f + (a / f));
s += x;
f = x;
}
cout << "Result: " << s << endl;
}
break;
case 2:
{
cout << " Enter the numder a:" << endl;
cin >> a;
x = sqrt(a);
cout << "Result: " << x << endl;
}
break;
}
cout << "Press 1 to repeat!" << endl;
cin >> d;
} while (d == 1);
}
The dot in "x = (1. / 2) * (f + (a / f));" is used for assuring that the division is interpreted as a float division instead of an integer division, so the result of (1/2) is 0.5 instead of 0.
Related
i am not sure how to resolve this math problem. what should i recall and where did i miss something. i have tried different opportunities. i think i just call not existing index or something like that..
#include <iostream>
using namespace std;
double recur(int n, int x);
double x;
int number;
int main()
{
cout << "enter n: " ;
cin >> number;
cout << endl;
do
{
cout << "enter float x!=0: ";
cin >> x;
cout << endl;
} while (x==0);
cout << "recur(" << number << "," << x << ")=" << recur(number, x) << endl;
system("pause");
}
double recur(int n, int x)
{
if (n > 1) return (x * recur(n, x - n) * recur(n - 1, x));
else if( n == 1) return x * recur(n,x) - x;
else return 1;
}
Formula:
For formula:
It's implementation:
#include <iostream>
#include<cmath>
using namespace std;
double recur(int n, int x);
double x;
int number;
int main()
{
cout << "enter n: " ;
cin >> number;
cout << endl;
do
{
cout << "enter float x!=0: ";
cin >> x;
cout << endl;
} while (x==0);
cout << "recur(" << number << "," << x << ")=" << recur(number, x) << endl;
system("pause");
}
double recur(int n, int x)
{
if (n > 1) return (x*(pow(log(x),n)) - n*(recur(n-1,x)));
else if( n == 1) return x * log(x) - x;
}
For n>1 line
(x*(pow(log(x),n)) = x*(ln x)^n
n*(recur(n-1,x)) = n* integral( (lnx)^(n-1) ) <- In next recursion call one power will get reduced
For n=1 line
x * log(x) - x = xlnx - x <- base condition(where recursive call will stop)
In this implementation recur(n,x) denotes integration of (lnx)^n w.r.t x.
Your integral isn't the same as the log portion, so you should probably split that out.
double ln_n(int n, double x) {
while (n --> 0) x = log(x);
return x;
}
double integral(int n, double x) {
if (n > 0) return (x * ln_n(n, x)) - (n * integral(n - 1, x));
return x;
}
See it live
this is my code for RSA cryptosystem
but I have an error with using the pow function that git an error solution
c = pow(m,e)%n;
and for gcd (greatest common divisor) I'm using #include numeric and algorithm and it does not run
but I create function and will be git a solution
this is a full code
#include<iostream>
#include<cmath>
#include<math.h>
#include <algorithm>
#include<numeric>
using namespace std;
int prime(long int pr)
{
int i;
int j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
/*int power(int a, int b) {
float r = 1;
while (b != 0) {
r *= a;
--b;
}
return r;
}
*/
int main() {
int flag,w,k;
cout << "Welcome to RSA Project\n\n ";
cout << "Enter First larg prime for p ";
int p,q,phi,n,e,d;
cin >> p;
flag = prime(p);
if (flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
cout << "Enter Second larg prime for q ";
cin >> q;
flag = prime(q);
if (flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
//compute n= p*q
n = p * q;
// comput phi
phi = (p - 1) * (q - 1);
//chose e or select randomly e must be gcd(e,phi)=1
w = 2;
while (gcd(w, phi) != 1)
{
w++;
}
e = w;
//chose d from random numbers
k = 1;
while (((k * e) % phi) != 1)
{
k++;
}
d = k;
cout << "\nThe public key is : " << "{ " << n << " , " << e << " }";
cout << "\nThe private key is : " << "{ " << n << " , " << d << " }";
cout << "\n*****\n\n p=" << p << "\nq=" << q <<"\nn="<<n<<"\nphi = " << phi << "\ne = " << e << "\nd = " << d << "\n *****";
//Encryption
cout << " \nEnter the message for Encrypt : ";
int m , c;
cin >> m;
//c = pow(m,e)%n; ((( here an error )))
c = pow(m, e);
c = c% n;
cout << "\n message for encrypt : " << m;
cout << " \nmessage after encrypt : " << c;
//Decryption
m = pow(c , d);
m = c% n;
cout << " \nmessage after decrypt : " << m;
}
output :
output
it's fully tested but the error with pow function ..
if anyone can help me please ..
I have this math problem which requires to find if a point lies in all the circles... I have almost all the code but it has one problem that is it checks which point lies in each circle and not if one point lies in all circles.. I think I need to do a minor change but I can't figure it out...
#include<iostream>
#include<stdlib.h>
#include<math.h>
int main()
{
using namespace std;
system("chcp 1251");
int k, m, c, ic;
float x[12], y[12], r[12], xp[12], yp[12], d;
do
{
cout << "Enter how many circles (1..12): ";
cin >> k;
}
while (k < 1 || k > 12);
for (c = 0; c < k; c++)
{
cout << "Enter coordinates of circles № " << 1 + c << endl;
cout << "x= "; cin >> x[c];
cout << "y= "; cin >> y[c];
cout << "r= "; cin >> r[c];
}
do
{
cout << "Enter how many points (1..20): ";
cin >> m;
} while (m < 1 || m > 20);
cout << "Enter coordinates of points:" << endl;
for (c = 0; c < m; c++)
{
cout << "Point № " << 1 + c << endl;
cout << "x= "; cin >> xp[c];
cout << "y= "; cin >> yp[c];
}
for (c = 0; c < k; c++)
{
cout << "Points in circle № " << 1 + c << ": ";
for (ic = 0; ic < m; ic++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
if (d < r[c] )
cout << 1 + ic << " ";
}
cout << endl;
}
system("pause");
return 0;
}
I think I just need to change the if.. so if anyone can help me I would appreciate.
after all if you allow to enter 20 point your arrays xp and yp should have a size of 20, another advice if it's not a school work is using the structers, for solving your problem this is the code for fix it
bool b;
for (ic = 0; ic < m; ic++)
{
b=true;
for (c = 0; c < k; c++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
b=b&&(d < r[c]);
}
if(b)
cout << "This point lies "<< 1 + ic << " ";
cout << endl;
}
Testing one point against many circles:
bool inCircle=true;
for (c = 0; c < k; c++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
if (d > r[c])
{
inCircle = false;
cout << "the point is outside circle " << c << endl;
break; // this is not strictly needed
}
}
if(inCircle)
cout << "the point is inside all of the circles" << endl;
Once you have this working perfectly, you can put a loop around it to test many points.
#include <iostream>
#include <iomanip>
using namespace std;
//Power function
float power (float base, int exp)
{
if (exp < 0)
{
if (base == 0)
{
cout << "Base cannot be 0.";
return -1;
}
}
if (exp == 0)
return 1;
if (exp == 1)
return base;
return base * power (base, exp - 1);
}
//Factorial function
int facto (int n)
{
return n <= 0 ? 1 : n * facto (n - 1);
}
//Cos function
float cosCalc (float rad)
{
float cos = 0;
int x;
for (x = 0; x < 10; x++)
{
cos += power (-1, x) * power (rad, 2 * x) / facto (2 * x);
}
return cos;
}
//Sin function
float sinCalc (float rad)
{
float sin = 0;
int x;
for (x = 0; x < 10; x++)
{
sin += power (-1, x) * power (rad, 2 * x + 1) / facto (2 * x + 1);
}
return sin;
}
//Main function
int main()
{
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << "Select:";
cout << endl << "1. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;
if (choice == 1)
{
int angle, anglePh;
float rad;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
float cos = 0;
float sin = 0;
cout << endl << "Please enter an angle. => ";
cin >> angle;
anglePh = angle;
angle %= 360;
rad = angle * pi / 180;
cout << anglePh << " degrees = " << rad << " radian";
cout << endl << "Calculating Cos...";
cosCalc (rad);
cout << endl << "Cos = " << fixed << cos;
cout << endl << "Calculating Sin...";
sinCalc (rad);
cout << endl << "Sin = " << fixed << sin;
}
if (choice == 9)
{
break;
}
}
}
I am building a program that calculates Sin and Cos off an angle input, and when I run it, it outputs 0.000000 for both Sin and Cos. I suspect there is something to do with me declaring float cos = 0 and float sin = 0 in the if loop for choice == 1, and I tried messing around with it but it either results in the program straight out giving me errors on launch, or I get the same outputs.
Any idea where I went wrong?
Thanks for your insight in advance, cheers!
Your cosin and sine function return a float, but in order to get that result you still have to store it in a variable.
So instead of:
cosCalc (rad);
Do:
rad = cosCalc (rad);
and the same for your sine function.
I'm making a program that factors functions (f(x), not fully factored though):
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
int x3;
int x2;
int x;
int remain;
int r = 0;
int factor;
int main() {
int b, i, j = 0;
int factors[101];
cout << "f(x) = x^3 + x^2 + x + r (Factor tool)" << endl;
cout << "x^3?: ";
cin >> x3;
cout << "x^2: ";
cin >> x2;
cout << "x: ";
cin >> x;
printf("remain (Y intercept): ");
scanf("%d", &b);
cout << "f(x) = " << x3 << "x^3 + " << x2 << "x^2 + " << x << "x + " << b
<< "" << endl;
cout << "factors of remainder are: " << endl;
for (i = 1; i <= b; i++) {
if (b % i == 0) {
factors[j++] = i;
printf("%d\t", i);
}
}
getchar();
while (true) {
int good;
if (factors[1] == 0) {
cout <<endl;
cout << "Equation Cannot be factored";
break;
}
int factorv = factors[r];
int nx1 = x3 * factors[r];
int nx2 = (nx1 + x2);
int nx3 = x + (nx2 * factors[r]);
int nx4 = remain + (nx3 * factors[r]);
if (nx4 == 0) {
int factored = (0 - factors[r]);
cout <<endl;
cout << "The Factored Function: f(x) = "
<< "(x " << factored << ")(" << nx1 << "x^3 + " << nx2 << "x^2 + "
<< nx3 << "x"
<< ")"
<< "";
break;
} else {
r = r + 1;
}
}
}
but in this part of the code, it shows as (x 0)(0x^3 + (x3 input instead of calculated nx1)x^2 + (x2 input instead of calculated nx2)x).
if (nx4 == 0) {
int factored = (0-factors[r]);
cout<<"The Factored Function: f(x) = "<<"(x "<<factored<<")("<<nx1<<"x^3 + "<<nx2<<"x^2 + "<<nx3<<"x"<<")"<<"";
break;
What happen to my nx variables? Why is it coming up incorrect or as a 0 when it was calculated properly above?
You have some of your variables twice:
int nx1;
int nx2;
int nx3;
int nx4;
They exists as global variables and again in the scope of main. They have the same name but are different variables.
Take the lesson: Global variables are no good.
Moreover you have a logic error in your code. When I add a std::cout << r << std::endl; in the last while loop I see its value increasing until there is a segfault, because factors[r] is out-of-bounds. broken code # wandbox
I cannot really tell you how to fix it, because I would have to dive into the maths first. I can only suggest you to never use infinte loops in numerical codes without an "emergency exit". What i mean is that unless fully tested, you cannot be sure that the loop will end at some point and when it doesn't typically the consequences are bad and difficult to diagnose. Always make sure the loop will end at some point:
int max_iteratons = 100;
int counter;
while (counter < max_iteratons) {
// do something
++counter;
}
if (counter == max_iterations) std::cout << "i have a bug :(";