Which algorithm should I use to calculate the given math sequence? - c++

The user must enter a number n (1; infinity). Then the program does this:
cos1/sin1 * (cos1+cos2)/(sin1+sin2) * … * (cos1+cos2+...+cos⁡ n)/(sin1+sin2+...+sin ⁡n )
I tried to calculate that:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
double res;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++)
{
res = cos(i) / sin(i);
}
cout << res;
}
But I don't know which algorithm would do it correctly.

Since you need to divide the two sums in each step, you need to store those sums, and multiply an accumulating product with the result of dividing them.
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
double cos_sum = 0.0;
double sin_sum = 0.0;
double res = 1.0;
for (int i = 1; i <= n; i++)
{
cos_sum += cos(i);
sin_sum += sin(i);
res *= cos_sum / sin_sum;
}
cout << res;
}

There are basically three quantities that accumulate during the iteration. They are:
C[N] = cos1+cos2+cos3+cos3+ .... cosN
S[N] = sin1+sin2+sin3+sin3+ .... sinN
X[N] = C[1] / S[1] * C[2] / S[2] * ... C[N] / S[N]
The recursive relations are:
C[0] = 0
C[N + 1] = C[N] + cosN+1
S[0] = 0
S[N + 1] = S[N] + sinN+1
X[0] = 1
X[N + 1] = X[N] * C[N+1] / S[N+1]
Using this it should be straightforward to write the loop:
#include <iostream>
int main() {
int n;
double X = 1;
double C = 0;
double S = 0;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++) {
C += cos(i);
S += sin(i);
X *= C / S;
}
cout << X;
}

Related

In Cpp for is not working properly from the function

I need to do this exercise, with struct and include the function in the struct to do the calculations, but for some reason, it's not working.
I recommended you to check the image for a better idea.
#include <iostream>
using namespace std;
struct Alfa{
double h,x,n,a;
void shuma(){
cout << "enter n: "; cin >> n;
cout << "enter a: "; cin >> a;
for (int i = 1; i >= n; i++){
x = 2 * i + a;
}
};
};
int main() {
Alfa alf;
alf.shuma();
alf.h = (alf.x / 2) + 3;
cout << alf.h;
return 0;
}
You are not following the formula in the image. Use this instead:
double sum = 0;
for (int i = 1; i <= n + 1; i++) {
if (i != 4) {
sum += 2 * i + a;
}
}
h = x / 2 + 3 * sum;
for (int i = 1; i >= n; i++) means i initiated to 1, while i>=n do the loop, then inc i. So when n is bigger then 1, it will never enter the loop. Maybe you want for (int i = 1; i <= n+1; i++) ?

Need to find 3 points that make up the largest perimeter

I have a structure called point. Obviously, it provides coordinates of a point( x and y). I have an array of points.
What I need to do is find 3 points that make up the largest triangle. I've tried a lot of options and I didn't make it.
Can you think of an algorithm that would create the outcome I need?
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
struct point {
double x, y;
};
int main()
{
double n, c1, c2;
double max(0), max1(0), max2(0), temp;
point *a = NULL;
cout << "Enter the number of points: ";
cin >> n;
a = new point[n];
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a[i] = dot;
};
for (int i = 0; i < n-1; i++) { // here I'm running out of ideas
for (int j = 1; j < n; j++) {
temp = sqrt((a[i].x + a[i].y)*(a[i].x + a[i].y)- (a[j].x + a[j].y)*(a[j].x + a[j].y));
if (temp > max)
}
}
You can just iterate over all sets of three points. Note that it is better to use a vector instead of an array and it is useful to put the code to calculate the perimeter in a separate function
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct point {
double x, y;
};
double perim(point p1, point p2, point p3)
{
double result = 0;
result += sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
result += sqrt(pow(p2.x - p3.x, 2) + pow(p2.y - p3.y, 2));
result += sqrt(pow(p3.x - p1.x, 2) + pow(p3.y - p1.y, 2));
return result;
}
int main()
{
double n, c1, c2;
double max(0), temp;
int p1 = 0, p2 = 0, p3 = 0;
vector <point> a;
cout << "Enter the number of points: ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a.push_back(dot);
};
for (int i = 0; i < n - 2; i++) { // here I'm running out of ideas
for (int j = i+1; j < n - 1; j++) {
for (int k = j+1; k < n; k++) {
temp = perim(a[i], a[j], a[k]);
if (temp > max) {
max = temp;
p1 = i; p2 = j; p3 = k;
}
}
}
}
cout << p1 << " "<<p2<< " "<<p3;
}

Pi Approximation using Leibniz formula

I can't seem to find what is wrong with my code.
It should not give me any negative answers as long as I type in any integer higher than 0.
#include <iostream>
#include <math.h>
using namespace std;
int k;
double getPi(int = k) {
double num1, num2=0;
const int p = 4;
for(int i = 1; k >= i; i ++) {
num1 = pow(-1, k + 1)/(2 * k - 1);
num2 = num2 + num1;
}
return num2 * p;
}
int main() {
cout << "Iterations: ";
cin >> k;
cout << "Pi is approximated to be " << getPi(k);
}
Use i instead of k in below line
num1 = pow(-1, k + 1)/(2 * k - 1);

Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted

this error keeps coming up yet i dont see a problem with the code (its in c++)
the program is supposed to find the inverse of a 2x2 matrix
#include <iostream>
using namespace std;
int main() {
float d;
float A[2][2], B[2][2];
do {
cout << "please enter valid parameters in for 11,12,21,22" << endl;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++)
cin >> A[i][j];
}
d = (A[1][1] * A[2][2]) - (A[1][2] * A[2][1]);
} while(d == 0);
B[1][1] = A[2][2] * (1.0 / d);
B[1][2] = A[1][2] * (-1.0 / d);
B[2][1] = A[2][1] * (-1.0 / d);
B[2][2] = A[1][1] * (1.0 / d);
for(int k = 0; k < 2; k++) {
for(int h = 0; h < 2; h++)
cout << B[k][h] << " ";
cout << endl;
}
return 0;
}
You are indexing B and A from 1 to 2 , instead use it from 0 to 1.

Series converging to the number e in C++

How to calculate serie 1 + 1/1! + 1/2! + 1/3! +...+1/n! in C++?
I have an outline:
#include <iostream>
using namespace std;
int main()
{
int n, i, j, fat;
float soma = 0.0;
cin >> n;
for (i = 1; i <= n; i++)
{
fat = 1;
soma += 1 / fat;
for (j = 1; j <= n; j++)
{
fat *= j;
}
}
cout << soma << endl;
return 0;
}
Keep a running term and add that to the result:
double result = 1.0;
double term = 1.0;
for (unsigned int i = 1; i != N; ++i)
{
term /= i;
result += term;
}
return result;
You can compute any exp(x) with a small modification.
You have an integer division right here:
soma += 1 / fat;
change it to this:
soma += 1. / fat;
Also be aware that your implementation is very vulnerable to integer overflow when n gets large.
Here's the working version. There were 2 more errors:
int main()
{
int n, i, j, fat;
float soma = 1.0; // Change to 1.0
cin >> n;
for (i = 1; i <= n; i++)
{
fat = 1;
for (j = 1; j <= i; j++)
{
fat *= j;
}
soma += 1. / fat; // Move this to after the loop.
}
cout << soma << endl;
return 0;
}
As mentioned in the comments, you don't need to recompute the factorial at each step.